AnalystPath

Latest Booking per Room

PandasMediumMid level~10 min

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.

guests
guest_idguest_name
1Wendell
2Bianca
3Soraya
4Mateo
5Kelyn
bookings
booking_idbooked_onguest_idroom_id
12021-07-3111
22021-07-3022
32021-08-2933
42021-07-2941
52021-06-1012
rooms
room_idroom_namenightly_rate
1Garden Suite120
2Loft80
3Penthouse600
4Cabin450

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 rooms

Solve 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

Related Pandas questions