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 NameType
ride_idint
bike_numberint
start_timedatetime
end_timedatetime

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_number and its corresponding latest end_time
  • Order results by the most recently used bikes first (latest end_time descending)
  • Each ride_id is 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
Bike Usage Timeline AnalysisBike 10108:3010:20LATESTBike 10209:4512:25LATESTBike 10311:30LATESTFinal Result (Ordered by Latest Usage)1. Bike 102 - 12:25 (most recent)2. Bike 103 - 11:303. Bike 101 - 10:20 (least recent)
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

n
2n
โšก Linearithmic
Space Complexity
O(k)

Where k is the number of unique bikes (much smaller than total records)

n
2n
โœ“ 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
Asked in
Uber 45 Lyft 32 Amazon 28 Google 25
28.5K Views
Medium Frequency
~12 min Avg. Time
890 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