← Questions

Time for an infection to spread through a binary tree

0
TeslaCodingLast asked Jan 2026

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 — tree is in level order: the node at index i has its children at 2*i + 1 and 2*i + 2, and None marks a missing node. target is 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 not None.
  • Node values are unique, and target is 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

What it tests

trees · bfs · graphs

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