The Delivering Boxes from Storage to Ports interview question is a complex optimization problem. You have a truck with weight and box-count limits and a sequence of boxes destined for different ports. You must deliver them in the given order, making multiple trips if necessary. Each trip starts and ends at the storage. A trip to a port costs 1, and moving between different ports costs 1. You need to minimize the total number of trips (trips between locations).
Companies like Nutanix ask this Delivering Boxes from Storage to Ports coding problem to test a candidate's mastery of Dynamic Programming and optimization techniques. It's a "Hard" problem because a standard DP will time out. Candidates must use a Sliding Window or Monotonic Queue to optimize the transitions to .
This problem uses Dynamic Programming with Monotonic Queue Optimization.
dp[i] is the min trips to deliver the first i boxes.dp[i] = min(dp[j] + cost_to_deliver_from_j_to_i) where the window [j, i] satisfies truck limits.j indices that potentially minimize the DP expression.Boxes for ports: [1, 1, 2]. Truck limits: max 2 boxes, max 10kg.
[1, 1]. Cost: Storage -> Port 1 (1) -> Storage (1) = 2.[2]. Cost: Storage -> Port 2 (1) -> Storage (1) = 2.
Total: 4.
Alternatively, if the truck could take all 3: Storage -> Port 1 (1) -> Port 2 (1) -> Storage (1) = 3.j from 0 to i-1, resulting in which is too slow for .This is an advanced variation of the "Sliding Window Maximum" pattern. If you see a DP where the transition depends on a range that "slides" forward, look into Monotonic Queue Optimization to bring the complexity down from quadratic to linear.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Jump Game VI | Medium | Solve | |
| Constrained Subsequence Sum | Hard | Solve | |
| Longest Increasing Subsequence II | Hard | Solve | |
| Maximum Number of Robots Within Budget | Hard | Solve | |
| Shortest Subarray with Sum at Least K | Hard | Solve |