Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

CODING CHALLENGE · N°49

Raft Leader Election (Majority)

Easy Distributed SystemsConsensusRaft

The heartbeat of the Raft consensus algorithm: a candidate becomes leader only if it wins votes from a strict majority of the cluster. That majority rule is what guarantees at most one leader per term — the property that keeps a distributed system consistent. Decide the outcome of an election round. Solve it in Python or TypeScript, with hidden tests.

The problem

A Raft cluster has n nodes. In an election, a candidate votes for itself and requests votes from the other n-1 peers; you are given grants, a list of booleans (one per peer) saying whether each granted its vote. The candidate becomes leader iff it collects a strict majority — more than half of n, i.e. ⌊n/2⌋ + 1 votes (including its own). Return True if it becomes leader, else False.

SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? open the hints under the problem, or Reveal solution is one click away.

YOUR TASK

Implement election_result(n, grants): count 1 (self) plus the granted votes, and return whether that reaches the majority threshold ⌊n/2⌋ + 1.

CPython · WebAssembly
Examples and constraints 4 examples
EXAMPLE 1
Input n = 5, grants = [True, True, False, False]
Output True
1 self + 2 = 3 votes; majority of 5 is 3
EXAMPLE 2
Input n = 5, grants = [False, False, False, False]
Output False
only its own vote — far short of 3
EXAMPLE 3
Input n = 4, grants = [True, False, False]
Output False
2 votes, but a majority of 4 is 3 — a split vote fails
EXAMPLE 4
Input n = 1, grants = []
Output True
edge case: a single-node cluster wins on its own vote, 1 of 1
CONSTRAINTS
  • The candidate always counts its own vote, so total votes = 1 + (number of True in grants).
  • Majority means strictly more than half: ⌊n/2⌋ + 1. For even n, exactly half is not enough.
  • This strict-majority rule is why two candidates can never both win the same term — any two majorities overlap on at least one node, and a node votes once per term.
Hints 4 ideas
HINTS — 4 IDEAS
  1. Total votes = 1 (the candidate’s own) + the number of True entries in grants.
  2. The threshold to win is n // 2 + 1 — a strict majority of the whole cluster, not of those who replied.
  3. Return total_votes >= threshold.
  4. Note that a single-node cluster (n = 1) always elects its candidate, and an even cluster can suffer a split vote where no one wins.
Approach, complexity & discussion — open after you solve

Start from brute force

The version most people write first is “at least half”: 1 + sum(grants) >= n / 2. It looks right and passes every odd cluster, because n / 2 is never a whole number there, so reaching 2.5 of 5 already means 3 votes. It costs O(n) time for the one pass over grants and O(1) space, so speed is not the problem; correctness is. On an even cluster it accepts a tie: (4, [True, False, False]) has 2 votes, 2 ≥ 2.0 is true, and it returns True where the answer is False. Two candidates holding disjoint pairs of a 4-node cluster would then both become leader in the same term.

The approach

The insight is that a Raft election is decided by a fixed number, not by who replied: the candidate needs a strict majority of the whole cluster, n // 2 + 1 votes, and its own vote counts toward it. Any two sets of that size share at least one node, and a node votes at most once per term, so two candidates can never both reach it in the same term. That overlap is the entire safety argument, which is why the bar is computed from n and nothing else.

The reference does it in three lines. votes = 1 + sum(1 for g in grants if g) adds the self-vote to the granted ones (in TypeScript, grants.filter((g) => g).length). majority = n // 2 + 1 uses integer division, so for n = 4 the bar is 3, not 2. It returns votes >= majority, the same test as votes > n // 2. A single-node cluster passes on its own vote, 1 ≥ 1.

Walk through an example

The first example, n = 5 and grants = [True, True, False, False], counted the way the reference counts it.

stepreadsvotesnote
1self-vote1the 1 + in the sum
2grants[0] = True2granted
3grants[1] = True3granted
4grants[2] = False3not counted
5grants[3] = False3not counted
6majority = 5 // 2 + 13the bar is 3
7votes >= majority33 ≥ 3, returns True

Complexity

Reference: O(n) time, one pass over the n − 1 entries of grants, and O(1) extra space, because the generator counts without building a list (the TypeScript filter builds an O(n) array; a loop counter avoids it). Naive “at least half”: the same O(n) time and O(1) space; its defect is the threshold, not the cost.

Edge cases to test

  • n = 1, grants = []: True. The self-vote alone meets the bar of 1 // 2 + 1 = 1.
  • n = 2, grants = [False]: False. A two-node cluster needs both votes, so one failed node stops all elections; the “at least half” rule wrongly returns True here.
  • n = 4, grants = [True, False, False]: False. Two of four is a tie, and an even cluster needs n / 2 + 1 = 3.
  • n = 5, grants = [False, False, False, False]: False. Only the self-vote, 1 of the 3 needed.
  • n = 6, grants = [True, True, False, False, False]: False. Three votes is exactly half of six; the bar is 4.

