DSA interview question
A fire starts at one node of a binary tree, and every second it spreads from each burning node to its parent and both children. How many seconds until the whole tree is burning? That's the largest distance from the start node to any other node, since fire travels along every edge at the same speed. Record parent pointers, then BFS from the start node counting levels until every node has been reached.
Example: tree [1, 2, 3, 4, 5, 6, 7], fire starts at 2 → 3 seconds (the last nodes to burn are 6 and 7).
int minTimeToBurn(TreeNode* root, TreeNode* start);
Input (tree, start) | Output | Explanation |
|---|---|---|
([1,2,3,4,5,null,6,null,null,7,8,null,9,null,null,null,null,null,10], 8) | 7 | 8-5-2-1-3-6-9-10 |
([1, 2, 3, 4, 5, 6, 7], 2) | 3 | 2-1-3-6 |
([1, 2, 3, 4, 5, 6, 7], 1) | 2 | From the root: its height |
([1, 2, null, 3, null, 4], 3) | 2 | Up to 1 takes 2 seconds, down to 4 takes 1 |
([1], 1) | 0 |
(Confirm: fire spreads to the parent and children each second; values are unique; the start node is in the tree.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in