← Questions

Most days picking distinct products

0
AmazonCodingLast asked Jul 2025

A coding interview question asked in Amazon's online assessment, last asked Jul 2025. It tests greedy, sorting and binary-search.

You have several products, with quantities[i] units of product i. On day 1 you must pick 1 unit, on day 2 exactly 2 units, and in general on day d exactly d units, each from a different product that still has stock. You choose which products to use each day. Return the largest number of days you can keep this going, starting from day 1.

Interface

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

  • max_days(quantities: list[int]) -> int — returns the number of days completed.

Example

[2, 2, 3]     ->  3   # day 1: the 3; day 2: two products; day 3: all three
[1, 1, 1, 1]  ->  2   # days 1 and 2 use three products; day 3 needs three, one is left
[5, 1, 1]     ->  2   # day 3 needs three products with stock
[3, 3, 3, 3]  ->  4

Constraints

  • 1 <= len(quantities) <= 200,000, 1 <= quantities[i] <= 10^9
  • The total of all quantities is at most 200,000.
  • The answer is at most len(quantities).

Clarifying questions

  • Must the days be consecutive from day 1, with exactly d units on day d?
  • Can a day take two units from the same product? (No.)
  • Do units left over after the last day matter?

What a strong answer covers

  • The greedy: each day take one unit from the d products with the most stock left, so as many products as possible stay alive. Why taking from small piles first loses.
  • A max-heap of remaining quantities: pop d, decrement, push back the non-zero ones; stop when fewer than d products remain. The bound on the total keeps this O(S log n).
  • A faster check: binary search on the number of days D, feasible when sum(min(q, D)) >= D(D+1)/2, and why that condition is enough.
  • Tests: one product, all ones, one huge pile.

Follow-ups

  • Quantities are up to 10^9 with no bound on their sum. Which approach still works?
  • Return the plan: which products to pick each day.
  • Every day now needs exactly k distinct products. How many days?

Where it's asked

  • Amazon · Online assessment · Last asked Jul 2025Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant

What it tests

greedy · sorting · binary-search

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