PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Solve deleting a node from a binary search tree while preserving BST ordering. Covers missing keys, leaf and one-child deletion, two-child replacement with inorder successor, Python code, correctness, and complexity.

  • hard
  • Google
  • Coding & Algorithms
  • Software Engineer

Delete a Node From a Search Tree

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given the root of a binary search tree and an integer `key`, delete the node whose value equals `key` if it exists. Return the possibly new root while preserving the binary search tree property. A binary search tree has this property: for every node, all values in its left subtree are smaller than the node value, and all values in its right subtree are larger. Example: ```text Input: 5 / \ 3 6 / \ \ 2 4 7 key = 3 One valid output: 5 / \ 4 6 / \ 2 7 ``` ### Constraints & Assumptions - Values are unique unless the interviewer states a duplicate policy. - Return the original root if `key` is not found. - The node to delete may be a leaf, have one child, have two children, or be the root. - An iterative or recursive solution is acceptable. - Aim for `O(h)` time where `h` is the tree height. ### Clarifying Questions to Ask - Are duplicate values possible, and if so where are duplicates stored? - Should we use the inorder successor, inorder predecessor, or either for the two-child case? - Is the tree balanced, or can it be skewed? - Should the function mutate the existing tree or build a new tree? ### What a Strong Answer Covers - Search for the node using BST ordering. - Correct handling of missing key. - Leaf deletion. - One-child deletion by returning the child. - Two-child deletion by replacing with inorder successor or predecessor. - Correct root replacement. - Time and space complexity. ### Follow-up Questions - How would the complexity change for a balanced BST versus a skewed BST? - How would you write the solution iteratively? - How would you handle duplicate keys? - How would deletion work in a self-balancing tree such as AVL or Red-Black tree?

Quick Answer: Solve deleting a node from a binary search tree while preserving BST ordering. Covers missing keys, leaf and one-child deletion, two-child replacement with inorder successor, Python code, correctness, and complexity.

Given BST values in insertion order and a key, delete the key and return the inorder traversal of the resulting BST.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([5,3,6,2,4,7], 3)

Expected Output: [2, 4, 5, 6, 7]

Explanation: Prompt example returns sorted remaining values.

Input: ([5,3,6], 9)

Expected Output: [3, 5, 6]

Explanation: Missing key.

Input: ([1], 1)

Expected Output: []

Explanation: Delete root leaf.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)