Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: An undirected graph on 9 vertices (0–8) has edges 0-1, 1-2, 3-4, 4-5, 5-3, 6-7. Vertex 8 is isolated. Count the unordered pairs of distinct vertices that are NOT connected to each other.

  1. A. 30
  2. B. 21
  3. C. 7
  4. D. 29

Correct answer: D. 29

Components have sizes 3, 3, 2 and 1, so connected pairs are 3 + 3 + 1 = 7 and unconnected pairs are C(9,2) − 7 = 29. Counting non-adjacent pairs (36 − 6 edges) gives 30, forgetting isolated vertex 8 gives 28 − 7 = 21, and 7 counts the connected pairs.

More Coding Interview Problems questions

Exams that ask this topic