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?
- A. 65
- B. 80
- C. 90
- 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
- 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…
- Worker i has the cost C[i][j] of doing job j, with C = [[9,2,7,8],[6,4,3,7],[5,8,1,8],[7,6,9,4]]. Each worker takes exactly one distinct…
- How many subsets of {1, 2, 3, ..., 12} have XOR equal to 0? The empty subset counts, because its XOR is 0.
- How many binary strings of length 10 contain no two adjacent 1s?
- In how many ways can a 3 x 4 grid be covered completely by non-overlapping 1 x 2 dominoes (each domino covers two adjacent cells, in…
- The universe {0, ..., 5} must be covered by a union of chosen sets from {0,1,2}, {2,3}, {3,4,5}, {0,4}, {1,5}. What is the minimum number…