Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that removes the item instead of checking availability.
✗ Incorrect
The method is_available checks if the item is free to reserve.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to hold an item that is already reserved or held.
✗ Incorrect
The method is_available ensures the item is free before holding it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' which is a list method, not dictionary.
✗ Incorrect
The pop method removes the key and returns its value safely.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both key and value.
✗ Incorrect
Use item_id and user_id as variable names for keys and values.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable to the validity check function.
✗ Incorrect
The comprehension uses item_id and user_id for keys and values, and checks validity by passing item_id.
