Problem · Array

Minimum Emergency Deliveries

Learn this problem
MediumAmazonINTERNOA
See Amazon hiring insights

Problem 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 by tasks[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[]) → int

Examples

Example 1

maxProducts = 12tasks = [-5, 0, 10, -13, 0]return = 2

One optimal plan is to get emergency products on two days.

  1. Before the first inspection, get 7 products on emergency. After day 1, the warehouse has 2 products. The day 2 inspection is safe. After the day 3 delivery of 10 products, the warehouse has 12 products, which does not exceed max_products.
  2. After dispatching 13 products on day 4, the warehouse has -1 products. Before the day 5 inspection, get 1 emergency product so the warehouse has 0 products.

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

drafts saved locally
public int minimumEmergencyDeliveries(int maxProducts, int[] tasks) {
  // write your code here
}
maxProducts12
tasks[-5, 0, 10, -13, 0]
expected2
checking account