List Every Warehouse Exactly K Links From a Failed Node

Last updated: November 29, 2026

Quick Overview

Warehouses are connected as a binary tree of network links. Given a pointer to a node that just failed and an integer k, return the values of all nodes exactly k links away from it, in any order.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Warehouses are connected as a binary tree of network links. Given a pointer to a node that just failed and an integer k, return the values of all nodes exactly k links away from it, in any order.

Amazon onsite interviewers use this to see whether you realize a tree is just an undirected graph once you can walk to parents. It maps neatly to blast-radius analysis when one site in a logistics network goes down.

What the Interviewer Expects
  • Notice that you must move upward as well as downward
  • Build parent pointers or an adjacency list in one pass
  • Run BFS from the target for exactly k layers with a visited set
  • Handle k = 0 and k larger than the tree height
  • Analyze O(n) time and space
Key Topics to Cover
binary trees
BFS
parent pointers
graph conversion
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
  • How would you answer many (target, k) queries quickly on the same tree?
  • Can you solve it without extra parent storage using a single DFS that returns distances?
  • How would you return the nodes grouped by distance 0 through k?
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

Inputs: tree root, a target node that exists in the tree, and k >= 0. Output: list of node values at distance exactly k (edges counted). Node values are unique....

High-Level Design

First DFS from the root to record each node's parent. Now every node has up to three neighbors: left, right and parent. BFS outward from the target, expanding one full layer per step and marking visit...


Submit Your Answer
Markdown supported