Bird
Raised Fist0

Identify the bug in the following availability check code:

medium📝 Analysis Q14 of Q15
LLD - Design — Hotel Booking System
Identify the bug in the following availability check code:
def is_available(stock, requested):
    if requested > stock:
        return True
    else:
        return False

print(is_available(5, 10))
AThe function should return False when requested is greater than stock
BThe function is correct and returns True
CThe condition should be 'requested <= stock' to return True
DThe function should compare 'stock > requested' instead
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition logic

    Current code returns True if requested > stock, meaning more requested than available stock.
  2. Step 2: Correct logic for availability

    Availability means stock should be enough or more than requested. So, if requested > stock, return False.
  3. Final Answer:

    The function should return False when requested is greater than stock -> Option A
  4. Quick Check:

    Requested > stock means not available [OK]
Quick Trick: Availability means stock >= requested, else False [OK]
Common Mistakes:
MISTAKES
  • Returning True when requested exceeds stock
  • Confusing greater than with less than
  • Not testing with example values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes