Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: n ≤ 18 cities, find the shortest route visiting every city exactly once (travelling salesman). Which approach fits?

  1. A. Try all permutations – O(n!) ≈ 6.4 × 10¹⁵
  2. B. Greedy nearest neighbour – always optimal
  3. C. Bitmask DP over (visited set, last city) – O(2ⁿ · n²) ≈ 8.5 × 10⁷
  4. D. Dijkstra from the start city – O(E log V)

Correct answer: C. Bitmask DP over (visited set, last city) – O(2ⁿ · n²) ≈ 8.5 × 10⁷

Held–Karp stores the best cost for each subset ending at each city: 2¹⁸ × 18 states with 18 transitions each. Permutations explode, nearest-neighbour is only a heuristic, and Dijkstra solves a different problem (it does not force visiting all cities).

More Coding Interview Problems questions

Exams that ask this topic