LLD - Design — Hotel Booking SystemWhich 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)Check Answer
Step-by-Step SolutionSolution:Step 1: Acquire LockLocking the slot ensures no other process can modify availability concurrently.Step 2: Check AvailabilityCheck if the slot is free after acquiring the lock to avoid race conditions.Step 3: Reserve SlotReserve the slot only if it is available.Step 4: Release LockRelease the lock to allow other operations.Final Answer:def book(slot): acquire_lock(slot) if is_available(slot): reserve(slot) release_lock(slot) -> Option AQuick Check:Lock before check prevents race conditions [OK]Quick Trick: Always lock before checking availability [OK]Common Mistakes:MISTAKESChecking availability without lockingReserving before checking availabilityNot releasing locks after booking
Master "Design — Hotel Booking System" in LLD9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepArchTryChallengeDesignRecallScale
More LLD Quizzes Advanced LLD Concepts - Dependency injection framework - Quiz 14medium Advanced LLD Concepts - Code review checklist for LLD - Quiz 8hard Advanced LLD Concepts - Clean Architecture layers - Quiz 9hard Design — Chess Game - Special moves (castling, en passant) - Quiz 2easy Design — Food Delivery System - Delivery agent assignment - Quiz 11easy Design — Food Delivery System - Restaurant, Menu, Order classes - Quiz 6medium Design — Hotel Booking System - Booking conflict resolution - Quiz 7medium Design — Online Shopping Cart - Order state machine - Quiz 9hard Design — Online Shopping Cart - Notification on state change - Quiz 3easy Design — Splitwise (Expense Sharing) - Simplify debts algorithm - Quiz 15hard