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

Four towns have road distances M = [[0,10,15,20],[10,0,35,25],[15,35,0,30],[20,25,30,0]] (symmetric). A salesperson starts at town 0, visits every town exactly once and returns to town 0. Using bitmask DP over visited sets, what is the minimum total distance?

  1. A. 65
  2. B. 80
  3. C. 90
  4. D. 85

Correct answer: B. 80

dp[mask][v] is the cheapest walk from town 0 covering mask and ending at v. The cycle must close with the edge back to 0; the route 0-1-3-2-0 costs 10+25+30+15 = 80, which is the minimum. Leaving out the return edge gives the cheaper open path 65.

More Coding Interview Problems questions

Exams that ask this topic