Bird
Raised Fist0
LLDsystem_design~10 mins

Reservation and hold system in LLD - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the method that reserves an item.

LLD
def reserve_item(self, item_id):
    if self.inventory.[1](item_id):
        self.inventory.remove(item_id)
        return True
    return False
Drag options to blanks, or click blank then click option'
Ais_available
Bcheck_availability
Chas_item
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that removes the item instead of checking availability.
2fill in blank
medium

Complete the code to hold an item for a user.

LLD
def hold_item(self, item_id, user_id):
    if self.inventory.[1](item_id):
        self.holds[item_id] = user_id
        return True
    return False
Drag options to blanks, or click blank then click option'
Ais_reserved
Bis_locked
Cis_held
Dis_available
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to hold an item that is already reserved or held.
3fill in blank
hard

Fix the error in the method that releases a hold on an item.

LLD
def release_hold(self, item_id):
    if item_id in self.holds:
        [1] self.holds[item_id]
        return True
    return False
Drag options to blanks, or click blank then click option'
Adelete
Bpop
Cremove
Ddiscard
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' which is a list method, not dictionary.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps item IDs to user IDs for all held items.

LLD
held_items = { [1]: [2] for [1], [2] in self.holds.items() }
Drag options to blanks, or click blank then click option'
Aitem_id
Buser_id
Ckey
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both key and value.
5fill in blank
hard

Fill all three blanks to filter held items where the user ID matches and the hold is still valid.

LLD
valid_holds = { [1]: [2] for [1], [2] in self.holds.items() if self.is_hold_valid([3]) }
Drag options to blanks, or click blank then click option'
Aitem_id
Buser_id
Dhold_id
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable to the validity check function.

Practice

(1/5)
1. What is the primary purpose of a hold in a reservation and hold system?
easy
A. To delete all reservations from the system
B. To permanently reserve a resource without expiration
C. To cancel a confirmed reservation immediately
D. To temporarily block a resource before final booking

Solution

  1. Step 1: Understand the role of a hold

    A hold temporarily blocks a resource to prevent others from booking it while the user decides.
  2. Step 2: Differentiate hold from reservation

    A reservation is permanent until canceled, while a hold expires if not confirmed.
  3. Final Answer:

    To temporarily block a resource before final booking -> Option D
  4. Quick Check:

    Hold = Temporary block [OK]
Hint: Holds are temporary blocks, not permanent reservations [OK]
Common Mistakes:
  • Confusing hold with permanent reservation
  • Thinking holds never expire
  • Assuming holds cancel reservations
2. Which data structure is best suited to track holds with expiration times efficiently?
easy
A. Simple array without ordering
B. Linked list without timestamps
C. Hash map with timestamps and a priority queue for expirations
D. Stack data structure

Solution

  1. Step 1: Identify requirements for hold tracking

    We need fast lookup by hold ID and efficient expiration handling.
  2. Step 2: Choose data structures

    A hash map allows quick hold lookup; a priority queue orders holds by expiration for timely removal.
  3. Final Answer:

    Hash map with timestamps and a priority queue for expirations -> Option C
  4. Quick Check:

    Hash map + priority queue = efficient hold tracking [OK]
Hint: Use hash map for lookup and priority queue for expirations [OK]
Common Mistakes:
  • Using unordered arrays causing slow expiration checks
  • Choosing stack which is LIFO, not suitable for expirations
  • Ignoring timestamps in data structure
3. Consider this pseudo-code for confirming a hold:
if hold.exists(hold_id) and not hold.is_expired(hold_id):
    reservation.create(hold.resource)
    hold.remove(hold_id)
    return "Confirmed"
else:
    return "Failed"
What will be the output if the hold has expired?
medium
A. "Failed"
B. "Confirmed"
C. Error due to missing hold
D. "Confirmed" but resource is double booked

Solution

  1. Step 1: Check hold existence and expiration

    The code confirms only if hold exists and is not expired.
  2. Step 2: Analyze expired hold case

    If hold is expired, condition fails and returns "Failed" without creating reservation.
  3. Final Answer:

    "Failed" -> Option A
  4. Quick Check:

    Expired hold = "Failed" confirmation [OK]
Hint: Expired holds cause confirmation to fail [OK]
Common Mistakes:
  • Assuming expired holds confirm successfully
  • Expecting errors instead of failure message
  • Ignoring hold expiration check
4. A developer wrote this code to release expired holds:
for hold in holds:
    if hold.expiration_time < current_time:
        holds.remove(hold)
What is the main issue with this code?
medium
A. Holds should not be removed, only marked expired
B. Modifying a list while iterating causes skipped elements or errors
C. Expiration time comparison is incorrect
D. Loop should use while instead of for

Solution

  1. Step 1: Understand iteration and modification

    Removing items from a list while iterating over it causes skipping or runtime errors.
  2. Step 2: Identify correct approach

    Use a separate list to collect expired holds or iterate over a copy to safely remove.
  3. Final Answer:

    Modifying a list while iterating causes skipped elements or errors -> Option B
  4. Quick Check:

    Remove during iteration = skipped elements [OK]
Hint: Never remove items from list while looping over it [OK]
Common Mistakes:
  • Ignoring iteration modification side effects
  • Assuming expiration comparison is wrong
  • Thinking loop type causes the issue
5. You need to design a scalable reservation and hold system for a popular event with thousands of simultaneous users. Which approach best ensures no double booking and timely hold expiration?
hard
A. Use distributed locks on resources, store holds with TTL in a distributed cache, and confirm with atomic transactions
B. Store all holds in a single database table without expiration, confirm by updating status
C. Allow multiple holds per resource and resolve conflicts manually later
D. Use client-side timers to expire holds and update server asynchronously

Solution

  1. Step 1: Prevent double booking with distributed locks

    Distributed locks ensure only one user can hold a resource at a time across servers.
  2. Step 2: Use TTL in distributed cache for hold expiration

    TTL automatically expires holds after timeout, preventing indefinite blocking.
  3. Step 3: Confirm holds atomically

    Atomic transactions guarantee reservation creation without race conditions.
  4. Final Answer:

    Use distributed locks on resources, store holds with TTL in a distributed cache, and confirm with atomic transactions -> Option A
  5. Quick Check:

    Distributed locks + TTL + atomic confirm = scalable, safe system [OK]
Hint: Combine distributed locks, TTL cache, and atomic confirm for scale [OK]
Common Mistakes:
  • Ignoring concurrency causing double booking
  • Relying on client-side expiration only
  • Not using atomic operations for confirmation