KatInterview

Find Riders Idle Through the Latest Week of Activity

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Dataanti-joindate-windowhash-set

Last 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.

KeyTypeMeaning
user_idintUnique identifier of the user
citystrThe user's city; never null

rides: one dictionary per ride.

KeyTypeMeaning
ride_idintUnique identifier of the ride
ride_datestrDate of the ride in YYYY-MM-DD format; never null
ride_ratingint or NoneRating from 1 to 5, or None if the ride was not rated
user_idintThe 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_date found in rides. 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_id and city.
  • Sort the result by user_id in 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_rating is 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 rides is empty, there is no window and no activity, so every user is idle.
  • If users is empty, return an empty list.

Constraints

  • user_id values in users are unique, so the ordering is unambiguous.
  • Every ride_date is a valid ISO date string.
  • Every user_id in rides appears in users.
  • ride_rating may be None and 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.