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
Data Manipulation (SQL/Python)
Data Engineer
Amazon
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Hard

0

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
gaps and islands
LAG
running sum
state changes
inventory
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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.

  1. Use LAG to get the previous status and previous snapshot date within each ASIN and FC.
  2. Flag `new_run ...

Submit Your Answer
Markdown supported