Implement Plurality and Runoff Voting
Company: Coursera
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of voting algorithms, tie-breaking rules, ballot processing, and correctness under edge cases such as incomplete or exhausted ballots, and is commonly asked to assess algorithmic implementation, state simulation, and careful handling of corner cases in interview settings.
Part 1: Simple Plurality Winner
Constraints
- 0 <= len(ballots) <= 10^4
- Each ballot contains distinct candidate names.
- Candidate names are non-empty strings and are compared using normal lexicographic string order.
- Ballots may rank all candidates or only a subset.
Examples
Input: [['Alice', 'Bob'], ['Bob', 'Alice'], ['Alice'], []]
Expected Output: "Alice"
Explanation: Only first choices count: Alice gets 2 votes, Bob gets 1.
Input: [['Bob'], ['Alice'], ['Bob', 'Carol'], ['Alice', 'Bob']]
Expected Output: "Alice"
Explanation: Alice and Bob each get 2 first-place votes, so return the lexicographically smaller name.
Input: [[], [], []]
Expected Output: ""
Explanation: No non-empty ballot contributes a vote.
Input: [['Zoe', 'Yan', 'Xena']]
Expected Output: "Zoe"
Explanation: A single non-empty ballot gives Zoe the win.
Hints
- You only need to inspect ballot[0] for each non-empty ballot.
- After counting first-place votes, find the maximum count and break ties with the lexicographically smallest name.
Part 2: Instant-Runoff Winner
Constraints
- 0 <= len(ballots) <= 2000
- The total number of ranked names across all ballots is at most 20000.
- Each ballot contains distinct candidate names.
- Candidate names are non-empty strings and are compared using normal lexicographic string order.
Examples
Input: [['Alice', 'Bob', 'Carol'], ['Bob', 'Carol', 'Alice'], ['Carol', 'Alice', 'Bob'], ['Carol', 'Bob', 'Alice']]
Expected Output: "Carol"
Explanation: Round 1: Alice 1, Bob 1, Carol 2. Bob is eliminated by the tie rule with Alice. Round 2: Carol receives 3 of 4 active votes and wins.
Input: [['Ann'], ['Bob'], ['Cal']]
Expected Output: "Ann"
Explanation: All three tie in round 1, so eliminate the lexicographically largest: Cal. Then Ann and Bob tie, so eliminate Bob. Ann is the last active candidate.
Input: [['A', 'C'], ['B'], ['C', 'A'], ['C'], []]
Expected Output: "C"
Explanation: Round 1: A 1, B 1, C 2. B is eliminated. In round 2, B's ballot is exhausted, and C gets 2 of 3 non-exhausted votes, which is a strict majority.
Input: [[], []]
Expected Output: ""
Explanation: No candidate appears on any ballot.
Input: [['D', 'C', 'B', 'A'], ['C', 'A'], ['B', 'A'], ['A']]
Expected Output: "A"
Explanation: This case forces ballots to skip over several eliminated candidates across multiple rounds before a winner remains.
Hints
- In each round, scan each ballot from left to right until you find the first candidate who is still active.
- Be careful that the majority threshold is based only on non-exhausted ballots, and candidates with 0 votes can still be eliminated.