Find All Services Exactly K Hops From a Failing Node
Last updated: November 29, 2026
Quick Overview
An Azure service dependency topology is stored as a binary tree. Given a reference to a failing node and an integer k, return every node exactly k edges away, moving up or down.
Microsoft
November 29, 20260
0
0 solved
An Azure service dependency topology is stored as a binary tree. Given a reference to a failing node and an integer k, return every node exactly k edges away, moving up or down.
A tree-as-graph question in the style of Microsoft onsite loops, motivated by blast-radius analysis during incidents. Interviewers want the candidate to realize that parent links turn the tree into an undirected graph and that a visited set prevents walking back.
What the Interviewer Expects
- Recognize that distance can go through ancestors
- Record parent pointers in one traversal
- BFS outward k levels from the target
- Use a visited set so you never step back
- Handle k = 0 and k larger than the tree
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Do it without storing parent pointers, using one DFS that returns distance to target.
- Return nodes within distance k rather than exactly k.
- The topology is a general graph with cycles. What changes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: root, a target node in the tree, and k >= 0.
- Distance counts edges in either direction.
- Output: values at exactly distance k (sorted here for determinism)....
High-Level Design
Children are easy to reach, but nodes in other branches are reached by going up. Build a parent map with one DFS, so each node has up to three neighbors: left, right, parent. Then BFS from the targe...