Determine All Players with Fixed Rankings
GoogleSoftware Engineer · OnsiteLast updated
Determine which players in a competition have a uniquely determined ranking given a set of pairwise match outcomes.
Problem Statement
There are n players labeled from 1 to n participating in a round-robin style competition. You are given an integer n and a 2D array matches where matches[i] = [u, v] indicates that player u defeated player v (u is ranked strictly higher than v).
The ranking relation is strictly transitive: if player A defeats player B and player B defeats player C, then player A is ranked higher than player C.
A player's exact rank can be uniquely determined if and only if their relative standing against every other player is known (i.e., for every other player x, we can deduce either that p is ranked higher than x or that x is ranked higher than p).
Assume the match results are consistent and contain no contradictions (cycles). Return a sorted list of all player IDs whose exact final ranking can be uniquely determined.
Function Signature
def find_fixed_rank_players(n: int, matches: list[list[int]]) -> list[int]:
pass
Constraints
1 <= n <= 5000 <= matches.length <= 10^4matches[i] = [u, v]with1 <= u, v <= nandu != v- All pairs in
matchesare distinct, and there are no cyclic contradictions.
Examples
Example 1
Input: n = 5, matches = [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]]
Output: [2]
Explanation:
- Player 2 is beaten by 4, 3, and 1, and beats 5.
Player 2 has relations with all 4 other players (3 above, 1 below), so player 2's rank is uniquely 4th.
- For player 1, we know 1 > 2 and 1 > 5, but relative ordering between 1, 3, and 4 is unknown.
Only player 2 has a fixed rank.
Example 2
Input: n = 3, matches = [[1, 2], [2, 3]]
Output: [1, 2, 3]
Explanation:
1 > 2 > 3 forms a total order. Every player's rank is uniquely determined:
Player 1 is 1st, player 2 is 2nd, player 3 is 3rd.
Edge Cases
n = 1: The single player always has rank 1 ([1]).matchesis empty andn > 1: No relationships exist; return[].- Disconnected components: Players with no connections cannot have fixed ranks unless
n = 1.
Hints
Hint 1
A player p has a uniquely determined rank if and only if every other player is known to be either strictly better than p or strictly worse than p.
Hint 2
Model the match results as a directed acyclic graph (DAG) where an edge u -> v means u beat v.
Hint 3
For each node, compute the number of reachable nodes (players beaten) and the number of nodes that can reach it (players who beat it); if the sum equals n - 1, the rank is fixed.
Follow-up questions
- How would you optimize the transitive closure computation if n were up to 2000? (Consider bitset/bit-parallel optimizations for reachability.)
- What if the input contains cycles due to noisy data or upsets? How would you detect them and handle strongly connected components?
- Can you return the actual 1-indexed rank of each determinable player alongside their ID?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.