Aggregate orders into daily revenue (sum of amount per order_date). For each day compute change = revenue minus the revenue of the previous day that has orders, and pct_change = change / previous revenue * 100 rounded to 2 decimals. The first day has NULL change and NULL pct_change; pct_change is also NULL when the previous revenue is 0. Sort by order_date ascending.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | N | A | 1 | 1000.0 | 2024-01-01 |
| 2 | 2 | N | B | 1 | 500.0 | 2024-01-01 |
| 3 | 1 | S | A | 1 | 1800.0 | 2024-01-02 |
| 4 | 3 | E | C | 1 | 900.0 | 2024-01-03 |
| order_date | revenue | change | pct_change |
|---|---|---|---|
| 2024-01-01 | 1500 | NULL | NULL |
| 2024-01-02 | 1800 | 300 | 20 |
| 2024-01-03 | 900 | -900 | -50 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | N | A | 1 | 200.0 | 2024-02-10 |
| 2 | 1 | N | A | 1 | 300.0 | 2024-02-12 |
| order_date | revenue | change | pct_change |
|---|---|---|---|
| 2024-02-10 | 200 | NULL | NULL |
| 2024-02-12 | 300 | 100 | 50 |
Return columns order_date, revenue, change, pct_change, sorted by order_date.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Top 2 Earners per Department (Ties Included) · 3-Day Moving Average per Store ›
{{ totalCount }} questions · {{ shHardPct }}% Hard. Real PySpark DataFrame and Spark SQL interview questions. Write PySpark in your browser on TechTown’s Spark engine, run sample tests, then submit against hidden tests.
Guests can solve {{ freeLimit }} problems for free. Log in or sign up (free) to unlock everything, keep your streak and appear on the leaderboard. New to a topic? Learn it first in the Spark course.
{{ p.statement }}
{{ p.constraints }}
No submissions yet. Press Submit to judge your code against all tests.
The model solution and step-by-step explanation unlock when you solve this problem or after 3 unsuccessful submissions ({{ shFailsLabel }}). Struggling? Try the hints on the Question tab first.
{{ p.solution }}
Write real PySpark: from pyspark.sql import functions as F, Window, spark.sql(...). It runs in your browser on a lightweight Spark engine, and the same code runs unchanged on Databricks or any Spark cluster. Use df.show() or print() to debug.
Create your free TechTown account to unlock every problem, track your progress, bookmark questions and keep preparing for interviews.