Data Structures & AlgorithmsVery hardIBM · Deloitte pattern
Keys 4, 49, 97, 15, 7, 96, 24, 50 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from the root, branching toward whichever side contains both values), what is the lowest common ancestor of 15 and 97?
- A. 15
- B. 49
- C. 97
- D. 4
Correct answer: B. 49
Starting at the root, move left while both values are smaller, move right while both are larger, and stop at the first node that splits the two (or equals one of them) — that node is the LCA. For 15 and 97 in this tree, that gives 49. The root (4) is only the LCA if the values fall on opposite sides of it, which may not hold here.
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
- Keys 38, 28, 18 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- An undirected graph has edges (u–v: weight) A–C:16, B–C:30, C–D:17, A–D:2, A–B:3, B–D:12. What is the weight of its minimum spanning tree?
- Undirected graph edges: C–D, D–B, C–B, A–C, A–B, A–D. Using a BFS 2-colouring, is this graph bipartite?
- A disjoint-set (union-find with union by rank and path compression) over 7 elements (0 to 6), each initially its own component, runs this…
- Keys 5, 22, 60, 78, 66, 47, 79, 17, 35 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from…
- Keys 41, 51, 61 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…