Compute time to burn tree
Company: Tesla
Role: Backend Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates proficiency with tree algorithms, distance computation, and propagation modeling, testing the ability to reason about how a process spreads across parent-child relationships in a tree.
Read the full Tesla Backend Engineer interview experience this question came from
Constraints
- `1 <= len(tree) <= 10^5`
- `tree[0]` is not `None`
- Each entry in `tree` is either an integer or `None`
- All non-`None` node values are unique
- `target` is guaranteed to be one of the non-`None` values in `tree`
Examples
Input: ([1, 2, 3, 4, 5, None, 6], 5)
Expected Output: 4
Explanation: The longest path from node 5 is 5 -> 2 -> 1 -> 3 -> 6, which has 4 edges, so the tree finishes burning in 4 minutes.
Input: ([7], 7)
Expected Output: 0
Explanation: There is only one node, and it is already burning at time 0.
Hints
- If you can move from a node to its parent as well as its children, the tree behaves like an undirected graph.
- Build the parent/adjacency relationships first, then run BFS from the target one minute at a time to find the farthest reachable node.