1366. Rank Teams by Votes
Asked at Atlassian
Difficulty
Medium
Time
O(n * m + m^2 * log m)
Space
O(m^2)
Pattern
Array
Companies
1
Problem
A group of voters each rank the same set of teams from best to worst. Every voter ranks all the teams, and each voter's string lists the teams in that voter's order (first character = that voter's top pick).
To decide the final order, compare teams by how many voters put them in 1st place. The team with more 1st-place votes ranks higher. If two teams are tied there, compare their number of 2nd-place votes, then 3rd-place votes, and so on through every position. If teams are still tied after all positions, the team whose letter comes earlier in the alphabet ranks higher.
Return one string containing all the teams in final ranked order, best team first.
Tally each team's votes per position
Constraints
- ●
1 <= votes.length <= 1000 - ●
1 <= votes[i].length <= 26 - ●
All votes[i] have the same length. - ●
votes[i][j] is an English uppercase letter. - ●
All characters inside one votes[i] are unique. - ●
Every character in votes[0] also appears in every other votes[j].
Examples
votes = ["ABC","ACB","ABC","ACB","ACB"]"ACB"votes = ["WXYZ","XYZW"]"XWYZ"votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]"ZMNAGUEDSJYLBOPHRQICWFXTVK"Asked At
1 companies| 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).
Sort teams by their count vector
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
O(n * m + m^2 * log m) timedef 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 runs in O(n * m + m^2 * log m) time and O(m^2) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
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.