Data Structures & AlgorithmsHardProduct-company MCQ pattern
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 the root, branching toward whichever side contains both values), what is the lowest common ancestor of 60 and 17?
- A. 5
- B. 17
- C. 60
- D. 22
Correct answer: D. 22
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 60 and 17 in this tree, that gives 22. The root (5) 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 41, 51, 61 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- 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…
- Build a binary min-heap in place from the array [50, 42, 62, 89, 76, 45, 3, 82] using bottom-up heapify (sift-down from the last internal…
- A directed graph has edges C→B, B→D, A→D, C→A, C→D, A→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle)…
- Directed edges: A→B (6), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- A directed graph has edges D→B, C→B, B→A, D→C, B→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…