Reduced Toll for Repeat Crossings
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +150 XP
Problem
A toll plaza rewards vehicles that come straight back: if a vehicle crosses again within 4 hours of its previous crossing, that crossing is charged a reduced toll instead of the regular one.
Given toll_log, work out what each individual crossing was charged.
Tables
Table: toll_log
| Column | Type | Description | | --- | --- | --- | | toll_id | INT | Unique toll transaction id | | vehicle_id | VARCHAR | Vehicle identifier | | vehicle_type | VARCHAR | One of `car`, `bike`, `truck`, `bus` | | toll_time | DATETIME | Time of the crossing |
Pricing
| Vehicle type | Regular toll | Repeat-crossing toll | | --- | --- | --- | | car | 100 | 50 | | bike | 60 | 30 | | truck | 200 | 120 | | bus | 150 | 90 |
Rules
- A crossing is REDUCED when the same vehicle crossed within the previous 4 hours; otherwise it is REGULAR.
- The comparison is against that vehicle's immediately previous crossing -- not its first. A vehicle crossing every 3 hours keeps earning the reduced toll.
- A gap of exactly 4 hours still counts as REDUCED. Four hours and one second does not.
- A vehicle's first crossing always pays the regular toll, and its
previous_toll_timeisNULL.
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 |
Expected Output
| vehicle_id | toll_time | vehicle_type | previous_toll_time | toll_category | toll_amount | | --- | --- | --- | --- | --- | --- | | V101 | 2024-01-15 08:00:00 | car | NULL | REGULAR | 100 | | V101 | 2024-01-15 10:30:00 | car | 2024-01-15 08:00:00 | REDUCED | 50 | | V101 | 2024-01-15 15:00:00 | car | 2024-01-15 10:30:00 | REGULAR | 100 | | V102 | 2024-01-15 09:00:00 | bike | NULL | REGULAR | 60 | | V102 | 2024-01-15 12:00:00 | bike | 2024-01-15 09:00:00 | REDUCED | 30 | | V103 | 2024-01-15 07:00:00 | truck | NULL | REGULAR | 200 | | V103 | 2024-01-15 12:00:00 | truck | 2024-01-15 07:00:00 | REGULAR | 200 | | V104 | 2024-01-15 08:00:00 | bus | NULL | REGULAR | 150 | | V104 | 2024-01-15 12:00:00 | bus | 2024-01-15 08:00:00 | REDUCED | 90 |
Explanation
V101 returns after 2h30m, so its second crossing is REDUCED (50). Its third
comes 4h30m after the second -- outside the window -- so it pays the regular 100
again.
V103 waits 5 hours, so both crossings are REGULAR (200 each).
V104 returns after exactly 4 hours, which is inside the window, so it is
REDUCED (90).
Notes
- Return:
vehicle_id,toll_time,vehicle_type,previous_toll_time,toll_category,toll_amount - Sort by
vehicle_id ASC, thentoll_time ASC - Every input row produces exactly one output row -- nothing is filtered out
- Function to use:
LAG(toll_time) OVER (PARTITION BY vehicle_id ORDER BY toll_time)
What this SQL challenge teaches you
“Reduced Toll for Repeat Crossings” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with LAG, window functions, PARTITION BY, CASE WHEN, INTERVAL, conditional pricing — 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 functions
- PARTITION BY
- CASE WHEN
- INTERVAL
- conditional pricing
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:
- Each row needs to know about the row BEFORE it for the same vehicle. That is LAG() -- the mirror image of LEAD(). No self-join needed.
- LAG(toll_time) OVER (PARTITION BY vehicle_id ORDER BY toll_time) gives the previous crossing. PARTITION BY keeps vehicles separate; ORDER BY decides what 'previous' means.
- A crossing is reduced when the previous one is recent enough: previous_toll_time >= toll_time - INTERVAL 4 HOUR. Use >= so exactly 4 hours still qualifies.
- For the first crossing LAG returns NULL, and NULL >= anything is NULL, not TRUE -- so CASE falls through to the REGULAR branch on its own. Do not add special NULL handling and do not filter those rows out.
- Two CASE expressions are needed: one for the category and one for the price. The price CASE has the same peak/off-peak shape -- an inner CASE vehicle_type inside each branch.
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 SQL query 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 SQL 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 SQL 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.