Google questions

Fixed Ranks

GoogleOnsiteMedium

There are n players numbered 0 to n - 1. Each game result is a [winner, loser] pair meaning the winner ranks strictly better than the loser, with rank 0 the best. Better-than carries over: if a beat b and b beat c, then a ranks better than c even if they never played.

Part 1: Determined ranks

You are given n players numbered 0 to n - 1 and a list game_results of [winner, loser] pairs. Each pair says the winner ranks strictly better than the loser, and this carries over transitively: if a beat b and b beat c, then a ranks better than c even if they never played.

A player's rank is the number of players that rank strictly better than them. Return a [player, rank] pair for every player whose rank is the same in every complete ordering of the n players that agrees with all results, sorted by player. Leave out players whose rank can vary between orderings.

  1. Example 1
    n
    5
    game_results
    [[0,1],[1,2],[2,3],[2,4],[3,4]]
    Output
    [[0,0],[1,1],[2,2],[3,3],[4,4]]

    Why: The results form a chain 0, 1, 2, 3, 4 (2 beats 4 is implied by 2 beats 3 beats 4), so every player is comparable with every other and each player's rank equals the number of players above them in the chain.

Constraints

  • 1 <= n <= 500
  • 0 <= len(game_results) <= n * (n - 1) / 2
  • 0 <= winner, loser < n and winner != loser
  • No two results in game_results contradict each other
  • The same [winner, loser] pair may appear more than once in game_results
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Fixed Ranks: Google interview question | LoopCoach