Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

The 4-cycle graph 0-1-2-3-0 (undirected, so both directions of every edge exist) is given as an adjacency matrix. How many ordered sequences (v0, v1, v2, v3) use all four vertices exactly once with consecutive vertices adjacent?

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

Correct answer: C. 8

An ordered sequence is a directed Hamiltonian path, so a path and its reversal count separately. The mask DP counts each ordering; halving it gives undirected paths, which is the trap. Unrestricted orderings of 4 vertices number 24, but adjacency removes most of them.

More Coding Interview Problems questions

Exams that ask this topic