Longest stockout run

window functions

Longest stockout run

Blinkit SQL Interview Question

Blinkit judges a dark store on how long a product stays missing, not on how often it goes missing. One empty day is a delivery that slipped. Four empty days in a row is a customer who has already installed another app.

For every store and product, return the store_id, the sku, and the length in days of its longest unbroken run of days out of stock as longest_stockout_days. Leave out any store and product that was never out of stock.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

blinkit_stock_checksTable30 rows

Column NameType
check_idBIGINT
store_idBIGINT
skuVARCHAR
checked_onDATE
is_availableBOOLEAN

blinkit_stock_checksExample Input

check_idstore_idskuchecked_onis_available
1101ATTA-5KG2024-06-01true
2101ATTA-5KG2024-06-02true
3101ATTA-5KG2024-06-03false
4101ATTA-5KG2024-06-04false
5101ATTA-5KG2024-06-05false
6101ATTA-5KG2024-06-06true
7101ATTA-5KG2024-06-07true
8101ATTA-5KG2024-06-08false
11101MILK-1L2024-06-01true
12101MILK-1L2024-06-02false
13101MILK-1L2024-06-03true
14101MILK-1L2024-06-04true
15101MILK-1L2024-06-05true
16101MILK-1L2024-06-06true
17101MILK-1L2024-06-07false
18101MILK-1L2024-06-08false

Example Output

store_idskulongest_stockout_days
101ATTA-5KG3
101MILK-1L2

Explanation

ATTA-5KG was missing on three days in a row, came back for two days, then went missing for one more day, so its longest run is 3. MILK-1L was missing once on its own and later for two days together, so its longest run is 2.

The example above is a small slice of the data. Your query runs against the full tables.