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
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
binary trees
breadth-first search
parent pointers
tree as graph
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported