Implement a Listing Calendar That Rejects Overlapping Reservations
Last updated: November 29, 2026
Quick Overview
Design a class for one listing's reservations with book(check_in, check_out, id), cancel(check_in, id), and is_free(day), using half-open day ranges. Booking must fail if it overlaps any existing stay, while back-to-back stays are allowed.
Airbnb
November 29, 20260
0
0 solved
Design a class for one listing's reservations with book(check_in, check_out, id), cancel(check_in, id), and is_free(day), using half-open day ranges. Booking must fail if it overlaps any existing stay, while back-to-back stays are allowed.
Preventing double bookings is the most fundamental invariant on Airbnb. In a coding round this becomes an ordered-set exercise: keep stays sorted so each check only needs the neighbors, and make cancel and lookup correct too.
What the Interviewer Expects
- Keep reservations sorted by check-in
- Check only the previous and next stays for overlap
- Handle back-to-back stays correctly with half-open ranges
- Make cancel verify the reservation id
- Discuss the cost of list insertion versus a balanced tree
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What data structure gives O(log n) inserts in a language without a sorted container?
- How would you support a listing that has three identical rooms?
- How does this change when two servers receive bookings at the same time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Stays are [check_in, check_out) integer days. book returns True and stores the stay if it does not overlap any existing stay. cancel removes the stay only if the id matches. is_free(day) says wh...
High-Level Design
Because existing stays never overlap, sorting them by check-in also sorts them by check-out. A new stay can only conflict with its immediate neighbors in that order: the last stay starting before it (...