Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: For the path graph P5 on vertices 1–5 (edges 1-2, 2-3, 3-4, 4-5), what is the sum of the distances over all unordered pairs of vertices (the Wiener index)?
- A. 40
- B. 4
- C. 10
- D. 20
Correct answer: D. 20
Distances 1, 2, 3, 4 occur 4, 3, 2, 1 times: 4 + 6 + 6 + 4 = 20. Not halving the ordered sum gives 40, summing only edges gives 4, and 10 is just the number of pairs.
More Coding Interview Problems questions
- Problem: The string "abcdabcd" is examined for windows of length 4 with all four characters distinct. How many such windows are there?
- Problem: What is the minimum number of characters to delete from "programming" so that all remaining characters are distinct (keep one…
- Problem: Compute the longest palindromic prefix of "abacabacaba" (the longest prefix that reads the same backwards). What is its length?
- Problem: What is the length of the longest common substring (contiguous) of "abcdxyz" and "xyzabcd"?
- Problem: From the array [3, 2, 7, 10], choose a subset with no two adjacent indices chosen, maximising the sum. What is the maximum sum?
- Problem: The array [5, 1, 6, 2, 7] must be made strictly increasing by deleting the fewest elements. What is the minimum number of…