Drone Circular Route — Minimum Total Travel Cost
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question tests a candidate's ability to apply dynamic programming to combinatorial optimization, specifically the Traveling Salesman Problem (TSP) with bitmask DP. It evaluates practical algorithm design for asymmetric weighted graphs with up to 15 nodes, a classic pattern in software engineering interviews assessing graph traversal and optimization under exponential state spaces.
Constraints
- 1 <= n <= 15
- 0 <= cost[i][j] <= 10^4
- cost[i][i] == 0 for all i
- The cost matrix may be asymmetric: cost[i][j] need not equal cost[j][i]
- The answer fits in a 32-bit signed integer
Examples
Input: (3, [[0, 10, 15], [20, 0, 35], [25, 30, 0]])
Expected Output: 65
Explanation: Tour 0 -> 2 -> 1 -> 0 = 15 + 30 + 20 = 65, beating 0 -> 1 -> 2 -> 0 = 70.
Input: (4, [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
Expected Output: 80
Explanation: Example 2 from the prompt; the optimal symmetric-ish tour costs 80.
Input: (1, [[0]])
Expected Output: 0
Explanation: Single hub: the drone is already home, so the cost is 0.
Input: (2, [[0, 5], [8, 0]])
Expected Output: 13
Explanation: Only one tour: 0 -> 1 -> 0 = 5 + 8 = 13. The asymmetry (5 out, 8 back) both edges count.
Input: (4, [[0, 3, 1, 5], [2, 0, 7, 4], [6, 8, 0, 9], [3, 2, 6, 0]])
Expected Output: 14
Explanation: Best asymmetric tour is 0 -> 2 -> 3 -> 1 -> 0 = 1 + 9 + 2 + 2 = 14.
Input: (3, [[0, 100, 1], [1, 0, 100], [100, 1, 0]])
Expected Output: 3
Explanation: Direction matters: 0 -> 2 -> 1 -> 0 = 1 + 1 + 1 = 3, while the reverse 0 -> 1 -> 2 -> 0 = 300. This is the wrap-around / asymmetry trap.
Hints
- n <= 15 is the giveaway: 2^15 subsets is small enough for an exponential-in-n bitmask DP (Held-Karp), but n! brute force over permutations blows up.
- Let dp[mask][i] be the minimum cost of a path that starts at hub 0, visits exactly the hubs in `mask`, and currently sits at hub i. Initialize dp[{0}][0] = 0.
- Because the costs are asymmetric, you must respect direction: a transition into hub j adds cost[i][j], not cost[j][i].
- Don't forget the closing leg. The answer is min over i of dp[fullMask][i] + cost[i][0] — the original interview report specifically warns that the tail-to-head return edge is the easy thing to miss.
- Handle the degenerate n == 1 case (a single hub) separately: the cost is 0.