Bikes Last Time Used - Problem
Imagine you're managing a bike-sharing system across the city, and you need to track when each bike was last returned to understand usage patterns and maintenance schedules.
You have a Bikes table that records every bike ride with the following structure:
| Column Name | Type |
|---|---|
| ride_id | int |
| bike_number | int |
| start_time | datetime |
| end_time | datetime |
Your task: Write a SQL query to find the last time each bike was used (i.e., the latest end_time for each bike).
Requirements:
- Return the
bike_numberand its corresponding latestend_time - Order results by the most recently used bikes first (latest
end_timedescending) - Each
ride_idis unique, and all datetime values are valid
Input & Output
Basic Example - Multiple Rides per Bike
$
Input:
Bikes table:
| ride_id | bike_number | start_time | end_time |
|---------|-------------|--------------------|--------------------|
| 1 | 101 | 2023-12-01 08:00:00| 2023-12-01 08:30:00|
| 2 | 102 | 2023-12-01 09:00:00| 2023-12-01 09:45:00|
| 3 | 101 | 2023-12-01 10:00:00| 2023-12-01 10:20:00|
| 4 | 103 | 2023-12-01 11:00:00| 2023-12-01 11:30:00|
| 5 | 102 | 2023-12-01 12:00:00| 2023-12-01 12:25:00|
โบ
Output:
| bike_number | last_used |
|-------------|--------------------|
| 102 | 2023-12-01 12:25:00|
| 103 | 2023-12-01 11:30:00|
| 101 | 2023-12-01 10:20:00|
๐ก Note:
Bike 102 was used twice (ending at 09:45 and 12:25), so its last_used is 12:25. Bike 101 was also used twice (ending at 08:30 and 10:20), so its last_used is 10:20. Results are ordered by most recent usage first.
Single Ride per Bike
$
Input:
Bikes table:
| ride_id | bike_number | start_time | end_time |
|---------|-------------|--------------------|--------------------|
| 1 | 201 | 2023-11-30 14:00:00| 2023-11-30 14:45:00|
| 2 | 202 | 2023-11-30 15:00:00| 2023-11-30 15:30:00|
| 3 | 203 | 2023-11-30 13:00:00| 2023-11-30 13:20:00|
โบ
Output:
| bike_number | last_used |
|-------------|--------------------|
| 202 | 2023-11-30 15:30:00|
| 201 | 2023-11-30 14:45:00|
| 203 | 2023-11-30 13:20:00|
๐ก Note:
Each bike has only one ride, so the last_used time is simply their respective end_time. Results are ordered by end_time descending.
Same Bike, Different Days
$
Input:
Bikes table:
| ride_id | bike_number | start_time | end_time |
|---------|-------------|--------------------|--------------------|
| 1 | 301 | 2023-11-28 10:00:00| 2023-11-28 10:30:00|
| 2 | 301 | 2023-11-29 14:00:00| 2023-11-29 14:25:00|
| 3 | 301 | 2023-11-27 16:00:00| 2023-11-27 16:15:00|
โบ
Output:
| bike_number | last_used |
|-------------|--------------------|
| 301 | 2023-11-29 14:25:00|
๐ก Note:
Bike 301 was used on three different days. The latest end_time is 2023-11-29 14:25:00, which becomes the last_used time for this bike.
Visualization
Tap to expand
Understanding the Visualization
1
Collect All Rides
Gather all bike ride records from the database
2
Group by Bike
Organize rides by bike_number into separate groups
3
Find Latest per Group
For each bike group, find the maximum end_time
4
Sort by Recency
Order results by latest usage first
Key Takeaway
๐ฏ Key Insight: GROUP BY with MAX() allows the database to efficiently find the maximum value within each group in a single pass, avoiding the need for multiple subqueries or complex joins.
Time & Space Complexity
Time Complexity
O(n log n)
O(n) for grouping + O(n log n) for sorting by end_time
โก Linearithmic
Space Complexity
O(k)
Where k is the number of unique bikes (much smaller than total records)
โ Linear Space
Constraints
- 1 โค number of rides โค 105
- 1 โค bike_number โค 104
- start_time < end_time for all rides
- All datetime values are in format 'YYYY-MM-DD HH:MM:SS'
- ride_id is unique for each row
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code