Minimum Emergency Deliveries
Learn this problemProblem statement
Amazon has opened a new warehouse recently. There are no products in the warehouse currently. The warehouse is under inspection for n days. The manager has to do a task on each day, where the task to be performed on the ith day is represented by the array tasks[i].
In the evening of each of the n days, any one of the following things will happen:
- If
tasks[i] > 0,tasks[i]products will be delivered to the warehouse, hence increasing the number of products in the warehouse bytasks[i]. - If
tasks[i] < 0,|tasks[i]|products will be dispatched from the warehouse, hence decreasing the number of products in the warehouse by|tasks[i]|. If the warehouse does not have enough products to be dispatched, then the manager will borrow some products and the warehouse will now have a negative number of products. - If
tasks[i] = 0, an inspector from Amazon's team will come and inspect the warehouse. If the warehouse has a negative number of products, the inspector will issue a notice to close the warehouse.
In the morning of each day, the manager of the warehouse can increase the number of products in the warehouse by getting any number of products delivered to the warehouse on an emergency. The manager knows beforehand about the details of the tasks.
The manager's job is to ensure that the warehouse does not have a negative number of products whenever the inspection takes place, and the number of products in the warehouse does not exceed max_products, the maximum capacity of the warehouse, any day, by getting an optimal number of products delivered on an emergency on some days.
Given n days and an array tasks, find the minimum number of days the manager will have to get the products. If the manager cannot do their job, return -1.
Function
minimumEmergencyDeliveries(maxProducts: int, tasks: int[]) → intExamples
Example 1
maxProducts = 12tasks = [-5, 0, 10, -13, 0]return = 2One optimal plan is to get emergency products on two days.
- Before the first inspection, get
7products on emergency. After day1, the warehouse has2products. The day2inspection is safe. After the day3delivery of10products, the warehouse has12products, which does not exceedmax_products. - After dispatching
13products on day4, the warehouse has-1products. Before the day5inspection, get1emergency product so the warehouse has0products.
The manager cannot do it in one emergency-delivery day, because the first inspection must be made nonnegative while still leaving enough capacity for the later 10-product delivery, and the final inspection then still needs another emergency delivery. So the answer is 2.
Source note (June 28, 2026): The original source image did not include the output for this example. FastPrep worked it out from the problem statement and the given input. If you have the original output or notice something wrong, please let us know and we'll fix it. If we find a fuller source later, we'll come back and update this too. 🐿️
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026