Aggregate Daily Toll Revenue
PYSPARK coding challenge · Difficulty: easy · Topic: Aggregation · +75 XP
Problem
You are working with Uber toll-plaza transactions in a PySpark DataFrame called toll_log. Each row is one vehicle crossing the plaza.
Calculate the total toll revenue collected per day.
The data is large and lives in a distributed Spark environment, so the aggregation has to be done by Spark itself -- not by pulling rows back to the driver.
Input DataFrame — `toll_log`
| Column | Type | Description | | --- | --- | --- | | toll_id | INT | Unique toll transaction id | | vehicle_id | STRING | Vehicle identifier | | vehicle_type | STRING | Type of vehicle | | toll_time | TIMESTAMP | Time of the crossing | | toll_amount | DECIMAL | Toll collected |
Example Input
| toll_id | vehicle_id | vehicle_type | toll_time | toll_amount | | --- | --- | --- | --- | --- | | 1 | V101 | car | 2024-01-15 08:00:00 | 100 | | 2 | V102 | truck | 2024-01-15 09:30:00 | 200 | | 3 | V103 | bike | 2024-01-15 12:00:00 | 60 | | 4 | V104 | car | 2024-01-15 18:30:00 | 100 | | 5 | V105 | bus | 2024-01-16 08:15:00 | 150 | | 6 | V106 | car | 2024-01-16 10:00:00 | 100 | | 7 | V107 | truck | 2024-01-16 14:30:00 | 200 | | 9 | V109 | bike | 2024-01-18 00:10:00 | 60 | | 8 | V108 | car | 2024-01-17 23:50:00 | 100 |
Note the last two rows: they are stored out of order, and they sit either side
of midnight. Both are there on purpose.
Expected Output
| toll_date | total_revenue | | --- | --- | | 2024-01-15 | 460.00 | | 2024-01-16 | 450.00 | | 2024-01-17 | 100.00 | | 2024-01-18 | 60.00 |
Calculation
- 2024-01-15:
100 + 200 + 60 + 100 = 460 - 2024-01-16:
150 + 100 + 200 = 450 - 2024-01-17: the
23:50crossing ->100 - 2024-01-18: the
00:10crossing ->60
The last two are twenty minutes apart but belong to different days. Truncating
toll_time to a date is what separates them.
Why the totals show two decimals: toll_amount is a DECIMAL, and summing a
decimal keeps its scale, so F.sum("toll_amount") gives 460.00 rather than
460. Both are the same number and grading compares by value, so you do not need
to reformat anything -- just do not change the total.
Task
Build a DataFrame called df_result that:
1. Extracts the date from toll_time
2. Groups the transactions by that date
3. Sums the toll amount for each date as total_revenue
4. Orders the result by toll_date ascending
5. Ends with df_result.show()
Constraints
- PySpark DataFrame API only -- no
spark.sql(), no SQL strings - Do not
collect()the data to the driver - Do not use a Python loop to aggregate
- Use Spark's distributed
groupBy()andsum() toll_logis provided -- do not recreate it
Notes
- Return exactly two columns:
toll_date,total_revenue - Functions to use:
F.to_date,groupBy,F.sum,orderBy
What this PYSPARK challenge teaches you
“Aggregate Daily Toll Revenue” is a easy-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with groupBy, sum, to_date, aggregation, orderBy — 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
- groupBy
- sum
- to_date
- aggregation
- orderBy
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:
- A TIMESTAMP carries a time of day, so grouping by toll_time directly gives you one group per crossing. You need the date part only.
- F.to_date('toll_time') truncates a timestamp to a date. Add it as a column first, then group by it.
- Aggregate with .groupBy('toll_date').agg(F.sum('toll_amount').alias('total_revenue')) — the alias is what names the output column.
- Finish with .orderBy('toll_date') so the days come out oldest first, then df_result.show().
- toll_amount is a DECIMAL, so the total prints as 460.00 rather than 460. That is just how a decimal renders -- the value is what is graded, so no reformatting is needed.
Where this comes up
Variations of this problem have been reported in interviews at Uber. Interviewers use 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
- 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.