Start learning free

LESSON 176 OF 188 · BACKEND: INTERVIEW QUESTIONS

Don't Double-Book the Room in Python

Practice

An interview favorite: "Build POST /bookings for meeting rooms. Never let two bookings of the same room overlap." The whole question comes down to one test. Two time ranges overlap when each one starts before the other one ends: a.start < b.end and b.start < a.end. That one line catches every shape of clash: partly over, inside, or all around.

An example with other times: 14:00–15:30 and 15:00–16:00 overlap, because 14:00 < 16:00 and 15:00 < 15:30. But 14:00–15:00 and 15:00–16:00 don't: 15:00 < 15:00 is false, so a meeting may start the minute another one ends. Times like "09:30" can be compared as text, because they all have the same shape.

A clash gets 409 Conflict: the request is fine by itself, but it clashes with what is already stored. A helpful 409 names the thing in the way, like this route for another resource:

@app.post("/usernames")
def claim_username():
    name = request.get_json()["name"]
    if name in taken:
        return jsonify({"error": "Name taken", "name": name}), 409
    taken.append(name)
    return jsonify({"name": name}), 201

These requests, in order, must get these answers:

POST /bookings  {"room": "Blue", "start": "10:00", "end": "11:00"}   →  201  {"id": 1, "room": "Blue", "start": "10:00", "end": "11:00"}
POST /bookings  {"room": "Blue", "start": "11:00", "end": "12:00"}   →  201  {"id": 2, "room": "Blue", …}
POST /bookings  {"room": "Blue", "start": "10:30", "end": "11:30"}   →  409  {"error": "The room is already booked", "booking": {"id": 1, "room": "Blue", "start": "10:00", "end": "11:00"}}
POST /bookings  {"room": "Blue", "start": "13:00", "end": "14:00"}   →  201  {"id": 3, "room": "Blue", …}
POST /bookings  {"room": "Blue", "start": "12:30", "end": "15:00"}   →  409  {"error": "The room is already booked", "booking": {"id": 3, …}}
POST /bookings  {"room": "Green", "start": "10:30", "end": "11:30"}  →  201  {"id": 4, "room": "Green", …}
POST /bookings  {"room": "Green", "start": "16:00", "end": "15:00"}  →  400  {"error": "The end must be after the start"}
GET /bookings  →  200  [bookings 1, 2, 3 and 4]

Your task: the starting code checks that the end comes after the start, then saves every booking and answers 201. Write a function overlaps(a, b), and before saving, loop over the stored bookings: the first one of the same room that overlaps gets a 409 with the error "The room is already booked" and that booking as "booking".


Assignment
Follow the Coding Tutorial to write the overlap test and refuse a booking that clashes with a stored one.


Hint
Use <, not <=, in both comparisons, so back-to-back bookings are allowed. Check the room too: a booking of another room never clashes. The return inside the loop ends the function before the new booking is saved.


STEP 1 OF 5
    Ctrl + Enter to check

    Bq Double Booking in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free