Bird
0
0

A developer wrote this code to release expired holds:

medium📝 Analysis Q14 of 15
LLD - Design — Library Management System
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?
AHolds should not be removed, only marked expired
BModifying a list while iterating causes skipped elements or errors
CExpiration time comparison is incorrect
DLoop should use while instead of for
Step-by-Step Solution
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]
Quick Trick: Never remove items from list while looping over it [OK]
Common Mistakes:
MISTAKES
  • Ignoring iteration modification side effects
  • Assuming expiration comparison is wrong
  • Thinking loop type causes the issue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes