Calculate Parking Fees and Duration - Problem

You are given a table ParkingTransactions that contains information about parking transactions for different cars across multiple parking lots.

Write a solution to find:

  • The total parking fee paid by each car across all parking lots
  • The average hourly fee (rounded to 2 decimal places) paid by each car
  • The parking lot where each car spent the most total time

Return the result table ordered by car_id in ascending order.

Note: Test cases ensure that an individual car cannot be in multiple parking lots at the same time.

Table Schema

ParkingTransactions
Column Name Type Description
lot_id PK int ID of the parking lot
car_id PK int ID of the car
entry_time PK datetime Entry timestamp for parking
exit_time datetime Exit timestamp for parking
fee_paid decimal Fee paid for the parking duration
Primary Key: (lot_id, car_id, entry_time)
Note: Each row represents a parking transaction with unique combination of lot_id, car_id, and entry_time

Input & Output

Example 1 — Multiple Cars, Multiple Lots
Input Table:
lot_id car_id entry_time exit_time fee_paid
1 1001 2023-06-01 08:00:00 2023-06-01 10:30:00 5
1 1001 2023-06-02 11:00:00 2023-06-02 12:45:00 3
2 1001 2023-06-01 10:45:00 2023-06-01 12:00:00 6
2 1002 2023-06-01 09:00:00 2023-06-01 11:30:00 4
3 1001 2023-06-03 07:00:00 2023-06-03 09:00:00 4
3 1002 2023-06-02 12:00:00 2023-06-02 14:00:00 2
Output:
car_id total_fee_paid avg_hourly_fee most_time_lot
1001 18 2.4 1
1002 6 1.33 2
💡 Note:

Car 1001 has 4 transactions totaling $18.00 across 7.5 hours (avg $2.40/hour). Most time spent in lot 1 (4.25 hours). Car 1002 has 2 transactions totaling $6.00 across 4.5 hours (avg $1.33/hour). Most time spent in lot 2 (2.5 hours).

Example 2 — Single Transaction Per Car
Input Table:
lot_id car_id entry_time exit_time fee_paid
1 2001 2023-06-01 14:00:00 2023-06-01 16:00:00 8
2 2002 2023-06-01 10:00:00 2023-06-01 10:30:00 2.5
Output:
car_id total_fee_paid avg_hourly_fee most_time_lot
2001 8 4 1
2002 2.5 5 2
💡 Note:

Each car has only one transaction. Car 2001 paid $8.00 for 2 hours ($4.00/hour) in lot 1. Car 2002 paid $2.50 for 0.5 hours ($5.00/hour) in lot 2. Most time lot equals the only lot used.

Constraints

  • 1 ≤ lot_id ≤ 100
  • 1 ≤ car_id ≤ 10000
  • entry_time < exit_time
  • fee_paid ≥ 0
  • A car cannot be in multiple parking lots at the same time

Visualization

Tap to expand
Calculate Parking Fees and Duration INPUT ParkingTransactions car_id lot_id entry exit fee 101 A 08:00 10:00 6.00 101 B 12:00 14:00 8.00 101 A 16:00 19:00 9.00 102 B 09:00 12:00 12.00 Columns: - car_id: Vehicle ID - lot_id: Parking lot - entry_time: Start - exit_time: End - fee_paid: Amount Multiple Cars ALGORITHM STEPS 1 Calculate Duration TIMESTAMPDIFF(exit, entry) duration_hours per row 2 Group by car_id SUM(fee) = total_fee SUM(hours) = total_hours 3 Avg Hourly Rate ROUND(total_fee / total_hours, 2) 4 Find Most Time Lot Subquery: GROUP BY car_id, lot_id RANK() OVER partition SELECT car_id, SUM(fee) total_fee, ROUND(AVG(fee/hr),2) ORDER BY car_id ASC FINAL RESULT Output Table car_id total_fee avg_hr most_lot 101 23.00 3.29 A 102 12.00 4.00 B Car 101: Fees: 6+8+9 = 23.00 Hours: 2+2+3 = 7 Lot A: 5hrs (most time) Car 102: Fees: 12.00 Most time: Lot B OK - Sorted by car_id ASC Key Insight: Use window functions (RANK/ROW_NUMBER) with PARTITION BY car_id to find the lot with maximum time per car. Join aggregated results with a subquery that calculates time spent per car per lot, then filter for rank = 1. TIMESTAMPDIFF calculates hours between entry and exit. ROUND ensures 2 decimal precision for avg hourly fee. TutorialsPoint - Calculate Parking Fees and Duration | Optimal Solution
Asked in
Uber 28 Lyft 15 Amazon 12
23.4K Views
Medium Frequency
~18 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen