Pandas interview question: rolling 7-day revenue
Problem
Shared data rulesFor each city and day in Sep 7–27, 2026, find revenue over the last 7 days.
Formula
revenue_7d— the sum of amount_usd over orders placed fromwindow_end − 6towindow_end
Example
orders| order_id | created_at | status | city | amount_usd | Row note |
|---|---|---|---|---|---|
| 101 | 2026-09-02 12:00 | delivered | Boston | 20 | |
| 102 | 2026-09-03 18:20 | delivered | Boston | 40 | |
| 103 | 2026-09-04 09:30 | delivered | Boston | 30 | |
| 104 | 2026-09-06 11:10 | delivered | Boston | 50 | ← no orders on Sep 5 |
| 105 | 2026-09-07 19:30 | delivered | Boston | 20 | |
| 106 | 2026-09-08 13:00 | delivered | Boston | 40 | |
| 107 | 2026-09-09 10:15 | delivered | Boston | 30 |
- Order 104: no orders on Sep 5
| city | window_end | revenue_7d |
|---|---|---|
| Boston | 2026-09-07 | 160 |
| Boston | 2026-09-08 | 200 |
| Boston | 2026-09-09 | 210 |
Windows for Sep 7–9. The Sep 7 window covers Sep 1–7, so orders from before Sep 7 count too: 20 + 40 + 30 + 50 + 20 = $160.
Solve it free
Your code runs on real pandas in your browser. No sign-up needed.
Skills it tests
More interview questions
- Pandas: Cart-to-order conversionMedium · Funnels and conversion
- Pandas: Find the bugs in an average order value notebookEasy · Code review
- Python: Merge overlapping intervalsMedium · Intervals
- Python: Find the bugs in a median functionEasy · Code review
- SQL: Second-highest price in each groupEasy · Nth-highest value