Toll Revenue with Repeat-Crossing Discounts
PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +150 XP
Problem
You are analysing toll transactions from an Uber toll plaza. The toll_log DataFrame holds every vehicle crossing.
What a crossing costs depends on how recently that vehicle last came through:
- A vehicle's first crossing is charged the regular toll.
- If the vehicle crossed within the previous 4 hours, it gets a 50% discount.
- Otherwise the regular toll applies.
Price every crossing, then report the total toll revenue per day.
Input DataFrame — `toll_log`
| Column | Type | | --- | --- | | toll_id | INT | | vehicle_id | STRING | | vehicle_type | STRING | | toll_time | TIMESTAMP |
Toll Rates
| vehicle_type | regular_toll | discounted | | --- | --- | --- | | car | 100 | 50 | | bike | 60 | 30 | | truck | 200 | 100 | | bus | 150 | 75 |
Rules
- The 4-hour window is measured against that vehicle's immediately previous crossing, not its first. So the discount chains: a vehicle crossing every 3 hours keeps earning it.
- A gap of exactly 4 hours still earns the discount.
- Group the revenue by the date of the crossing, sorted ascending.
Example Input
| toll_id | vehicle_id | vehicle_type | toll_time | | --- | --- | --- | --- | | 1 | V101 | car | 2024-01-15 08:00:00 | | 2 | V101 | car | 2024-01-15 10:30:00 | | 3 | V101 | car | 2024-01-15 15:00:00 | | 4 | V102 | bike | 2024-01-15 09:00:00 | | 5 | V102 | bike | 2024-01-15 12:00:00 | | 6 | V103 | truck | 2024-01-15 07:00:00 | | 7 | V103 | truck | 2024-01-15 12:00:00 | | 8 | V104 | bus | 2024-01-15 08:00:00 | | 9 | V104 | bus | 2024-01-15 12:00:00 | | 10 | V101 | car | 2024-01-16 08:00:00 | | 11 | V105 | truck | 2024-01-15 06:00:00 | | 12 | V105 | truck | 2024-01-15 09:00:00 | | 13 | V105 | truck | 2024-01-15 12:00:00 |
Expected Output
| toll_date | total_revenue | | --- | --- | | 2024-01-15 | 1365 | | 2024-01-16 | 100 |
Explanation
Crossing by crossing on 2024-01-15:
| vehicle | time | gap | charged | | --- | --- | --- | --- | | V101 car | 08:00 | first | 100 | | V101 car | 10:30 | 2h30m | 50 | | V101 car | 15:00 | 4h30m | 100 | | V102 bike | 09:00 | first | 60 | | V102 bike | 12:00 | 3h | 30 | | V103 truck | 07:00 | first | 200 | | V103 truck | 12:00 | 5h | 200 | | V104 bus | 08:00 | first | 150 | | V104 bus | 12:00 | **exactly 4h** | 75 | | V105 truck | 06:00 | first | 200 | | V105 truck | 09:00 | 3h | 100 | | V105 truck | 12:00 | 3h | 100 |
That is 1365. On 2024-01-16, V101's only crossing is 17 hours after its
previous one, so it pays the full 100.
V105 is the case worth studying. Its third crossing is 6 hours after its
first but only 3 hours after its second -- so it *is* discounted. Measuring
against the first crossing instead of the previous one gives 1065 and is wrong.
Notes
- The DataFrame is created for you -- do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Return exactly two columns:
toll_date,total_revenue, sorted bytoll_date - Functions to use:
Window.partitionBy(...).orderBy(...),F.lag,F.when,F.to_date,F.sum
What this PYSPARK challenge teaches you
“Toll Revenue with Repeat-Crossing Discounts” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with lag, Window, partitionBy, unix_timestamp, when/otherwise, groupBy — 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
- lag
- Window
- partitionBy
- unix_timestamp
- when/otherwise
- groupBy
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:
- Two steps: price each crossing row by row, then aggregate. Do not try to do both in one groupBy.
- To see a vehicle's previous crossing use a window: w = Window.partitionBy('vehicle_id').orderBy('toll_time'), then F.lag('toll_time').over(w).
- Subtracting two timestamp columns does not give you hours. Convert both with F.unix_timestamp(...) and subtract to get seconds, then compare against 4*3600.
- The first crossing has no previous row, so lag() is null and the gap is null. null <= 14400 is null, not true, so F.when(...).otherwise(regular) already charges it the full rate -- no special case needed.
- Build the regular rate with a chained F.when(...).when(...) on vehicle_type, then halve it for discounted rows. Cast the halved value to int so 75 does not print as 75.0.
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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
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.