Rank Teams by Votes
Asked at Atlassian
Problem
Given an array of strings votes where each string represents a voter's ranking of teams (first character is the voter's top choice), rank the teams by votes. Teams are ranked by the number of first-place votes, then by the number of second-place votes, and so on. If there is a tie, the team with the lexicographically smaller name is ranked higher. Return a string with the teams sorted from rank 1 to rank m.
Asked At
| Company | Difficulty | |
|---|---|---|
| Atlassian | MEDIUM | View all Atlassian questions → |
How to Think About It
Brute force: for each rank position, count how many teams got that rank and sort by those counts.
Use a hash map to store each team's vote counts at each rank position (m positions for m teams).
Build a comparison key for each team: (rank 0 count, rank 1 count, ..., team name) — sort teams by this key in descending order.
Handle ties: when two teams have identical vote counts across all positions, use lexicographic order of the team name as the tiebreaker.
Optimal: O(n * m) to count votes plus O(m * m * log m) for comparison-based sorting, where n is the number of voters and m is the number of teams.
Optimal Approach
Create a hash map where each key is a team character and the value is an array of length m (number of teams) initialized to zeros. For each voter string, iterate through each position and increment the count for the team at that position in that voter's ranking. After counting all votes, sort the teams using a custom comparator: compare by the count at position 0 (descending), then position 1 (descending), and so on. If all counts are equal, compare team names lexicographically (ascending). Concatenate the sorted team names into the result string. Time complexity is O(n * m + m * m * log m).
What Trips People Up in Real Interviews
Clarify the ranking criteria: number of first-place votes first, then second-place, and so on.
Ask what happens in a complete tie across all positions — lexicographic order breaks it.
Walk through a concrete example with 3 voters and 3 teams to demonstrate the counting.
Explain the data structure: a dictionary mapping each team name to an array of m integers.
Discuss why you sort in descending order for vote counts but ascending for team names.
Solution Code
def rankTeams(votes):
m = len(votes[0])
counts = {}
for team in votes[0]:
counts[team] = [0] * m
for ballot in votes:
for i, team in enumerate(ballot):
counts[team][i] += 1
ranked = sorted(counts.keys(), key=lambda t: (counts[t], [-ord(c) for c in t]), reverse=True)
return ''.join(ranked)Frequently Asked Questions
What is the Rank Teams by Votes problem?
Given an array of strings votes where each string represents a voter's ranking of teams (first character is the voter's top choice), rank the teams by votes. Teams are ranked by the number of first-place votes, then by the number of second-place votes, and so on. If there is a tie, the team with the lexicographically smaller name is ranked higher. Return a string with the teams sorted from rank 1 to rank m.
How do you solve Rank Teams by Votes?
The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
What companies ask Rank Teams by Votes?
Rank Teams by Votes is asked at Atlassian. It is a medium difficulty problem.
What are common mistakes on Rank Teams by Votes?
- Clarify the ranking criteria: number of first-place votes first, then second-place, and so on.
- Ask what happens in a complete tie across all positions — lexicographic order breaks it.
- Walk through a concrete example with 3 voters and 3 teams to demonstrate the counting.
- Explain the data structure: a dictionary mapping each team name to an array of m integers.
- Discuss why you sort in descending order for vote counts but ascending for team names.