Question 71
HardAn AVL tree receives the keys 10, then 30, then 20. Which rebalancing is needed after the last insertion, and what becomes the root?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
An AVL tree receives the keys 10, then 30, then 20. Which rebalancing is needed after the last insertion, and what becomes the root?
A single left rotation; the root becomes 30
A single right rotation; the root stays 10
No rotation is needed
A right-left (double) rotation; the root becomes 20
Sign up free to see the answer and a step-by-step explanation.
The keys 10, 20, 30, 40, 50 and 25 are inserted in that order into an empty AVL tree. Which key is at the root at the end?
40
30
20
25
Sign up free to see the answer and a step-by-step explanation.
How can you find the k-th smallest element of a BST in O(h) time?
Store subtree sizes in each node and walk down comparing k with the left size
Use a heap
Sort the keys
Do a full inorder traversal
Sign up free to see the answer and a step-by-step explanation.
What is a treap?
A BST on keys that is also a heap on random priorities, giving expected O(log n) height
A B-tree
A heap of trees
A trie
Sign up free to see the answer and a step-by-step explanation.
Why do databases use B+ trees rather than binary search trees on disk?
BSTs can't store keys
Each node holds many keys, so the tree is shallow and needs few disk reads
B+ trees are binary
They don't
Sign up free to see the answer and a step-by-step explanation.
How can two BSTs be merged into one balanced BST in O(m + n)?
Rotate repeatedly
Join the roots
Insert each node of one into the other
Inorder-traverse both into sorted lists, merge them, and build a balanced tree from the result
Sign up free to see the answer and a step-by-step explanation.
What does a BST iterator using a stack achieve?
next() in amortised O(1) and O(h) memory
O(log n) per call always
O(n) memory
Random access
Sign up free to see the answer and a step-by-step explanation.
How do you check that a binary tree is a valid BST?
Count the nodes
Pass down an allowed (low, high) range and check every node lies inside it
Check only that each left child is smaller than its parent
Check the root only
Sign up free to see the answer and a step-by-step explanation.
What is the floor of 23 in a BST holding 10, 15, 20, 25, 30?
25
20
15
23
Sign up free to see the answer and a step-by-step explanation.
A BST of n keys is converted into a sorted doubly linked list in place. What is the time?
O(n)
O(n²)
O(log n)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.