For every order, compute running_total: the cumulative sum of amount for that customer, in order of order_date and then order_id (so two orders on the same day are added one at a time). NULL amounts add nothing to the total.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 101 | North | Laptop | 1 | 55000.0 | 2024-01-05 |
| 2 | 101 | North | Mobile | 1 | 15000.0 | 2024-01-20 |
| 3 | 102 | South | Monitor | 2 | 18000.0 | 2024-01-07 |
| 4 | 101 | North | Keyboard | 1 | 1500.0 | 2024-02-02 |
| customer_id | order_id | order_date | amount | running_total |
|---|---|---|---|---|
| 101 | 1 | 2024-01-05 | 55000 | 55000 |
| 101 | 2 | 2024-01-20 | 15000 | 70000 |
| 101 | 4 | 2024-02-02 | 1500 | 71500 |
| 102 | 3 | 2024-01-07 | 18000 | 18000 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 10 | 201 | West | Mobile | 1 | 12000.0 | 2024-03-01 |
| 11 | 201 | West | Headphones | 2 | 4000.0 | 2024-03-01 |
| 12 | 202 | East | Laptop | 1 | 60000.0 | 2024-03-02 |
| customer_id | order_id | order_date | amount | running_total |
|---|---|---|---|---|
| 201 | 10 | 2024-03-01 | 12000 | 12000 |
| 201 | 11 | 2024-03-01 | 4000 | 16000 |
| 202 | 12 | 2024-03-02 | 60000 | 60000 |
Return columns customer_id, order_id, order_date, amount, running_total. Row order does not matter.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Rank Salaries Within Each Department · Previous Order Amount with lag() ›
{{ 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.