Merge Overlapping Room Bookings
Problem
You are given a DataFrame `booking` recording bookings for a coworking space's meeting rooms. It has columns `room_id`, `from_day`, and `to_day` (date strings). Each booking covers a continuous block of days in one room, from `from_day` to `to_day` inclusive.
Two bookings for the same room overlap if they share at least one day (a booking ending on a day and another starting on that same day overlap). Overlapping bookings should be collapsed into a single continuous block.
For each room, merge all overlapping bookings into consolidated blocks. Return one row per merged block with columns `room_id`, `from_day`, and `to_day`. The result may be returned in any order.
Input data
Example rows — the live problem includes the full dataset.
| room_id | from_day | to_day |
|---|---|---|
| 1 | 2023-01-13 | 2023-01-14 |
| 1 | 2023-01-14 | 2023-01-17 |
| 1 | 2023-01-18 | 2023-01-25 |
| 2 | 2022-12-09 | 2022-12-23 |
| 2 | 2022-12-13 | 2022-12-17 |
Expected output
Your answer should return 4 rows with the columns room_id, from_day, to_day.
Starter code (Pandas (Python))
import pandas as pd
def merge_overlapping_room_bookings(booking) -> pd.DataFrame:
# Your code here
return bookingSolve 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