InterviewStack.io LogoInterviewStack.io

Binary Trees and Binary Search Trees Questions

Focuses on tree data structures, specifically binary trees and binary search trees. Candidates should understand node relationships, common traversals including in order, pre order, post order, and level order, and be able to implement traversals both recursively and iteratively. Cover binary search tree properties and operations including search, insertion, deletion, validation of binary search tree property, and finding the lowest common ancestor. Include problems on tree paths, height and balance calculations, serialization and deserialization, checking and restoring balance at a high level, and use cases in system design. Emphasize complexity analysis, recursion versus iterative solutions using stacks or queues, and handling edge cases such as duplicate keys and degenerate trees.

HardTechnical
59 practiced
Design and implement delete(key) for a BST where duplicate keys are allowed and represented by a 'count' integer stored at each node. Deletion should decrement count if greater than 1 and remove the node only when count reaches zero. Provide Python pseudocode, explain how search and traversal must adapt to counts, and discuss maintaining subtree sizes for order-statistics.
MediumTechnical
62 practiced
Implement level-order traversal that returns nodes grouped by level and also computes the maximum width (largest number of nodes at any level). Write Python function level_order_with_width(root) that returns (levels, max_width). Discuss memory versus CPU trade-offs for extremely wide levels and strategies to handle very large output.
HardTechnical
79 practiced
Two nodes of a BST were swapped by mistake. Design and implement an algorithm in Python to recover the BST by swapping them back without changing the tree structure. Aim for O(n) time and O(h) space (or O(1) if using Morris traversal). Explain how to detect swapped nodes by scanning the inorder sequence and give code sketch for fixing them.
HardSystem Design
62 practiced
Design a concurrent in-memory BST that supports concurrent search, insert, and delete. Compare lock-based (coarse-grained and fine-grained) and lock-free approaches in terms of implementation complexity, throughput, and consistency guarantees. Discuss how such choices affect an AI model parameter server that sees millions of reads per second and moderate writes.
HardTechnical
46 practiced
Implement an algorithm to find the maximum path sum in a binary tree where a path may start and end at any nodes (not necessarily root-to-leaf). Return both the maximum sum and the nodes along that path. Provide a Python implementation approach, explain why the naive O(n^2) approach can be improved to O(n), and handle cases with all negative values.

Unlock Full Question Bank

Get access to hundreds of Binary Trees and Binary Search Trees interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.