Running Total of Daily Sales (PySpark)

PYSPARK coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP

Problem

A manager wants a running total: for every day, the sum of all sales up to and including that day.

Schema — `daily_sales`

| Column |
| --- |
| order_date |
| sales |

Example Input — `daily_sales`

| order_date | sales |
| --- | --- |
| 2024-06-01 | 100 |
| 2024-06-02 | 150 |
| 2024-06-03 | 120 |
| 2024-06-04 | 180 |
| 2024-06-05 | 200 |

Expected Output

| order_date | sales | running_total |
| --- | --- | --- |
| 2024-06-01 | 100 | 100 |
| 2024-06-02 | 150 | 250 |
| 2024-06-03 | 120 | 370 |
| 2024-06-04 | 180 | 550 |
| 2024-06-05 | 200 | 750 |

Explanation

Row 1 has nothing before it, so its running total is just its own sales: 100.

Row 2 adds its own 150 to the 100 already accumulated, giving 250. Row 3 adds 120 to that 250, giving 370, and so on. The final row's total, 750, is the sum of all five days.

Note that every input row still appears in the output. A groupBy() would collapse them into a single row, which is why this needs a window function.

Notes

What this PYSPARK challenge teaches you

“Running Total of Daily Sales (PySpark)” is a easy-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window, sum, rowsBetween, Running Total — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Window.orderBy('order_date').rowsBetween(Window.unboundedPreceding, 0)
  2. F.sum('sales').over(w) keeps every row, unlike groupBy.
  3. Finish with df_result.show().

How to practise it on PySpark.in

Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYSPARK challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated easy and covers Window Functions.

Solve this challenge free on PySpark.in