Minutes to Infect Tree
Reported by candidates from SavantLabs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SavantLabs OA reported in January 2026 dresses up a plain graph question as an outbreak story. Strip the story and it's one thing: how far is the farthest city from start? That's BFS on an undirected tree. The only annoying part is the input. Edges come as strings like "1->5", so you parse them yourself and treat each one as two-way. If you blank on the parsing or the traversal during the assessment, StealthCoder is the invisible backup that reads the problem and hands you a working solution.
The problem
You are given a tree of cities. Each city has an integer name. The tree edges are given as strings in the format "u->v", meaning city u is connected to city v. At minute 0, an infection starts at city start. Each minute, every currently infected city infects all adjacent uninfected cities. Return the number of minutes needed for the entire tree to become infected. Function minutesToInfectTree(graph: String[], start: int) → int Examples Example 1 graph = ["1->5", "1->3", "5->4", "4->9", "4->2", "3->10", "3->6"] start = 3 return = 4 The farthest cities from 3 are 9 and 2, each at distance 4. Therefore all cities are infected after 4 minutes. Example 2 graph = ["1->2", "2->3", "3->4"] start = 2 return = 2 City 1 is infected after 1 minute, city 3 after 1 minute, and city 4 after 2 minutes. Constraints The given edges form a tree. Each edge string has the format "u->v". start is a city in the tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The reduction: infection spreads one edge per minute, so the answer is the maximum BFS depth from start. Parse each string by splitting on "->", convert both sides to ints, and add the edge to an adjacency map in both directions. The classic pitfall is treating the edges as directed because of the arrow. Example 1 breaks if you do that, since start 3 has to reach city 1 and go up through 5 and 4. Run BFS from start with a visited set (or parent tracking, since it's a tree), count levels, and return the last level index. Don't return the number of nodes or the tree height from the root. Edge case: a single edge or start at a leaf still works with the same code. Complexity is O(n) time and space. If the parsing or level counting trips you up live, StealthCoder is the safety net that gives you the full solution.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minutes to Infect Tree 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as amount of time for binary tree to be infected. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SavantLabs's OA.
SavantLabs reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minutes to Infect Tree FAQ
What's the trick in Minutes to Infect Tree?+
It's the farthest node from start. Infection moves one edge per minute, so the answer equals the maximum BFS distance from start. Build an undirected adjacency map, run BFS level by level, and return the number of levels minus one, or track depth directly.
Are the edges directed because of the arrow?+
No. The statement says city u is connected to city v, and infection spreads to all adjacent cities. Add both u to v and v to u. Example 1 proves it, since from city 3 the infection has to travel up to city 1 and then down through 5 and 4.
How hard is this one really?+
Easy to medium. The algorithm is a standard BFS. Most of the risk is string parsing and forgetting the undirected edges. If you've written BFS on an adjacency list before, you can finish it in a few minutes.
Can I use DFS instead of BFS?+
Yes. Since it's a tree, a DFS from start that tracks depth and the parent gives the same maximum distance. BFS is the more natural fit because each level equals one minute, and it avoids recursion depth problems on a long chain.
How do I prepare for this in 48 hours?+
Write BFS on an adjacency map from scratch twice. Practice parsing strings with split on "->". Then test both examples by hand, plus a chain and a start at a leaf. Know that the answer is max distance and you're set for this SavantLabs-style question.