Peak Sales Day and Its Share of Total (PySpark)
PYSPARK coding challenge · Difficulty: easy · Topic: Aggregation · +50 XP
Problem
Find the single best sales day in the period, and show what share of the period's total sales it accounted for.
Schema — `daily_traffic`
| Column | | --- | | order_date | | sales |
Example Input — `daily_traffic`
| order_date | sales | | --- | --- | | 2024-06-01 | 100 | | 2024-06-02 | 150 | | 2024-06-03 | 120 | | 2024-06-04 | 180 | | 2024-06-05 | 200 | | 2024-06-06 | 160 | | 2024-06-07 | 140 | | 2024-06-08 | 220 | | 2024-06-09 | 190 | | 2024-06-10 | 210 |
Expected Output
| order_date | sales | share_pct | | --- | --- | --- | | 2024-06-08 | 220 | 13.17 |
Explanation
Return exactly one row. The share is that day's sales as a percentage of the sum of all days, rounded to 2 decimals — so if the best day sold 220 out of 1670 sold in total, its share is 13.17.
Notes
- The DataFrame is created for you — do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Same problem in SQL: [Peak Sales Day and Its Share of Total](/challenges/peak-sales-day-and-its-share-of-total)
What this PYSPARK challenge teaches you
“Peak Sales Day and Its Share of Total (PySpark)” is a easy-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with agg, orderBy, limit, Aggregation — 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
- agg
- orderBy
- limit
- Aggregation
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:
- daily_traffic.agg(F.sum('sales')).collect()[0][0] gives the total.
- F.lit(total) puts a plain Python number into a column expression.
- orderBy(F.col('sales').desc()).limit(1) keeps only the best day.
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
- Find Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Average Order Amount for Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
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 Aggregation.