Implement plurality and majority voting
Problem Description
You are given an array votes of length n, where each element represents a candidate identifier (which can be a string or an integer). Your task is to implement two distinct voting systems based on this input.
1) Plurality Vote
Determine and return the candidate who received the highest number of votes.
- If multiple candidates are tied for the highest vote count, the tie must be broken by returning the lexicographically smallest candidate if identifiers are strings, or the smallest numeric ID if identifiers are integers.
2) Majority Vote (Boyer–Moore)
Determine and return the candidate who has strictly more than n/2 votes.
- If no such candidate exists, return
null(orNone). - This implementation must adhere to specific performance constraints:
O(n)time complexity andO(1)extra space complexity (excluding the input array itself). This implies the use of the Boyer–Moore majority vote algorithm.
Input and Output
- Input:
votes: List[Candidate] - Output:
pluralityWinner(votes) -> CandidatemajorityWinner(votes) -> Candidate | null
Constraints
- The length of the
votesarray,n, will be between1and10^6(inclusive). - Candidate identifiers are guaranteed to be comparable for tie-breaking purposes.
Examples
votes = ["A", "B", "A", "C", "B", "A"]- Plurality winner:
"A"(3 votes) - Majority winner:
null(3 is not strictly greater than 6/2)
- Plurality winner:
votes = [2, 2, 1, 2]- Plurality winner:
2 - Majority winner:
2(3 is strictly greater than 4/2)
- Plurality winner:
AI interview prep
Core dilemma
The central engineering conflict is balancing efficient vote counting and tie-breaking for the plurality system with the strict O(N) time and O(1) space constraints imposed by the Boyer-Moore algorithm for the majority vote system.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement plurality and majority voting
Problem Description
You are given an array votes of length n, where each element represents a candidate identifier (which can be a string or an integer). Your task is to implement two distinct voting systems based on this input.
1) Plurality Vote
Determine and return the candidate who received the highest number of votes.
- If multiple candidates are tied for the highest vote count, the tie must be broken by returning the lexicographically smallest candidate if identifiers are strings, or the smallest numeric ID if identifiers are integers.
2) Majority Vote (Boyer–Moore)
Determine and return the candidate who has strictly more than n/2 votes.
- If no such candidate exists, return
null(orNone). - This implementation must adhere to specific performance constraints:
O(n)time complexity andO(1)extra space complexity (excluding the input array itself). This implies the use of the Boyer–Moore majority vote algorithm.
Input and Output
- Input:
votes: List[Candidate] - Output:
pluralityWinner(votes) -> CandidatemajorityWinner(votes) -> Candidate | null
Constraints
- The length of the
votesarray,n, will be between1and10^6(inclusive). - Candidate identifiers are guaranteed to be comparable for tie-breaking purposes.
Examples
votes = ["A", "B", "A", "C", "B", "A"]- Plurality winner:
"A"(3 votes) - Majority winner:
null(3 is not strictly greater than 6/2)
- Plurality winner:
votes = [2, 2, 1, 2]- Plurality winner:
2 - Majority winner:
2(3 is strictly greater than 4/2)
- Plurality winner: