Bird
Raised Fist0

Given this booking function:

medium📝 Analysis Q5 of Q15
LLD - Design — Hotel Booking System
Given this booking function:
def book(slot):
    lock(slot)
    if is_available(slot):
        confirm_booking(slot)
        unlock(slot)
        return 'Booked'
    unlock(slot)
    return 'Unavailable'

What is the main reason for locking the slot before checking availability?
ATo check slot availability without any restrictions
BTo speed up the booking process
CTo allow multiple users to book the slot concurrently
DTo prevent other users from booking the same slot simultaneously
Step-by-Step Solution
Solution:
  1. Step 1: Understand Locking Purpose

    Locking prevents concurrent modifications to the slot's availability.
  2. Step 2: Prevent Race Conditions

    Without locking, multiple users could see the slot as available and book it simultaneously.
  3. Step 3: Ensure Consistency

    Locking ensures only one booking can succeed at a time.
  4. Final Answer:

    To prevent other users from booking the same slot simultaneously -> Option D
  5. Quick Check:

    Locking prevents simultaneous bookings [OK]
Quick Trick: Lock before availability check to avoid conflicts [OK]
Common Mistakes:
MISTAKES
  • Assuming locking speeds up booking
  • Allowing concurrent bookings without locks
  • Locking after checking availability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes