Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: A binary search tree is built from the values 15, 10, 20, 8, 12, 17, 25 inserted in that order. What is the number of leaves (nodes with no children)?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
The leaves of this BST are 8, 12, 17 and 25. Counting nodes with exactly one child would give a different number.
More Coding Interview Problems questions
- Problem: A stack-based simulation begins with an empty stack. Operations in order: push 4, push 7, push 2, pop, push 9, pop, push 5. What…
- Problem: A tree on 5 nodes is given by edges 1-2, 2-3, 2-4, 4-5. The diameter (in edges) of this tree is what value?
- Problem: A sequence is defined by s(1) = 1, s(2) = 2, and s(n) = s(n−1) + s(n−2) for n ≥ 3. What is the remainder when s(12) is divided by…
- Problem: How many subsets of {1, 2, 3, 4, 5, 6} contain exactly two odd numbers?
- Problem: The product of the first 6 positive odd numbers 1·3·5·7·9·11 is computed. What is that product's value modulo 100?
- Problem: A cyclic array [1, 2, 3, 4, 5] is rotated so that the element at index 0 becomes the element at index 2 (rotate left by 2). What…