Implement Popular and Ranked Voting
Problem Description
You are provided with aggregated ranked ballots. Each entry in the input dictionary represents a group of voters who submitted an identical preference order for candidates. The key is an ordered tuple of candidate names, indicating a voter's preference from highest to lowest, and the corresponding value is the count of voters who submitted that exact ranking.
You need to implement two distinct methods:
1. popular_vote(ballots) -> candidate
This method should identify and return the candidate who received the largest number of first-place votes across all ballots.
2. ranked_choice_winner(ballots) -> candidate
This method implements the ranked-choice voting system, following these rules:
- In each round, count only the highest-ranked non-eliminated candidate on each ballot.
- If any candidate achieves more than 50% of the active first-place votes, that candidate is declared the winner and returned.
- Otherwise, the candidate with the fewest first-place votes in that round is eliminated. The process then repeats with the remaining candidates.
- The process continues until a candidate secures a majority of active votes or only one candidate remains, who is then declared the winner.
Assumptions
- Every ballot ranks the same set of candidates.
- There will be no ties when determining which candidate to eliminate in a round.
Example Input
ballots = {
("A", "B", "C"): 4,
("B", "C", "A"): 3,
("C", "B", "A"): 2
}
Example Output for Input
- The popular-vote winner is
A, asAinitially has the most first-place votes. - The ranked-choice winner is
B:- Round 1: Initial counts are
A=4, B=3, C=2. CandidateChas the fewest votes and is eliminated. - Round 2:
C's ballots (2 votes) transfer to their next preference,B. The new counts areA=4, B=3+2=5.Bnow has 5 votes out of 9 total active votes (55.5%), which is more than 50%. Therefore,Bis the ranked-choice winner.
- Round 1: Initial counts are
AI interview prep
Core dilemma
Efficiently simulating an iterative, state-changing voting process (ranked-choice) where ballot preferences need to be dynamically re-evaluated and votes transferred without excessive re-computation, especially for a potentially large number of ballots and candidates.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Popular and Ranked Voting
Problem Description
You are provided with aggregated ranked ballots. Each entry in the input dictionary represents a group of voters who submitted an identical preference order for candidates. The key is an ordered tuple of candidate names, indicating a voter's preference from highest to lowest, and the corresponding value is the count of voters who submitted that exact ranking.
You need to implement two distinct methods:
1. popular_vote(ballots) -> candidate
This method should identify and return the candidate who received the largest number of first-place votes across all ballots.
2. ranked_choice_winner(ballots) -> candidate
This method implements the ranked-choice voting system, following these rules:
- In each round, count only the highest-ranked non-eliminated candidate on each ballot.
- If any candidate achieves more than 50% of the active first-place votes, that candidate is declared the winner and returned.
- Otherwise, the candidate with the fewest first-place votes in that round is eliminated. The process then repeats with the remaining candidates.
- The process continues until a candidate secures a majority of active votes or only one candidate remains, who is then declared the winner.
Assumptions
- Every ballot ranks the same set of candidates.
- There will be no ties when determining which candidate to eliminate in a round.
Example Input
ballots = {
("A", "B", "C"): 4,
("B", "C", "A"): 3,
("C", "B", "A"): 2
}
Example Output for Input
- The popular-vote winner is
A, asAinitially has the most first-place votes. - The ranked-choice winner is
B:- Round 1: Initial counts are
A=4, B=3, C=2. CandidateChas the fewest votes and is eliminated. - Round 2:
C's ballots (2 votes) transfer to their next preference,B. The new counts areA=4, B=3+2=5.Bnow has 5 votes out of 9 total active votes (55.5%), which is more than 50%. Therefore,Bis the ranked-choice winner.
- Round 1: Initial counts are