Data Structures & AlgorithmsHardProduct company OA
Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 are inserted into an empty BST. A range query counts keys in [33, 62], pruning subtrees that cannot contain a key in range. What is the count, and how many nodes does the query visit (including the root)?
- A. 5 keys, 10 nodes visited
- B. 5 keys, 8 nodes visited
- C. 4 keys, 8 nodes visited
- D. 5 keys, 7 nodes visited
Correct answer: B. 5 keys, 8 nodes visited
In range: 35, 40, 45, 50, 60 → 5 keys. Visits: 50 (go both ways), 30 (30 < 33, so skip its left subtree, i.e. 20) → 40 → 35, 45; on the right 70 (70 > 62, skip its right subtree 80) → 60 → its right child 65 (60 < 62, so it must be checked). 50, 30, 40, 35, 45, 70, 60, 65 = 8 visits. Visiting every node is 10.
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
- A robot moves only right or down from the top-left to the bottom-right cell of this 4 × 4 grid ('#' is blocked). How many paths are there?…
- DFS-based topological sort on the DAG with adjacency lists a: [b, c], b: [d], c: [d, e], d: [f], e: [f], f: [] starts at a, visits…
- DFS with white/grey/black colours detects a cycle in the directed graph with adjacency lists 1: [2, 3], 2: [4], 3: [4], 4: [5], 5: [3]…
- Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its…
- What is the size of a minimum vertex cover (a smallest set of vertices touching every edge) of the tree with edges 1–2, 1–3, 2–4, 2–5…
- In the tree with edges 1–2, 1–3, 2–4, 2–5, 3–6, 5–7, the rerooting technique computes S(v) = sum of distances from v to every other node…