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)?

  1. A. 40
  2. B. 4
  3. C. 10
  4. 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

Exams that ask this topic