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.

MediumTechnical
0 practiced
Implement BST insertion in Python with support for an optional parameter 'allow_duplicates' that accepts policies: 'left', 'right', or 'count' where duplicates increment a counter stored at the node. Return the root. Discuss how this BST performs in production and when SRE should prefer balanced trees or alternate indices for reliability and latency reasons.
HardTechnical
0 practiced
Explain Morris inorder traversal which achieves O(1) extra space by temporarily modifying tree pointers. Walk through the algorithm step-by-step on a small example tree, explain how and when threaded links are added and removed, and discuss the trade-offs for production SRE (pointer mutations during traversal, thread-safety concerns, and debugging complexity).
EasyTechnical
0 practiced
Implement level-order traversal (breadth-first search) of a binary tree in Python. Return a list of lists where each inner list contains node values at that depth (level 0 = root). Use a queue; show example: input tree represented by [1,2,3,4,5,null,7] should return [[1],[2,3],[4,5,7]]. Discuss time and space complexity and how you would stream results for very deep trees to avoid high memory usage in a production SRE environment.
HardTechnical
0 practiced
Given extremely skewed insertion order (e.g., always increasing keys) that leads to worst-case BST performance, propose algorithms to keep the tree balanced with minimal runtime overhead: randomized balancing (treaps), periodic rebuild from sorted array, or switching to self-balancing trees (AVL/Red-Black). Analyze amortized cost and explain how SRE would implement an automated rehealing job to fix skewed indexes.
HardTechnical
0 practiced
Implement insertion for an AVL tree in Python including necessary rotations to maintain balance. Provide helper functions to compute node heights and balance factors, and implement left and right rotation helpers. After insertion, ensure heights are updated and tree is balanced. Discuss complexity, typical bugs, and when SRE should prefer using library-provided balanced structures.

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.