Bird
0
0

Which code snippet correctly prevents race conditions when booking a test slot?

easy📝 Conceptual Q3 of 15
LLD - Design — Hotel Booking System
Which code snippet correctly prevents race conditions when booking a test slot?
Adef book(slot): acquire_lock(slot) if is_available(slot): reserve(slot) release_lock(slot)
Bdef book(slot): if is_available(slot): reserve(slot)
Cdef book(slot): reserve(slot) if is_available(slot): confirm(slot)
Ddef book(slot): check_availability(slot) reserve(slot)
Step-by-Step Solution
Solution:
  1. Step 1: Acquire Lock

    Locking the slot ensures no other process can modify availability concurrently.
  2. Step 2: Check Availability

    Check if the slot is free after acquiring the lock to avoid race conditions.
  3. Step 3: Reserve Slot

    Reserve the slot only if it is available.
  4. Step 4: Release Lock

    Release the lock to allow other operations.
  5. Final Answer:

    def book(slot): acquire_lock(slot) if is_available(slot): reserve(slot) release_lock(slot) -> Option A
  6. Quick Check:

    Lock before check prevents race conditions [OK]
Quick Trick: Always lock before checking availability [OK]
Common Mistakes:
  • Checking availability without locking
  • Reserving before checking availability
  • Not releasing locks after booking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes