← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jun 2026

Summary

Amazon OA question for a SWE role, simulation-style problem involving order books and sequential purchasing logic. Pretty clean problem statement but the edge cases are where it gets tricky.

Questions Asked (1)

Q1

You're given an array where each index represents a day and the value is the volume number that comes into stock that day. Each day, buy as many volumes as possible given that you can only buy volume i if you already own all volumes before it. Return a list of lists showing which volumes you bought on each day, or [-1] if none.

Algorithms & Data Structures
Author's notes

The greedy part is straightforward enough: each day a new volume arrives, check if it unlocks a chain of purchasable volumes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and define the buying rule precisely: each day, you can buy a contiguous prefix of volumes starting from the first volume you don't yet own, up to the maximum available volume that day. Then, simulate the process day by day, tracking the highest volume owned and the volumes bought each day, ensuring you only buy volumes that are both available and in the correct order.

Pro tip: Before coding, walk through a small example to confirm your understanding of the rule, especially the 'buy as many as possible' part—this often means buying all volumes from the next needed up to the maximum available that day, but only if they form a contiguous sequence. Also, consider edge cases like when no volumes can be bought or when the array is empty.

1. Clarify the problem

Ask questions to confirm the buying rule: Can you buy multiple volumes in a day? Do you have to buy in order? What does 'as many as possible' mean exactly? Confirm that you can only buy volume i if you already own all volumes before it, and that you buy the maximum possible contiguous set starting from the next needed volume.

2. Define the algorithm

Initialize a variable to track the highest volume you own (starting at 0). For each day, determine the maximum volume available that day. If that maximum is greater than your current highest owned, you can buy all volumes from highest+1 up to that maximum (since you already own all before highest+1). Record the list of volumes bought that day, or [-1] if none.

3. Walk through an example

Take a small array like [2, 1, 3] and simulate: Day 1: max available=2, own none, so buy volumes 1 and 2? But wait, volume 1 is not available on day 1 (only volume 2 is). So you cannot buy volume 1 because it's not in stock. Thus, you can only buy volume 2 if you already own volume 1, which you don't. So you buy nothing. This highlights the need to check availability of each volume in the prefix.

4. Refine the rule

The correct interpretation: On each day, you can buy a set of volumes that are available that day and form a contiguous sequence starting from the next volume you need (i.e., highest_owned+1). You can only buy volume i if you already own all volumes before it. So you need to check if the next needed volume is available that day; if not, you buy nothing. If it is, you can continue buying subsequent volumes as long as they are available that day and you own all previous ones.

5. Implement and test

Write code to simulate the process: maintain a set of owned volumes (or just the highest owned if volumes are bought in order). For each day, check if the next needed volume is in the day's available set. If yes, buy it and then check the next, and so on, until the next needed is not available. Record the bought volumes. Test with edge cases: empty array, all volumes available on day 1, volumes appearing out of order, etc.

Key Points to Mention

  • Clarify the buying rule: you can only buy volume i if you already own all volumes before it, and you can only buy volumes that are available that day.
  • The 'buy as many as possible' means buying the longest contiguous prefix of volumes starting from the next needed volume, provided they are all available that day.
  • Maintain the highest volume owned (or a set of owned volumes) to efficiently determine the next needed volume.
  • For each day, check if the next needed volume is available; if not, buy nothing. If yes, buy it and continue checking subsequent volumes.
  • Consider edge cases: empty array, days with no available volumes, volumes appearing out of order, and large input sizes.
  • Discuss time and space complexity: O(n) time where n is total number of volumes, and O(n) space for the output.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.