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:Checking 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 - Immutability for safety - Quiz 5medium Design — Chess Game - Why chess tests polymorphism and strategy - Quiz 10hard Design — Chess Game - Piece movement rules (polymorphism) - Quiz 15hard Design — Chess Game - Observer pattern for UI updates - Quiz 1easy Design — Food Delivery System - Why delivery systems test service coordination - Quiz 6medium Design — Hotel Booking System - Cancellation and refund policy - Quiz 12easy Design — Online Shopping Cart - Notification on state change - Quiz 8hard Design — Online Shopping Cart - Notification on state change - Quiz 13medium Design — Splitwise (Expense Sharing) - Balance calculation algorithm - Quiz 8hard Design — Splitwise (Expense Sharing) - Balance calculation algorithm - Quiz 2easy