InterviewDB Question

ASIN Average Price - Compute Rolling Average Price per Product

Question Details

Problem

You have a table of product price snapshots:

sql
CREATE TABLE price_snapshots (
    asin        VARCHAR(10),
    snapshot_dt DATE,
    price       DECIMAL(10,2)
);

For each ASIN, compute the 7-day rolling average price (average of the current day and the 6 preceding days where data exists).

Return rows with asin, snapshot_dt, and rolling_avg_price rounded to 2 decimal places.

Example

asin | snapshot_dt | price
B001 | 2024-01-01  | 10.00
B001 | 2024-01-03  | 12.00
B001 | 2024-01-07  | 14.00

-- For B001 on 2024-01-07:
-- dates in [2024-01-01, 2024-01-07] with data: 01-01, 01-03, 01-07
-- rolling_avg = (10 + 12 + 14) / 3 = 12.00

Follow-ups

  1. How would you write this using a window function with ROWS BETWEEN vs. RANGE BETWEEN? What is the difference?
  2. What if you need a strict 7-row window (exactly the last 7 records per ASIN), not date-based?
  3. How would you handle ASINs that have no data for 30+ days (staleness detection)?
  4. The table has 500M rows. What indexes and partitioning strategy would you recommend?

Full Details

Problem

You have a table of product price snapshots:

sql
CREATE TABLE price_snapshots (
    asin        VARCHAR(10),
    snapshot_dt DATE,
    price       DECIMAL(10,2)
);

For each ASIN, compute the 7-day rolling average price (average of the current day and the 6 preceding days where data exists).

Return rows with asin, snapshot_dt, and rolling_avg_price rounded to 2 decimal places.

Example

asin | snapshot_dt | price
B001 | 2024-01-01  | 10.00
B001 | 2024-01-03  | 12.00
B001 | 2024-01-07  | 14.00

-- For B001 on 2024-01-07:
-- dates in [2024-01-01, 2024-01-07] with data: 01-01, 01-03, 01-07
-- rolling_avg = (10 + 12 + 14) / 3 = 12.00

Follow-ups

  1. How would you write this using a window function with ROWS BETWEEN vs. RANGE BETWEEN? What is the difference?
  2. What if you need a strict 7-row window (exactly the last 7 records per ASIN), not date-based?
  3. How would you handle ASINs that have no data for 30+ days (staleness detection)?
  4. The table has 500M rows. What indexes and partitioning strategy would you recommend?
Free preview. Unlock all questions →

Topics

Coding Onsite Phone