MEDIUMasked at 8 companies

Amount of Time for Binary Tree to Be Infected

A medium-tier problem at 64% community acceptance, tagged with Hash Table, Tree, Depth-First Search. Reported in interviews at ShareChat and 7 others.

Founder's read

You're given a binary tree and a starting node. An infection spreads to adjacent nodes each minute. Find the total time until the entire tree is infected. ShareChat, PhonePe, Snap, and Goldman Sachs have all asked this one. It's a medium-difficulty problem with a 64% acceptance rate, which means most people who see it can solve it, but the trick isn't obvious on first read. The naive approach (simulate minute by minute) will TLE. If this problem hits your live assessment and you can't immediately see the graph-traversal angle, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
8
Difficulty
MEDIUM
Acceptance
64%

Companies that ask "Amount of Time for Binary Tree to Be Infected"

If this hits your live OA

Amount of Time for Binary Tree to Be Infected is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.

Get StealthCoder
What this means

The core insight: convert the tree into an undirected graph by building a parent pointer map (Hash Table), then run BFS from the infected node to find the farthest distance. The infection spreads like a multi-source BFS where every infected node simultaneously infects its neighbors each step. Most candidates fail because they try to solve it as a tree problem with only child pointers, missing that infection flows up through parents too. The trick is realizing you need to track visited nodes across all directions, not just down the tree. Once you've built the adjacency structure with parent pointers and run standard BFS, the answer is the maximum distance from the starting node. Depth-First Search (pre-order traversal to build parents) combined with Breadth-First Search (infection spread) solves it cleanly. StealthCoder hedges the moment you hit the wall on how to model parent-child infection bidirectionally.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Amount of Time for Binary Tree to Be Infected recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Amount of Time for Binary Tree to Be Infected interview FAQ

Why does the naive tree-only approach fail?+

Infection spreads in all directions from an infected node, including back to the parent. If you only track children, you'll miss branches above the starting node. You must convert the tree to an undirected graph with parent pointers so BFS can explore all neighbors equally.

Is this problem still asked at top companies?+

Yes. It's appeared across 8 companies including ShareChat, PhonePe, Snap, and Goldman Sachs. The medium difficulty and 64% acceptance rate suggest it's a standard screening question, not a rare find.

What's the time complexity after you build the parent map?+

Building parents is O(n) via DFS. BFS is O(n) since you visit each node once. Total is O(n). Space is O(n) for the hash table and queue, which is tight but acceptable for most constraints.

How do Hash Table and Tree topics connect here?+

The Hash Table stores the parent of each node (node -> parent mapping), built during tree traversal. This converts the unidirectional tree into a bidirectional graph for BFS to work correctly across all edges.

What if the starting node is a leaf?+

BFS still works. The infection spreads upward and sideways first, taking longer to reach distant subtrees on the opposite side. The farthest node determines the final time. The algorithm handles all positions automatically.

Want the actual problem statement? View "Amount of Time for Binary Tree to Be Infected" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.