Maximum Survival Time in a Graph Pursuit
Reported by candidates from Zip's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this Zip question hinges on is a plain adjacency list plus a queue. Reported in September 2026, it dresses up as a cat-and-mouse chase, but it's two BFS runs and a comparison. You've got an OA coming and this one looks scarier than it is. Build the graph, run BFS from Tom, run BFS from Jerry, then scan every room. Keep the ones where Jerry's distance is no greater than Tom's and take the max Jerry distance. If you blank on the setup, StealthCoder runs invisibly during the live OA as a safety net.
The problem
An undirected connected graph has roomCount rooms numbered from 0 through roomCount - 1. Tom starts in tomStart and Jerry starts in jerryStart. For this exercise, a room is reachable safely by Jerry when Jerry's shortest-path distance to that room is no greater than Tom's shortest-path distance. Return the greatest Jerry distance among all such rooms. This is the maximum number of whole escape moves supported by the reported distance comparison. Function maxEscapeSeconds(roomCount: int, edges: int[][], tomStart: int, jerryStart: int) → int Examples Example 1 roomCount = 4 edges = [[0,1],[1,2],[2,3]] tomStart = 0 jerryStart = 2 return = 1 Jerry can safely reach room 3 in one move; farther safe progress is impossible. Example 2 roomCount = 4 edges = [[0,1],[0,2],[0,3]] tomStart = 0 jerryStart = 1 return = 0 Every other leaf takes Jerry two steps but Tom one, so only Jerry's start is safe. Example 3 roomCount = 6 edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[2,5]] tomStart = 0 jerryStart = 4 return = 2 Jerry can move two edges to a room reached no later than Tom. Constraints 2 <= roomCount <= 10^5. The graph is simple, undirected, and connected. tomStart != jerryStart.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to ignore the story. The problem text defines safe rooms exactly: Jerry's shortest distance is less than or equal to Tom's. No game theory, no simulation. Edges are unweighted, so BFS gives shortest paths in O(V+E), which handles 10^5 rooms easily. Run it twice with a distance array initialized to -1. Then loop over all rooms, check distJerry[r] <= distTom[r], and track the max of distJerry[r]. Pitfalls: building the adjacency list wrong with undirected edges (add both directions), using recursion DFS and blowing the stack at 10^5, and using strict less-than instead of less-than-or-equal. Check Example 2: leaves are two steps for Jerry and one for Tom, so the answer is 0. Jerry's own start always qualifies, so the answer is never negative. If the live OA freezes you, StealthCoder can hand you the two-BFS skeleton fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Survival Time in a Graph Pursuit cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Zip's OA.
Zip reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Survival Time in a Graph Pursuit FAQ
How hard is the Zip max survival time question really?+
Easier than it reads. It's a standard BFS problem wrapped in a story. If you can write BFS on an adjacency list and loop over the results, you can solve it. Most of the difficulty is trusting the problem's own definition of a safe room instead of overthinking the chase.
What's the trick to this problem?+
Run BFS from both Tom and Jerry to get two distance arrays. A room counts when Jerry's distance is less than or equal to Tom's. Return the largest Jerry distance among those rooms. No simulation of turns is needed because the statement defines the comparison for you.
Why BFS and not Dijkstra or DFS?+
Edges are unweighted, so BFS gives exact shortest paths in linear time. Dijkstra works but adds a log factor for nothing. DFS doesn't give shortest paths in a general graph, and recursive DFS risks a stack overflow with 10^5 rooms.
What edge cases should I test?+
Test Example 2, where every room except Jerry's start is unsafe and the answer is 0. Test a cycle like Example 3, where two paths exist. Also confirm you use less-than-or-equal, since ties go to Jerry. Tom and Jerry never start in the same room.
How do I prepare for this in 48 hours?+
Write multi-source-free BFS on an adjacency list from memory until it takes five minutes. Practice returning a distance array with -1 for unvisited. Then do two or three graph distance comparison problems. That covers this question and most graph OAs.