Decide the outcome of a cat and mouse chase on an undirected graph under optimal play

Quick Overview

Decide the result of a pursuit game on an undirected graph where a mouse tries to reach a hole, a cat tries to catch it, and a repeated position is a draw. It tests modeling a two-player game as a set of states and reasoning about optimal play when those states form cycles.

Decide the outcome of a cat and mouse chase on an undirected graph under optimal play

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

A game on an undirected graph is played by two players, a mouse and a cat, who alternate turns. The graph is given as an adjacency list: `graph[a]` is a list of all nodes `b` such that `a` and `b` are joined by an edge. The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at node `0`. During each player's turn, they must travel along one edge of the graph that meets the node where they are. For example, if the mouse is at node `1`, it must travel to some node in `graph[1]`. The cat is not allowed to travel to the hole (node `0`). - If the mouse reaches the hole before the cat reaches the mouse, the mouse wins. - If the cat reaches the mouse before the mouse reaches the hole, the cat wins. - If a position is repeated (both players are back on the same nodes as at an earlier point, with the same player to move), the game is a draw. Given `graph`, and assuming both players play optimally, determine the outcome of the game. Return `1` if the mouse wins, `2` if the cat wins, or `0` if the game is a draw. ### Function Signature ```python def cat_mouse_game(graph: list[list[int]]) -> int: ``` ### Rules - The graph is undirected: `b` appears in `graph[a]` exactly when `a` appears in `graph[b]`. - Staying in place is not a move. The cat's legal moves are the neighbors of its node other than `0`. - The cat wins as soon as both players are on the same node, whichever of them moved there. A mouse that steps onto the cat's node loses. - Optimal play means each player forces a win when it can, and otherwise forces a draw when it can. ### Constraints - `3 <= len(graph) <= 50` - `1 <= len(graph[i]) < len(graph)` - `0 <= graph[i][j] < len(graph)` - `graph[i][j] != i` - The values in each `graph[i]` are distinct. - The mouse and the cat can always move. ### Examples ```text Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]] Output: 0 ``` The edges are 0-2, 0-5, 1-3, 2-4, 2-5, 3-4 and 3-5. The mouse must first move to 3, and if the cat answers with 4 the mouse escapes through 5, so the cat goes to 5 to guard the hole. After that the mouse's only safe move is to 4, the cat must reply with 2 (moving to 3 would let the mouse reach the hole through 2), and the mouse returns to 3. That repeats the position after the mouse's first move, and every move in this cycle is the only one that does not lose, so the game is a draw. ```text Input: graph = [[1,3],[0],[3],[0,2]] Output: 1 ``` Node 1 is adjacent to the hole, so the mouse moves to node 0 on its first turn and wins. ```text Input: graph = [[3],[3],[3],[0,1,2]] Output: 2 ``` The mouse's only move is to node 3, which is adjacent to the cat. The cat then moves onto node 3 and catches the mouse.

Overview: Decide the result of a pursuit game on an undirected graph where a mouse tries to reach a hole, a cat tries to catch it, and a repeated position is a draw. It tests modeling a two-player game as a set of states and reasoning about optimal play when those states form cycles.

Google logo
Google
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

A game on an undirected graph is played by two players, a mouse and a cat, who alternate turns.

The graph is given as an adjacency list: graph[a] is a list of all nodes b such that a and b are joined by an edge.

The mouse starts at node 1 and goes first, the cat starts at node 2 and goes second, and there is a hole at node 0.

During each player's turn, they must travel along one edge of the graph that meets the node where they are. For example, if the mouse is at node 1, it must travel to some node in graph[1]. The cat is not allowed to travel to the hole (node 0).

  • If the mouse reaches the hole before the cat reaches the mouse, the mouse wins.
  • If the cat reaches the mouse before the mouse reaches the hole, the cat wins.
  • If a position is repeated (both players are back on the same nodes as at an earlier point, with the same player to move), the game is a draw.

Given graph, and assuming both players play optimally, determine the outcome of the game. Return 1 if the mouse wins, 2 if the cat wins, or 0 if the game is a draw.

Function Signature

def cat_mouse_game(graph: list[list[int]]) -> int:

Rules

  • The graph is undirected: b appears in graph[a] exactly when a appears in graph[b] .
  • Staying in place is not a move. The cat's legal moves are the neighbors of its node other than 0 .
  • The cat wins as soon as both players are on the same node, whichever of them moved there. A mouse that steps onto the cat's node loses.
  • Optimal play means each player forces a win when it can, and otherwise forces a draw when it can.

Constraints

  • 3 <= len(graph) <= 50
  • 1 <= len(graph[i]) < len(graph)
  • 0 <= graph[i][j] < len(graph)
  • graph[i][j] != i
  • The values in each graph[i] are distinct.
  • The mouse and the cat can always move.

Examples

Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output: 0

The edges are 0-2, 0-5, 1-3, 2-4, 2-5, 3-4 and 3-5. The mouse must first move to 3, and if the cat answers with 4 the mouse escapes through 5, so the cat goes to 5 to guard the hole. After that the mouse's only safe move is to 4, the cat must reply with 2 (moving to 3 would let the mouse reach the hole through 2), and the mouse returns to 3. That repeats the position after the mouse's first move, and every move in this cycle is the only one that does not lose, so the game is a draw.

Input: graph = [[1,3],[0],[3],[0,2]]
Output: 1

Node 1 is adjacent to the hole, so the mouse moves to node 0 on its first turn and wins.

Input: graph = [[3],[3],[3],[0,1,2]]
Output: 2

The mouse's only move is to node 3, which is adjacent to the cat. The cat then moves onto node 3 and catches the mouse.