Data Structures & AlgorithmsHardProduct-company MCQ pattern
Keys 1, 2, 3, 4, 5, 6, 7 are inserted in that order into an empty AVL tree. How many rotations (counting a double rotation as one rebalancing) are performed, and what is the level order of the final tree?
- A. 4 rebalancings; 4 2 6 1 3 5 7
- B. 6 rebalancings; 4 2 6 1 3 5 7
- C. 4 rebalancings; 2 1 4 3 6 5 7
- D. 3 rebalancings; 4 2 6 1 3 5 7
Correct answer: A. 4 rebalancings; 4 2 6 1 3 5 7
Single left rotations happen after inserting 3 (at 1), 5 (at 3), 6 (at the root 2) and 7 (at 5), four in all, and the tree ends perfectly balanced: level order 4 2 6 1 3 5 7. A plain BST with these inserts would be a chain of height 6.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is the maximum possible height of an AVL tree with 20 nodes (a single node has height 0)?
- The words car, cart, care, cat, dog are inserted into an empty trie (one node per character, shared prefixes share nodes). How many nodes…
- How many calls in total (including the first) does C(5, 2) make? c int C(int n, int k) { if (k == 0 || k == n) return 1; return C(n - 1, k…
- For n = 6, how many disk moves does this Tower of Hanoi function count, and how many calls of hanoi are made in total? c int hanoi(int n…
- A robot moves only right or down on a 3 × 4 grid of cells (3 rows, 4 columns) from the top-left cell to the bottom-right cell. The cell in…
- What is the length of the longest palindromic SUBSEQUENCE of CHARACTER?