Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The cycle graph C8 has vertices 0–7 with i adjacent to (i+1) mod 8. How many unordered pairs of vertices are at distance exactly 3 along the shorter way around the cycle?

  1. A. 16
  2. B. 8
  3. C. 4
  4. D. 28

Correct answer: B. 8

Each vertex i pairs with i + 3 (and i − 3), and the shorter way is 3 because 3 < 8 − 3, so there are 8 × 2 / 2 = 8 unordered pairs. Counting ordered pairs gives 16, counting distance 4 (opposite vertices) gives 4, and 28 counts every pair.

More Coding Interview Problems questions

Exams that ask this topic