← All questions
MediumCoding

Closest Nodes Queries in a Binary Search Tree

Asked atAmazon
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Inorder-traverse the BST into sorted values.
  2. 02For each query, lower-bound the value.
  3. 03Use the exact value or prior element for floor.
  4. 04Use the lower-bound element for ceiling.
  5. 05Return minus one when either side is absent.

Reference answer

Then expect these follow-ups

  • When is direct BST walking preferable?

    Tests: follow-up reasoning

  • How do duplicates change the contract?

    Tests: constraint adaptation

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question