Election Results - Problem

You are given a table Votes with voter and candidate information.

Table: Votes

Column NameType
votervarchar
candidatevarchar

The combination (voter, candidate) is the primary key for this table. Each row contains the name of a voter and their chosen candidate.

Election Rules:

  • Each person has exactly 1 vote total
  • If a person votes for multiple candidates, their vote is split equally
  • Example: voting for 2 candidates means each gets 0.5 votes

Task: Find the candidate(s) who received the most votes and won the election. If there's a tie, return all winners ordered by candidate name ascending.

Table Schema

Votes
Column Name Type Description
voter PK varchar Name of the person voting
candidate PK varchar Name of the candidate being voted for
Primary Key: (voter, candidate)
Note: Each voter can vote for multiple candidates, splitting their single vote equally

Input & Output

Example 1 — Split Votes with Clear Winner
Input Table:
voter candidate
Alice John
Alice Bob
Bob John
Output:
candidate
John
💡 Note:

Vote Calculation:

  • Alice votes for 2 candidates (John, Bob) → each gets 0.5 votes
  • Bob votes for 1 candidate (John) → John gets 1.0 vote

Final Totals:

  • John: 0.5 + 1.0 = 1.5 votes
  • Bob: 0.5 votes

John wins with 1.5 votes.

Example 2 — Tie Between Candidates
Input Table:
voter candidate
Alice John
Alice Bob
Bob John
Bob Bob
Charlie Bob
Output:
candidate
Bob
💡 Note:

Vote Calculation:

  • Alice votes for 2 candidates → 0.5 each to John and Bob
  • Bob votes for 2 candidates → 0.5 each to John and Bob
  • Charlie votes for 1 candidate → 1.0 to Bob

Final Totals:

  • John: 0.5 + 0.5 = 1.0 vote
  • Bob: 0.5 + 0.5 + 1.0 = 2.0 votes

Bob wins with 2.0 votes.

Example 3 — Single Voter Multiple Candidates
Input Table:
voter candidate
Alice John
Alice Bob
Alice Charlie
Output:
candidate
Bob
Charlie
John
💡 Note:

Vote Calculation:

  • Alice votes for 3 candidates → each gets 1/3 = 0.333... votes

Final Totals:

  • All candidates tie with 0.333... votes each

All three candidates win and are returned in alphabetical order.

Constraints

  • 1 ≤ voter.length, candidate.length ≤ 100
  • Voter and candidate names contain only letters and spaces
  • Each voter votes for at least one candidate

Visualization

Tap to expand
Election Results - Vote Splitting Algorithm INPUT: Votes Table voter_id candidate 1 Alice 1 Bob 2 Alice 3 Bob 3 Carol 4 Carol Vote Splitting Example: Voter 1 Alice 0.5 Bob 0.5 Voter 3 Bob 0.5 Carol 0.5 ALGORITHM STEPS 1 Count Candidates per Voter (GROUP BY) 2 Calculate Fractional Vote = 1/count 3 Sum Votes per Candidate 4 Find MAX Score Return Winner(s) Vote Calculation: Alice: 0.5 + 1.0 = 1.5 Bob: 0.5 + 0.5 = 1.0 Carol: 0.5 + 1.0 = 1.5 MAX = 1.5 (Tie!) Alice and Carol tied FINAL RESULT WINNERS (TIE) Output: ["Alice", "Carol"] Both with 1.5 votes Final Vote Tally: Alice 1.5 Bob 1.0 Carol 1.5 OK - Verified Key Insight: Each voter has exactly 1 total vote, split equally among their candidates. Use GROUP BY to count candidates per voter, calculate fractional votes (1/count), then SUM by candidate to find winner(s). SQL: WITH voter_counts AS (...), fractional_votes AS (...) SELECT candidate WHERE total = MAX(total) TutorialsPoint - Election Results | Optimal Solution
Asked in
Meta 12 Amazon 8 Google 6
28.5K Views
Medium Frequency
~12 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen