Python Interview Prep/Month-over-Month Revenue
Amazon logo

Month-over-Month Revenue

Amazon·medium·pandas·About 20 min in the room

transactions holds every purchase, with its created_at date (as text) and its value. For each month, find the total revenue and the percentage change from the month before, rounded to 2 decimal places. Return result with the columns year_month (text such as 2025-01), revenue and pct_change, in month order. The first month has no month before it, so its pct_change is empty (NaN).

The data

transactions16 rows
idint64user_idint64created_atobjectvalueint64
172025-03-09310
232025-01-04120
392025-02-14230
432025-01-27200
5122025-03-21170
672025-04-05210
and 10 more rows

Target output

year_monthrevenuepct_change
2025-01400NaN
2025-02380-5
2025-0363065.79
2025-04350-44.44
2025-0567592.86
2025-06590-12.59

What this question tests

Tests dates end to end: text to datetime, a month key that sorts correctly across a year boundary, a total per month and a change against the row before. Revenue movement is one of the most common analyst questions there is, and the month key is where most answers quietly go wrong.

More Python interview questions

  • Weeks at Number One · Spotify · easy
  • The Salary Gap · LinkedIn · easy
  • Back Within a Week · Amazon · medium
  • Average Session Length · Meta · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...