Out-of-Stock Minutes per Dark Store SKU From Inventory Events
Last updated: November 29, 2026
Quick Overview
Given an inventory event log with store id, SKU id, event timestamp and quantity after the event, compute for each store and SKU how many separate out-of-stock spells occurred and the total out-of-stock minutes up to a report cutoff time.
Swiggy
November 29, 20260
0
0 solved
Given an inventory event log with store id, SKU id, event timestamp and quantity after the event, compute for each store and SKU how many separate out-of-stock spells occurred and the total out-of-stock minutes up to a report cutoff time.
Availability is the metric Instamart lives on: a SKU that shows Sold out loses the order, and category teams use out-of-stock minutes to tune replenishment. The screen checks gaps-and-islands thinking with window functions and careful handling of repeated states and open spells at the cutoff.
What the Interviewer Expects
- Turns quantity into a binary out-of-stock flag
- Keeps only rows where the state changes, so repeated zero events do not create extra spells
- Uses LEAD to find each spell's end and clips open spells at the cutoff
- Partitions windows by store and SKU
- Computes minutes from timestamp differences correctly for the engine
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 restrict out-of-stock minutes to the store's operating hours?
- The event stream can arrive late or out of order from store devices. How does the pipeline handle it?
- How would you weight out-of-stock minutes by expected demand for that hour?
- How would you build this as an incremental daily job instead of a full recompute?
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
inventory_events(store_id, sku_id, event_ts, qty_after): one row per stock change (sale, inward, adjustment), with the quantity after the change. Out of stock means qty_after = 0. The state holds ...
Approach
- Flag each row
oos = 1when quantity is zero and get the previous flag with LAG. - Keep only rows where the flag differs from the previous row (or is the first row); these are spell starts.
- On...