MediumArrayHash TableStringSortingCountingUpdated Sep 2026

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

votesABCACBABCACBACB01234Five ballots, each ranking the three teams A, B, C.counts1st2nd3rdABC500023032Row = team, column = position. A leads on 1st place; Cbeats B on 2nd place.
Team A is 1st on every ballot, so A wins. B and C tie at 0 first-place votes, so we compare their 2nd-place counts: C has 3, B has 2.

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

Example 1
Inputvotes = ["ABC","ACB","ABC","ACB","ACB"]
Output"ACB"
WhyA is 1st on all 5 ballots, so A wins. B and C are both never 1st, so we look at 2nd place: C is 2nd on 3 ballots while B is 2nd on only 2. So C beats B, giving ACB.
Example 2
Inputvotes = ["WXYZ","XYZW"]
Output"XWYZ"
WhyW and X each get one 1st-place vote, so we move to 2nd place. X has one 2nd-place vote and W has none, so X ranks above W. Y and Z each have one 3rd-place vote, so we fall back to the alphabet: Y before Z.
Example 3
Inputvotes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output"ZMNAGUEDSJYLBOPHRQICWFXTVK"
WhyWith only one voter, each team has exactly one vote at its position, so no team beats another on counts. The order is simply that voter's order.

Asked At

1 companies
CompanyDifficulty
AtlassianMediumView all Atlassian questions →

How to Think About It

1

Brute force: for each rank position, count how many teams got that rank and sort by those counts.

2

Use a hash map to store each team's vote counts at each rank position (m positions for m teams).

3

Build a comparison key for each team: (rank 0 count, rank 1 count, ..., team name) — sort teams by this key in descending order.

4

Handle ties: when two teams have identical vote counts across all positions, use lexicographic order of the team name as the tiebreaker.

5

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

counts1st2nd3rdABC500023032Step 1 — count how many voters placed each team at eachposition.compare 1st-place countstie? compare 2nd-place countstie? compare 3rd-place countsstill tie? smaller letter firstStep 2 — the comparison order used by the sort.resultACB012Step 3 — sorted order ACB: A first, then C, then B.key(team) = (count[0], count[1], ..., count[m-1], team)sort keys descending, but team name ascending
Build one count row per team, then sort rows by the counts left to right, breaking a full tie with the team letter.
ComplexityTime O(n * m + m^2 * log m)Space O(m^2)n is the number of voters and m the number of teams: counting costs n*m, and each comparison of two teams may scan up to m positions, giving m*log m comparisons.

What Trips People Up in Real Interviews

1

Clarify the ranking criteria: number of first-place votes first, then second-place, and so on.

2

Ask what happens in a complete tie across all positions — lexicographic order breaks it.

3

Walk through a concrete example with 3 voters and 3 teams to demonstrate the counting.

4

Explain the data structure: a dictionary mapping each team name to an array of m integers.

5

Discuss why you sort in descending order for vote counts but ascending for team names.

Solution Code

O(n * m + m^2 * log m) time
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)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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.