Remove a Discontinued Product From a Price-Ordered Search Tree
Last updated: November 29, 2026
Quick Overview
Products are indexed in a binary search tree keyed by price. Given the root and a price key, delete the node with that key if present and return the new root, keeping the tree a valid BST.
Amazon
November 29, 20260
0
0 solved
Products are indexed in a binary search tree keyed by price. Given the root and a price key, delete the node with that key if present and return the new root, keeping the tree a valid BST.
A standard Amazon technical screen test of pointer manipulation: interviewers want all three deletion cases handled cleanly and the root replaced correctly. The catalog framing is delisting a discontinued item from an in-memory price index.
What the Interviewer Expects
- Search for the key using BST ordering
- Handle the leaf, one-child and two-children cases
- For two children, replace with the inorder successor (or predecessor) and delete that node
- Return the possibly new root from every recursive call
- Discuss O(h) time and balance after many deletions
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you delete without recursion?
- How do self-balancing trees like red-black or AVL keep height logarithmic after deletes?
- How would you implement lazy deletion with tombstones and when would that be better?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: BST root (unique keys) and an integer key. Output: root of the tree after removing the key; if the key is absent, return the tree unchanged. The result must still satisfy BST ordering....
High-Level Design
Recurse toward the key, assigning node.left = delete(node.left, key) or the right equivalent so links are rewired on the way back. When the node is found: if it has no left child, return its right c...