AnalystPath

Merge Overlapping Room Bookings

PandasHardSenior level~10 min

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.

booking
room_idfrom_dayto_day
12023-01-132023-01-14
12023-01-142023-01-17
12023-01-182023-01-25
22022-12-092022-12-23
22022-12-132022-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 booking

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