Common mistakes

  • Forgetting the self-vote: sum(grants) alone gives 2 on the first example, short of 3, so a candidate that won is reported as losing.
  • Testing “at least half” with votes >= n / 2. It passes every odd cluster and accepts a tie on every even one, e.g. (4, [True, False, False]).
  • Computing the bar from len(grants) or from the peers that replied instead of from n. The bar then shrinks when nodes are unreachable, which is exactly when the overlap guarantee matters.
  • Treating the majority rule as sufficient on its own. It prevents two leaders only because every node grants at most one vote per term; a voter that grants twice breaks the overlap argument.

Follow-ups an interviewer asks next

Now write the voter side. When should a node grant its vote?
Reject a request whose term is older than the voter’s own; a newer term makes the voter adopt it and become a follower first. Grant only if it has not already voted for a different candidate in that term and the candidate’s log is at least as up to date: a higher last-log term, or the same last-log term and a last index at least as large. Persist the term and vote before replying, so a restart cannot vote twice.
What happens when no candidate reaches the majority?
Nobody leads that term. Each candidate’s election timer expires and it starts a new election in the next term. Raft makes repeats unlikely with randomized election timeouts (the paper’s example range is 150 to 300 ms), so one node usually times out first, wins, and starts sending heartbeats before the others time out again.
Votes arrive one at a time over the network. How do you decide as they come in?
Keep a set of the node ids that granted, so a retried or duplicated reply is never counted twice, and become leader the moment the set plus the self-vote reaches n // 2 + 1. Ignore replies stamped with an older term, and step down to follower as soon as any reply carries a higher term than your own.
Membership is changing. What is n during a reconfiguration?
In Raft’s joint consensus, decisions need separate majorities of both the old and the new configuration, so the check becomes two calls of this function, one per member set, and both must pass. Counting only the new configuration could let an old-configuration majority and a new-configuration majority each elect a leader in the same term.

Where this shows up

Raft leader election is how consensus systems like etcd and Consul pick a single leader safely despite crashes and partitions. The term counter, majority vote, and one-vote-per-term rules are precisely what guarantee at most one leader per term — the invariant the rest of the protocol (and your cluster’s correctness) rests on.

Reference solution

Reference solution — Python
def election_result(n, grants):
    votes = 1 + sum(1 for g in grants if g)   # self-vote plus granted votes
    majority = n // 2 + 1                      # strict majority of the cluster
    return votes >= majority


print(election_result(5, [True, True, False, False]))   # -> True
print(election_result(4, [True, False, False]))         # -> False
Reference solution — TypeScript
function electionResult(n: number, grants: boolean[]): boolean {
  const votes = 1 + grants.filter((g) => g).length;   // self + granted votes
  const majority = Math.floor(n / 2) + 1;              // strict majority
  return votes >= majority;
}

console.log(electionResult(5, [true, true, false, false]));  // -> true
console.log(electionResult(4, [true, false, false]));        // -> false

Questions about Raft Leader Election (Majority)

Why does Raft need a strict majority instead of half the votes?

Because two strict majorities of the same cluster always share at least one node, and each node votes at most once per term. That shared node can only have voted for one of the two candidates, so at most one candidate can collect a majority in any term. Half is not enough: in a 4-node cluster, two candidates can each get 2 votes from disjoint pairs, and a half-the-votes rule would crown both. With n // 2 + 1, a 4-node cluster needs 3 votes, and any two groups of 3 out of 4 must overlap.

Does the candidate vote for itself in a Raft election?

Yes. When its election timeout fires, a follower increments its current term, becomes a candidate, votes for itself, and sends RequestVote messages to every other node. That self-vote counts toward the majority, which is why the reference adds 1 to the granted votes. It also means a single-node cluster elects itself immediately: one vote against a bar of 1 // 2 + 1 = 1. Forgetting the self-vote makes the bar effectively one vote higher, so a candidate holding exactly a majority, such as 3 of 5, would be reported as losing.

Why are Raft clusters usually an odd number of nodes?

Because an even cluster costs one more machine without tolerating one more failure. A cluster can elect a leader while a majority is reachable, so it survives n minus (n // 2 + 1) failed nodes. Three nodes need 2 votes and survive one failure; four nodes need 3 votes and still survive only one. Five survive two, and six still survive two. The extra even node adds replication work and one more vote to collect without raising fault tolerance, and a partition that splits an even cluster down the middle leaves no side with a majority.

Is a majority of the replies enough to win a Raft election?

No. The bar is a majority of the whole configured cluster, n // 2 + 1, and a node that never answers counts as a vote not granted. Counting only replies breaks safety during a partition: in a 5-node cluster split into groups of 2 and 3, a candidate on the 2-node side would hold both votes it can reach and claim leadership while the 3-node side elects its own leader. With the cluster-wide bar of 3, only the larger side can elect anyone, and the smaller side keeps retrying until the partition heals.

Finished this one? 0 / 76 Challenges done
NextThe Quorum Dial Next in “Distributed Systems Deep Dive” · step 8 of 20

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Challenges