← Questions
Time for an infection to spread through a binary tree
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Tesla's second technical round, last asked Jan 2026. It tests trees, bfs and graphs.
A binary tree catches fire at one node at minute 0. Every minute, the fire spreads from each burning node to its left child, its right child and its parent. Return how many minutes it takes until every node is burning.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
burn_time(tree: list[int | None], target: int) -> int—treeis in level order: the node at indexihas its children at2*i + 1and2*i + 2, andNonemarks a missing node.targetis the value of the node that starts burning.
Example
burn_time([1, 2, 3, 4, 5, None, 6], 5) -> 4 # farthest node from 5 is 6: 5 -> 2 -> 1 -> 3 -> 6
burn_time([7], 7) -> 0 # the only node is already burning
Constraints
-
1 <= len(tree) <= 10^5,tree[0]is notNone. - Node values are unique, and
targetis in the tree. - The tree may be unbalanced.
Clarifying questions
- Is the tree given as nodes with pointers or as a level-order list?
- Are values unique, and is the target guaranteed to exist?
- Does the clock start at 0 when the target catches fire?
What a strong answer covers
- Treat the tree as an undirected graph: add parent links (or an adjacency list) in one pass.
- BFS from the target, one level per minute; the answer is the number of levels minus one, the target's farthest distance.
- O(n) time and space; an iterative traversal so a skewed tree does not overflow the stack.
- The alternative single DFS that computes the answer from subtree depths without parent links.
- Tests: single node, target at the root, target at a leaf, a long chain.
Follow-ups
- Solve it in one DFS with no extra parent map.
- Return the nodes that catch fire at each minute.
- Several nodes start burning at once.
- Fire takes a different number of minutes to cross each edge.
Where it's asked
- Tesla · Second technical round · Last asked Jan 202645 min · Coding, often combined with team fit
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
