Rank Teams from Ballot Preferences
Company: Netapp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## Problem
Each vote ranks every team exactly once, from best to worst. Rank teams by these rules:
1. The team with more first-place votes ranks higher.
2. If tied, compare second-place vote counts, then third-place counts, continuing through every position.
3. If every position count ties, the alphabetically smaller team ID ranks higher.
Return the team IDs concatenated in final rank order.
### Function Contract
Implement `rankTeams(votes)` and return a string.
### Constraints & Assumptions
- `1 <= len(votes) <= 1,000`.
- `1 <= len(votes[0]) <= 26`.
- Every vote contains the same distinct uppercase English letters exactly once, although their order differs.
- Team IDs are single uppercase letters.
### Clarifying Questions to Ask
- Are all teams present in every vote? Yes.
- Does a first differing position decide the comparison? Yes.
- Is alphabetical order ascending for a complete tie? Yes.
- Can there be only one vote or one team? Yes.
```hint Build one position-count vector per team
Sort teams by the negative counts at positions `0..t-1`, followed by the team letter.
```
### Examples
- `votes = ["ABC", "ACB", "ABC", "ACB", "ACB"]` returns `"ACB"`.
- `votes = ["WXYZ", "XYZW"]` returns `"XWYZ"`.
- `votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]` returns the same string.
### Evaluation Focus
- Accumulates counts at every rank position for every team.
- Compares count vectors in the correct descending direction and applies alphabetical fallback.
- Derives the team set from the ballots rather than assuming all 26 letters.
- Runs in `O(v * t + t^2 log t)` time or better for `v` votes and `t` teams.
### Extensions to Discuss
1. How would missing teams on some ballots change the rule?
2. What if team IDs are arbitrary strings rather than letters?
3. How would weighted voters affect the count vectors?
Overview: Rank teams from complete preference ballots by comparing vote counts at each position in order, then use alphabetical team ID as the final tie-break.
Every ballot ranks the same uppercase teams. Rank teams lexicographically by their vectors of position counts in descending count order; if every count ties, use ascending team letter. Return the concatenated team IDs.
Constraints
- 1 <= number of votes <= 1000.
- 1 <= teams <= 26.
- Every ballot contains the same distinct uppercase letters exactly once.
- Alphabetical ascending order breaks complete count ties.
Examples
Input: (['ABC', 'ACB', 'ABC', 'ACB', 'ACB'],)
Expected Output: 'ACB'
Explanation: A dominates first place and C beats B at second.
Input: (['WXYZ', 'XYZW'],)
Expected Output: 'XWYZ'
Explanation: Position vectors resolve the ranking.
Hints
- Count each team at every rank position.
- Compare count vectors from first place toward last place.