Latest Booking per Room
Problem
You are given three DataFrames:
- `guests` with `guest_id`, `guest_name`
- `bookings` with `booking_id`, `booked_on` (date string), `guest_id`, `room_id`
- `rooms` with `room_id`, `room_name`, `nightly_rate`
For each room, find the booking(s) with the most recent `booked_on` date. If two bookings of the same room share that latest date, return both of them (ties are kept).
Return columns `room_name`, `room_id`, `booking_id`, `booked_on`, sorted by `room_name`, then `room_id`, then `booking_id`. Rooms that were never booked do not appear.
Input data
Example rows — the live problem includes the full dataset.
| guest_id | guest_name |
|---|---|
| 1 | Wendell |
| 2 | Bianca |
| 3 | Soraya |
| 4 | Mateo |
| 5 | Kelyn |
| booking_id | booked_on | guest_id | room_id |
|---|---|---|---|
| 1 | 2021-07-31 | 1 | 1 |
| 2 | 2021-07-30 | 2 | 2 |
| 3 | 2021-08-29 | 3 | 3 |
| 4 | 2021-07-29 | 4 | 1 |
| 5 | 2021-06-10 | 1 | 2 |
| room_id | room_name | nightly_rate |
|---|---|---|
| 1 | Garden Suite | 120 |
| 2 | Loft | 80 |
| 3 | Penthouse | 600 |
| 4 | Cabin | 450 |
Expected output
Your answer should return 4 rows with the columns room_name, room_id, booking_id, booked_on.
Starter code (Pandas (Python))
import pandas as pd
def latest_booking_per_room(guests, bookings, rooms) -> pd.DataFrame:
# Your code here
return roomsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock