← Questions

Fewest emergency restocks to keep inventory valid

0
AmazonCodingLast asked Jan 2026

A coding interview question asked in Amazon's technical phone screen, last asked Jan 2026. It tests greedy and arrays.

A warehouse holds at most max_products items and starts empty. On day i the stock changes by task[i] at the end of the day. Before any day starts you may do an emergency restock and add any amount, as long as the stock right after the restock is at most max_products; each day you restock counts as one restock day, however much you add. Return the fewest restock days that keep every rule below, or -1 if no plan works.

  • The stock may never exceed max_products, after a restock or after a day's change.
  • On days where task[i] == 0 (stock checks), the stock at the end of the day must not be negative.
  • On other days a negative stock is allowed for the moment.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • min_restock_days(max_products: int, task: list[int]) -> int

Example

min_restock_days(10, [-3, 0, -3, 0])      ->  1
# stock is -3 after day 0; restock 13 on day 1 (up to 10), then 10, 7, 7.
# One restock covers both checks.

min_restock_days(5, [-1, 0, 5, -8, 0])    ->  2
# the +5 on day 2 caps how much can be added before it: restock 1 on day 1
# (stock 0 at the check, 5 after day 2, -3 after day 3), then 8 on day 4
# (stock 5 at the last check)

min_restock_days(3, [4])                  ->  -1   # day 0 alone exceeds the limit

Constraints

  • 0 <= len(task) <= 2 * 10^5; 1 <= max_products <= 10^9; -10^9 <= task[i] <= 10^9.
  • Use 64-bit arithmetic for the running sums.

Clarifying questions

  • Is the restock done before the day's change or after? (Before.)
  • Is a negative stock allowed on a non-check day? (Yes.)
  • What should an empty task return? (0.)

What a strong answer covers

  • Prefix sums: with total restocked amount R, the stock after day i is R + prefix[i], so the plan reduces to choosing when R rises.
  • Greedy: only raise R on a check day whose end stock would be negative, and raise it as high as is still safe, bounded by max_products - prefix[i - 1] (the restock itself) and by max_products minus the largest prefix from day i onward. If even that leaves the check negative, return -1.
  • Suffix maxima of the prefix sums precomputed, for O(n) overall.
  • Why restocking later and higher never loses to restocking earlier: an earlier restock is capped by more days.

Follow-ups

  • Return the days to restock and the amounts, not just the count.
  • Each restock costs a fixed fee plus a cost per item. Minimise the total cost.

Where it's asked

  • Amazon · Technical phone screen · Last asked Jan 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

greedy · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding