Code Room
CodingHardcod-g1098
Subject Raft log consistency and commit index from matchIndexLevel Senior–Staff~35 minCommon in Databases & SQL · Distributed systems interviewsIndustries Technology

Question

In Raft, the leader advances its commit index using replication progress and the term-safety rule. You are given the number of servers `n` (the leader plus followers; the leader counts as having replicated everything), `match_index`: a list of length n giving the highest log index known replicated on each server (the leader's own entry equals its last log index), `log_terms`: a list where log_terms[i] is the term of log entry at 1-based index i+1 (so log_terms has length = leader's last log index), and `current_term`. Return the highest log index `idx` (1-based) such that a MAJORITY of servers have match_index >= idx AND log_terms[idx-1] == current_term. If no such index exists, return 0. This is the rule that prevents committing entries from prior terms by counting alone.

Implement
raft_commit_index(n: int, match_index: list[int], log_terms: list[int], current_term: int) → int
Examples
in[3,[3,3,2],[1,2,2],2]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.