Data Structures & AlgorithmsHardICPC regional · Product Company SDE
Problem: On a tree with 9 nodes labelled 1–9, edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8, 8-9, count the unordered pairs of nodes whose path has exactly 3 edges.
- A. 9
- B. 14
- C. 7
- D. 24
Correct answer: C. 7
BFS from every node (or depth bookkeeping) finds 7 pairs at distance exactly 3, e.g. (4, 3), (1, 7), (2, 6), (1, 8), (3, 9). Counting distance 2 instead gives 9, counting ordered pairs gives 14, and counting all pairs within distance 3 gives 24.
More Coding Interview Problems questions
- Problem: A string is a palindrome-partition target: what is the minimum number of cuts needed to split "abbacxcaxyyx" into palindromic…
- Problem: A word-ladder graph: vertices are words of length 3 from {cat, cot, cog, dot, dog, hat, hot} and two words are adjacent when they…
- Problem: Count the subarrays of [2, 1, 3, 2, 1, 4, 2] that have exactly 2 distinct values.
- Problem: Start at 1 and apply the rules: multiply by 3 if the number is even, add 5 if it is odd. Apply 9 steps. What is the final value?
- Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so…
- Problem: A sequence is defined by a1 = 1, a2 = 1, and an = an−1 + an−2 mod 7 for n ≥ 3. What is a30 (1-indexed)?