KatInterview

Plurality and Instant-Runoff Vote Counting

Arrays & HashingAuto-graded · 13 test cases
HardAuto-gradedArrays & Hashingsimulationhash-mapvoting-algorithms

Last updated

Tests aggregated vote counting, majority detection, and iterative candidate elimination in ranked-choice systems.

You are implementing two vote-counting methods for an election. Ballots are provided in compressed form: each entry is a list containing a ranking tuple and a count, where the ranking lists candidates from most to least preferred, and the count indicates how many voters submitted that exact preference order.

Implement two functions:

plurality_winner(ballots)

Return the candidate who received the most top-preference votes. If multiple candidates tie for the highest count, return the lexicographically smallest candidate name among those tied.

instant_runoff_winner(ballots)

Count votes using instant-runoff (ranked-choice) rules:

  • In each round, tally votes by looking at each ballot and crediting the highest-ranked candidate who has not yet been eliminated.
  • If any candidate receives strictly more than 50% of the total votes in that round, that candidate wins immediately.
  • Otherwise, eliminate the candidate with the fewest votes in the current round.
  • Repeat until a winner emerges or only one candidate remains.

You may assume:

  • All ballots rank the same complete set of candidates (no partial rankings).
  • No ties occur when determining which candidate to eliminate.
  • Candidate names are non-empty strings.
  • At least one ballot is provided.

Input format: A list of [ranking, count] pairs, where ranking is a tuple of candidate names and count is a positive integer.

Example:

ballots = [
    [("Alice", "Bob", "Carol"), 4],
    [("Bob", "Carol", "Alice"), 3],
    [("Carol", "Bob", "Alice"), 2]
]

For this input:

  • plurality_winner(ballots) returns "Alice" (4 top-preference votes vs. 3 for Bob and 2 for Carol).
  • instant_runoff_winner(ballots) returns "Bob":
    • Round 1: Alice=4, Bob=3, Carol=2 (total 9). No majority. Eliminate Carol.
    • Round 2: Alice=4, Bob=5 (Carol's 2 voters now support Bob). Bob has 5 out of 9, which exceeds 50%, so Bob wins.

Constraints

  • 1 ≤ number of ballot groups ≤ 1000
  • 1 ≤ number of candidates ≤ 50
  • 1 ≤ count per ballot group ≤ 10⁶
  • All candidate names consist of uppercase and lowercase letters only
  • Total vote count fits in a 32-bit integer

Examples

Example 1

Input: plurality_winner([[['A', 'B', 'C'], 4], [['B', 'C', 'A'], 3], [['C', 'B', 'A'], 2]])

Output: 'A'

Hints

Hint 1

For plurality, accumulate first-choice counts in a dictionary, then find the maximum and break ties by name order.

Hint 2

In instant-runoff, track which candidates are still active. Each round, scan ballots and credit the first non-eliminated candidate on each ranking.

Hint 3

Initialize every active candidate's count to zero each round—candidates with no current support must still be eligible for elimination.