Collapse Daily Inventory Snapshots Into Out-of-Stock Periods per FC
Last updated: November 29, 2026
Quick Overview
Given one inventory status snapshot per ASIN, fulfillment center and day, collapse consecutive days with the same status into periods with a start date, end date and length, and list out-of-stock periods of three days or more.
Amazon
November 29, 20260
0
0 solved
Given one inventory status snapshot per ASIN, fulfillment center and day, collapse consecutive days with the same status into periods with a start date, end date and length, and list out-of-stock periods of three days or more.
A gaps-and-islands question grounded in Amazon fulfillment operations, where out-of-stock duration by FC drives replenishment and lost sales estimates. The interviewer wants state change detection with LAG, a running sum to label runs, and correct handling of missing snapshot days.
What the Interviewer Expects
- Detect a new run when status changes or when a snapshot day is missing
- Label runs with a running SUM of change flags
- Partition by both ASIN and fulfillment center
- Aggregate runs into start, end and length, then filter
- Discuss whether a missing day should break or bridge a run
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you estimate lost units during each out-of-stock period?
- How would you treat a period that is still open at the end of the data?
- How would you compute the share of days each ASIN was out of stock across the network?
- Can you do the same with the row number difference trick and when does it fail?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table inventory_snapshots(asin, fc_id, snapshot_date, status) with status in IN_STOCK or OUT_OF_STOCK, ideally one row per ASIN, FC and day. Questions: can snapshots be missing (yes, the job s...
Approach
This is gaps and islands driven by a state change rather than by consecutive dates alone.
- Use
LAGto get the previous status and previous snapshot date within each ASIN and FC. - Flag `new_run ...