cswork / libraryOpen practice workspace

Interview questions · LinkedIn

Find k closest values in a BST

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Find k closest values in a BST states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a binary search tree with n nodes and a real target t, return k node values whose distances to t are smallest. Implement an algorithm with O(log n + k) expected time and O(h) extra space (h is tree height), using predecessor and successor iterators (e.g., two in-order stacks) or a recursive approach. Specify tie-breaking rules, handle k > n and duplicate values, and analyze correctness and complexity. Discuss how you would adapt the solution if the structure were a general binary tree (not a BST).

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

Given a binary search tree with n nodes and a real target t, return k node values whose distances to t are smallest. Implement an algorithm with O(log n + k) expected time and O(h) extra space (h is tree height), using predecessor and successor iterators (e.g., two in-order stacks) or a recursive approach. Specify tie-breaking rules, handle k > n and duplicate values, and analyze correctness and complexity. Discuss how you would adapt the solution if the structure were a general binary tree (not a BST).

Recommended Approach

Choose traversal based on the required output. DFS is natural for subtree computations, reconstruction, and range pruning; BFS is natural for level order or side views. Keep per-depth or per-position state when the output depends on columns, rows, or depths.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Most tree traversals are O(n) time and O(h) recursion stack for DFS or O(w) queue space for BFS, where h is height and w is maximum width.

Edge Cases and Tests

Empty tree, one node, skewed tree, duplicate values when reconstruction assumes uniqueness, deep recursion, and tie-breaking for same row/column nodes.

Community answers

Nothing here yet.

Join in on CSWork →