Election Results - Problem

You're analyzing the results of a modern city election where voters can cast their ballots for multiple candidates or choose not to vote at all. Here's the twist: when someone votes for multiple candidates, their single vote gets equally split among all their chosen candidates.

For example, if Alice votes for both candidate "John" and candidate "Mary", then John receives 0.5 votes and Mary receives 0.5 votes from Alice.

Your task: Determine which candidate(s) won the election by receiving the most total votes. If there's a tie for the highest vote count, return all tied candidates.

Input: A database table Votes with columns voter (varchar) and candidate (varchar). Each row represents one voter's choice for one candidate.

Output: The name(s) of the winning candidate(s), ordered alphabetically.

Input & Output

example_1.sql โ€” Basic Election
$ Input: Votes table: | voter | candidate | |-------|----------| | Alice | John | | Alice | Mary | | Bob | John | | Carol | Mary |
โ€บ Output: | candidate | |----------| | John | | Mary |
๐Ÿ’ก Note: Alice votes for 2 candidates, so John and Mary each get 0.5 votes from Alice. Bob gives John 1.0 vote, Carol gives Mary 1.0 vote. Final: John=1.5, Mary=1.5 (tie).
example_2.sql โ€” Clear Winner
$ Input: Votes table: | voter | candidate | |-------|----------| | Alice | John | | Bob | John | | Carol | Mary |
โ€บ Output: | candidate | |----------| | John |
๐Ÿ’ก Note: Alice gives John 1.0 vote, Bob gives John 1.0 vote, Carol gives Mary 1.0 vote. Final: John=2.0, Mary=1.0. John wins.
example_3.sql โ€” Heavy Vote Splitting
$ Input: Votes table: | voter | candidate | |-------|----------| | Alice | John | | Alice | Mary | | Alice | Peter | | Bob | John |
โ€บ Output: | candidate | |----------| | John |
๐Ÿ’ก Note: Alice splits her vote 3 ways: John, Mary, and Peter each get 0.33 votes from Alice. Bob gives John 1.0 vote. Final: John=1.33, Mary=0.33, Peter=0.33.

Visualization

Tap to expand
Election Vote Splitting ProcessAliceVotes for: John, MaryVote weight: 1.0 รท 2 = 0.5 eachBobVotes for: JohnVote weight: 1.0 รท 1 = 1.0CarolVotes for: MaryVote weight: 1.0 รท 1 = 1.0Vote TotalsJohn: 0.5 + 1.0 = 1.5 votesMary: 0.5 + 1.0 = 1.5 votesResult: TIE! Both win๐ŸŽฏ Key InsightEach voter has exactly 1.0 total vote power that gets split equallyamong all candidates they choose. Use window functions to count and divide efficiently!
Understanding the Visualization
1
Vote Collection
Each voter submits their choices - they can pick multiple candidates
2
Vote Splitting
Each voter's single vote gets divided equally among their chosen candidates
3
Vote Counting
Sum up all fractional votes received by each candidate
4
Winner Selection
Find candidate(s) with the highest vote total
Key Takeaway
๐ŸŽฏ Key Insight: The key is recognizing that each voter's total influence is always 1.0, regardless of how many candidates they vote for. Window functions let us calculate the split efficiently in SQL.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through data with efficient window function calculations

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space proportional to number of unique candidates (k), not total votes (n)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of votes โ‰ค 104
  • 1 โ‰ค voter.length, candidate.length โ‰ค 20
  • voter and candidate contain only lowercase English letters
  • Each voter can vote for multiple candidates or not vote at all
  • The combination (voter, candidate) is unique in the table
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28 Netflix 22
24.6K Views
Medium-High Frequency
~18 min Avg. Time
892 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