Rank Teams by Votes - Problem
Election Committee Challenge: Rank Teams by Votes

Imagine you're running a democratic voting system for a programming competition! ๐Ÿ—ณ๏ธ Each voter ranks ALL participating teams from their most favorite (position 1) to least favorite (last position).

Here's how the special ranking algorithm works:
1. Primary Sort: Count position-1 votes - team with most first-place votes wins
2. Tie Breaking: If teams tie for first-place votes, compare their second-place votes, then third-place, and so on
3. Final Tie Breaker: If still tied after all positions, sort alphabetically by team letter

Input: Array of vote strings where each string represents one voter's complete ranking
Output: Single string of team letters sorted by the ranking algorithm

Example: If votes are ["ABC", "ACB", "ABC"]:
- Team A: 3 first-place votes โ†’ Winner!
- Team B: 2 second-place, 1 third-place
- Team C: 1 second-place, 2 third-place
Result: "ABC"

Input & Output

example_1.py โ€” Basic Ranking
$ Input: votes = ["ABC", "ACB", "ABC"]
โ€บ Output: "ABC"
๐Ÿ’ก Note: Team A gets 3 first-place votes and wins. For second place, B gets 2 second-place votes vs C's 1, so B beats C.
example_2.py โ€” Tie Breaking
$ Input: votes = ["WXYZ", "XYZW", "YZWX", "ZWXY"]
โ€บ Output: "XWYZ"
๐Ÿ’ก Note: All teams tie with 1 first-place vote each. Check second-place: W=1, X=1, Y=1, Z=1 (still tied). Continue checking until differences emerge, then use alphabetical order.
example_3.py โ€” Single Voter
$ Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
โ€บ Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
๐Ÿ’ก Note: With only one voter, the result is simply that voter's preference order since there are no ties to break.

Constraints

  • 1 โ‰ค votes.length โ‰ค 1000
  • 1 โ‰ค votes[i].length โ‰ค 26
  • votes[i].length == votes[j].length for all i, j
  • votes[i][j] is an English uppercase letter
  • All characters of votes[i] are unique
  • All the characters that occur in votes[0] also occur in votes[j] where 1 โ‰ค j < votes.length

Visualization

Tap to expand
Democratic Team Ranking Visualization๐Ÿ“Š Vote CollectionVoter 1: A B CVoter 2: A C BVoter 3: A B CComplete rankings๐Ÿ—ณ๏ธ Vote TallyingPosition 1: A=3, B=0, C=0Position 2: A=0, B=2, C=1Position 3: A=0, B=1, C=2Count by position๐Ÿ† Final Ranking1st: Team A2nd: Team B3rd: Team CFinal result: "ABC"๐Ÿ” Ranking Algorithm1Compare 1st place votes: A(3) > B(0) = C(0)2For tie between B & C, compare 2nd place: B(2) > C(1)3If still tied, continue to 3rd place, 4th place, etc.ฮฑFinal tiebreaker: alphabetical order (A < B < C)
Understanding the Visualization
1
Collect Ballots
Each voter submits a complete ranking of all teams
2
Tally Votes
Count how many votes each team got at each position
3
Apply Rules
Sort by 1st place votes, then 2nd place, then 3rd, etc.
4
Break Ties
Use alphabetical order if teams are still tied after all positions
Key Takeaway
๐ŸŽฏ Key Insight: This is essentially lexicographic sorting on multi-dimensional vectors where each team's vote counts form a vector [pos1_votes, pos2_votes, ...], and we sort by comparing these vectors position by position with alphabetical names as the final tiebreaker.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~18 min Avg. Time
780 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