Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE
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?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Two BFS passes: the farthest node from 1 is 5 (distance 3), and the farthest from 5 is 3 at distance 3 as well, so the diameter is three edges.
More Coding Interview Problems questions
- 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…
- Problem: Starting from 5 on a number line, you may jump +2 or +3 each turn and must land exactly on 25. How many distinct jump sequences…
- Problem: A rectangle of 12 by 18 units is cut into the largest possible identical squares that tile it exactly. How many such squares are…