Data Structures & AlgorithmsHardICPC regional · Product Company SDE
Problem: A tree on nodes 1–9 has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 3-8, 8-9. The eccentricity of a node is its largest distance to any other node. How many nodes have the minimum eccentricity (the centre of the tree)?
- A. 2
- B. 0
- C. 3
- D. 1
Correct answer: D. 1
Compute each node's eccentricity with BFS and count the minimal values. The centre of a tree can have one or two nodes; reporting the diameter's endpoints is a different set.
More Coding Interview Problems questions
- Problem: Count the pairs of nodes at the same depth (root depth 0) that are NOT siblings (no common parent), in the tree with parent array…
- Problem: Insert the keys 50, 20, 70, 10, 30, 60, 90, 25, 35, 65 into a binary search tree in this order. How many keys in the tree lie in…
- Problem: Count the ways to segment "catsanddog" into a sequence of words, each taken from the dictionary {cat, cats, and, sand, dog}…
- Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- Problem: A binary string "1100101011" is to be changed so that it alternates (no two adjacent characters are equal). Each operation flips…
- Problem: Count the ordered pairs of words (i, j) with i ≠ j in ["abc", "cba", "bca", "abc", "cab", "xyz"] such that the second word is the…