Reported April 2020
Bloombergsimulation

Instant Runoff Election

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reported this one in April 2020, and the title sounds scarier than the problem is. Instant Runoff Election gives you a grid of ranked ballots and asks who wins after repeated eliminations. If you've got an OA coming, the work is mostly simulation with a hash map of counts, plus tie-break rules you can't afford to misread. The input size is the quiet warning here. Re-scanning every ballot in every round gets slow if ballots are many, so you want a plan before you type. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic below is short enough to carry in your head.

The problem

Every row in ballots ranks all candidates from most to least preferred. In each round, count each ballot for its highest-ranked remaining candidate. A candidate with strictly more than half the ballots wins.
Otherwise eliminate the candidate with the fewest votes, breaking such ties by lexicographically largest name, and repeat. If all remaining candidates tie, return the lexicographically smallest candidate.

Function
instantRunoffWinner(ballots: String[][]) → String

Examples
Example 1
ballots = [["A","B","C"],["A","C","B"],["B","C","A"],["C","B","A"]]
return = "A"
C is eliminated from the initial 2-1-1 round, and its ballot transfers to B; the 2-2 tie eliminates B, leaving A.

Constraints
Every ballot contains the same distinct candidate names.
At least one ballot and candidate exist.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to simulate rounds exactly as written. Keep a set of remaining candidates. Each round, for every ballot, scan left to right and credit the first candidate still in the set. Count with a hash map, and make sure every remaining candidate starts at zero, otherwise a candidate with no first-choice votes never gets eliminated. If someone has strictly more than half of the total ballots, return them. Otherwise find the minimum count, and among the tied minimums remove the lexicographically largest name. The pitfalls are the tie rules. All remaining candidates tied means you return the smallest name, and you must check that before eliminating. Also use strictly more than half, so compare votes * 2 > ballots. There are at most as many rounds as candidates, so the cost is rounds times ballots times candidates. If you blank on the tie ordering live, StealthCoder is the hedge that gets you unstuck quietly.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Instant Runoff Election cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Instant Runoff Election FAQ

How hard is the Bloomberg Instant Runoff Election question really?+

Medium at most. There's no clever algorithm, just careful simulation. Most failures come from the tie-break rules and the zero-vote candidate edge case, not from complexity. If you can write a loop that counts and eliminates, you can solve it.

What's the main trick?+

Recount every round using each ballot's highest-ranked candidate who is still remaining. Initialize counts for all remaining candidates to zero so no-vote candidates can be eliminated. Then apply the majority check, the all-tied check, and the elimination tie-break in that order.

How do I handle the elimination tie-break?+

Find the minimum vote count among remaining candidates. Collect everyone with that count and remove the lexicographically largest name. Note this is opposite to the final all-tied rule, where you return the smallest name. Mixing those two up is the most common bug.

Does brute force pass on large inputs?+

Rescanning ballots each round is the expected approach, and it's fine because rounds are capped at the number of candidates. Don't do anything worse, like rebuilding ballots each round. Skip eliminated names with a set lookup and you're good.

How do I prepare for this in 48 hours?+

Write the simulation once from scratch, then test your own cases: a first-round majority, a full tie, and a zero-vote candidate. Run the sample where C is eliminated, then B, leaving A. That covers nearly every branch the OA can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it