Find Riders Idle Through the Latest Week of Activity
SQL & DataAuto-graded · 14 test casesLast updated
Given user and ride records, return users with no ride in the 7-day window ending at the latest ride date, including users who never rode, sorted by user_id.
Idle Riders in the Latest Week
A ride-hailing service keeps two collections of records: its registered users and the rides they have taken. Your job is to find the idle users, meaning those who took no ride during the most recent week of activity found in the data. A user who is registered but has never taken any ride is idle as well.
Input
Both arguments are lists of dictionaries.
users: one dictionary per registered user.
| Key | Type | Meaning |
|---|---|---|
user_id | int | Unique identifier of the user |
city | str | The user's city; never null |
rides: one dictionary per ride.
| Key | Type | Meaning |
|---|---|---|
ride_id | int | Unique identifier of the ride |
ride_date | str | Date of the ride in YYYY-MM-DD format; never null |
ride_rating | int or None | Rating from 1 to 5, or None if the ride was not rated |
user_id | int | The rider; always matches some user in users |
Task
Return the list of idle users.
- The window consists of 7 calendar days ending on the latest
ride_datefound inrides. It starts at that latest date minus 6 days and finishes on the latest date. Both endpoints are included. - A user is active if at least one of their rides has a date inside the window. Every other user is idle.
- Return one dictionary per idle user with exactly the keys
user_idandcity. - Sort the result by
user_idin ascending order.
Rules and Edge Cases
- The window depends only on the data, never on the current date.
- A ride counts toward activity whether or not it has a rating;
ride_ratingis irrelevant here. - A user with several rides in the window is simply active; each idle user appears exactly once in the output.
- A user whose rides all fall before the window is idle.
- If
ridesis empty, there is no window and no activity, so every user is idle. - If
usersis empty, return an empty list.
Constraints
user_idvalues inusersare unique, so the ordering is unambiguous.- Every
ride_dateis a valid ISO date string. - Every
user_idinridesappears inusers. ride_ratingmay beNoneand must not affect the result.
Examples
Example 1
Input: find_idle_riders([{'user_id': 1, 'city': 'SF'}, {'user_id': 2, 'city': 'SF'}, {'user_id': 3, 'city': 'Phoenix'}, {'user_id': 4, 'city': 'Phoenix'}, {'user_id': 5, 'city': 'LA'}], [{'ride_id': 101, 'ride_date': '2024-07-01', 'ride_rating': 5, 'user_id': 1}, {'ride_id': 102, 'ride_date': '2024-07-09', 'ride_rating': 4, 'user_id': 2}, {'ride_id': 103, 'ride_date': '2024-07-14', 'ride_rating': None, 'user_id': 3}, {'ride_id': 104, 'ride_date': '2024-07-15', 'ride_rating': 3, 'user_id': 1}, {'ride_id': 105, 'ride_date': '2024-07-08', 'ride_rating': 5, 'user_id': 4}])
Output: [{'user_id': 4, 'city': 'Phoenix'}, {'user_id': 5, 'city': 'LA'}]
Hints
Hint 1
Think of it as an anti-join: start from the full user list and remove those who have at least one qualifying ride, rather than starting from the rides.
Hint 2
Find the latest ride date first, subtract 6 days to get the window start, then gather the user IDs of rides whose date lies between the two (inclusive) into a set.
Hint 3
Guard against an empty rides list before taking a maximum, and remember to sort the surviving users by user_id and output only user_id and city.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is an anti-join: keep every user who has no ride inside the window.
- If
ridesis empty, there is no window, so the set of active users is empty. - Otherwise, parse every
ride_dateand find the latest one. The window starts 6 days before it, which gives 7 calendar days with both ends included. - Build a set of
user_idvalues for rides whose date falls within the window. - Iterate over all users sorted by
user_idand keep those not in the active set, outputting onlyuser_idandcity.
Why It Is Correct
- Iterating over the full
userslist means users who never rode cannot be lost, and users whose rides are all before the window are naturally kept because they never enter the active set. - A user is excluded exactly when at least one of their rides satisfies
start <= ride_date <= latest, which matches the definition of active. - The set makes duplicates irrelevant: several rides in the window still produce a single membership.
- Ratings are never read, so unrated rides count as activity.
- The empty-
ridesbranch avoids callingmaxon an empty sequence and returns all users, as required. - Sorting by
user_id(unique) gives a deterministic order.
Complexity
Let U be the number of users and R the number of rides.
- Time: O(R) to find the latest date and build the active set, plus O(U log U) to sort the users.
- Space: O(R) for the active set in the worst case, plus O(U) for the output.
Reference Solution
from datetime import date, timedelta
from typing import Dict, List
def find_idle_riders(users: List[Dict], rides: List[Dict]) -> List[Dict]:
# With no rides there is no window, so nobody is active.
if not rides:
active = set()
else:
latest = max(date.fromisoformat(r["ride_date"]) for r in rides)
start = latest - timedelta(days=6)
active = {
r["user_id"]
for r in rides
if start <= date.fromisoformat(r["ride_date"]) <= latest
}
return [
{"user_id": u["user_id"], "city": u["city"]}
for u in sorted(users, key=lambda u: u["user_id"])
if u["user_id"] not in active
]