Cumulative Store Sales on Days Above Average (PySpark)

PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP

Problem

Topic: Window Functions · running total (SUM() OVER (PARTITION BY ... ORDER BY ...)) · above-average filter · GROUP BY

The retail analytics team wants to see how each store's strong days add up: for every store, the running total of its sales counted only on days above the store's average daily sales.

Using the PySpark DataFrame API (or Spark SQL), find those days and their running total.

Assign the result to df_result with the columns store_id, sale_date, daily_sales and cumulative_sales, sorted by store_id, then sale_date, and finish with df_result.show().

Schema

DataFrame: sales

| Column | Type | Meaning |
| --- | --- | --- |
| store_id | integer | The store |
| sale_amount | integer | Amount of one sale |
| sale_date | string | The day of the sale, `yyyy-MM-dd` (sorts correctly as text) |

Example 1

Input: sales

| store_id | sale_amount | sale_date |
| --- | --- | --- |
| 1 | 100 | 2025-03-01 |
| 1 | 200 | 2025-03-01 |
| 1 | 50 | 2025-03-02 |
| 1 | 400 | 2025-03-03 |
| 1 | 250 | 2025-03-04 |
| 2 | 500 | 2025-03-01 |
| 2 | 100 | 2025-03-02 |
| 2 | 100 | 2025-03-02 |
| 2 | 100 | 2025-03-02 |
| 2 | 200 | 2025-03-03 |

Output:

`

+--------+----------+-----------+----------------+

|store_id| sale_date|daily_sales|cumulative_sales|

+--------+----------+-----------+----------------+

|       1|2025-03-01|        300|             300|
|       1|2025-03-03|        400|             700|
|       2|2025-03-01|        500|             500|

+--------+----------+-----------+----------------+

`

Explanation:

Days above it: 1 Mar (300) and 3 Mar (400). 4 Mar equals the average, so it is not kept.

Running total of the kept days: 300, then 300 + 400 = 700 (2 Mar is not added).

Only 1 Mar is above it: running total 500.

Constraints

What this PYSPARK challenge teaches you

“Cumulative Store Sales on Days Above Average (PySpark)” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window functions, Running total, groupBy, Above average, Retail — 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. daily = sales.groupBy('store_id', 'sale_date').agg(F.sum('sale_amount').alias('daily_sales')).
  2. Average the daily totals per store: daily.groupBy('store_id').agg(F.avg('daily_sales')).
  3. Join, keep daily_sales > average, and only then add the running total.
  4. F.sum('daily_sales').over(Window.partitionBy('store_id').orderBy('sale_date')) is the running total.

Interview practice

This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

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

Helpful resources

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 medium and covers Window Functions.

Solve this challenge free on PySpark.in