Find All Drivers Exactly K Hops From an Incident Node

Last updated: November 29, 2026

Quick Overview

Given a binary tree of drivers linked by a support hierarchy, a target node, and an integer k, return the values of all nodes exactly k edges away from the target, moving up or down the tree.

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

0

0

0 solved


Given a binary tree of drivers linked by a support hierarchy, a target node, and an integer k, return the values of all nodes exactly k edges away from the target, moving up or down the tree.

Shows up in Uber onsite coding rounds as a tree problem that secretly becomes a graph problem. Interviewers check whether you notice that you need to move upward and how you avoid revisiting nodes.

What the Interviewer Expects
  • Recognizes that distance can go through the parent, not only down
  • Builds a parent map with one DFS, then runs BFS from the target
  • Uses a visited set so nodes are not counted twice
  • Stops BFS at depth k
  • States O(n) time and space
Key Topics to Cover
binary tree
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
  • Can you do it without a parent map, using a single recursion that returns distance to target?
  • Return nodes within distance k rather than exactly k.
  • How would this change on a general graph of driver teams?
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 reference in the tree, integer k >= 0.
  • Output: values of all nodes whose edge distance to target is exactly k, any order.
  • Tree nodes have no parent pointers....
High-Level Design

Distance in a tree is symmetric, so treat it as an undirected graph. First DFS from the root to record each node's parent. Then BFS from the target, where neighbors are left child, right child and par...


Submit Your Answer
Markdown supported