Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
Problem: A tree has nodes 1–6 with weighted edges 1-2 (3), 1-3 (5), 2-4 (4), 2-5 (2), 3-6 (7). What is the weighted diameter (maximum over all pairs of the sum of edge weights on the path between them)?
- A. 12
- B. 19
- C. 4
- D. 21
Correct answer: B. 19
Two DFS passes (farthest node from any node, then farthest from that) give the path 4 → 2 → 1 → 3 → 6 = 4 + 3 + 5 + 7 = 19. The farthest distance from node 1 alone is 12, the unweighted diameter is 4 edges, and 21 sums every edge.
More Coding Interview Problems questions
- Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in…
- Problem: Insert the words "car", "cat", "care", "dog", "do", "dot" into an empty trie (one node per character, root has no letter). How…
- Problem: Count the occurrences of the pattern "aba" in "abababa", where occurrences may overlap.
- Problem: The words "listen", "silent", "enlist", "google", "gogole", "cat", "act", "tac", "dog" are grouped by anagram class (words with…
- Problem: In the binary string "1101001110100110", what is the length of the longest contiguous substring containing equal numbers of 0s…
- Problem: For the string "abcab", count the subsequences of length 3 that are palindromes (choose 3 indices i < j < k with s[i] = s[k]; the…