Bird
Raised Fist0

Find the error in this transition check:

medium📝 Analysis Q7 of Q15
LLD - Design — Online Shopping Cart

Find the error in this transition check:

def can_transition(current, next_state):
    valid = {
        'Pending': ['Processing', 'Cancelled'],
        'Processing': ['Shipped']
    }
    return next_state in valid[current]

AReturns True for invalid transitions
BNo error, function works correctly
CKeyError if current state not in valid dictionary
DSyntax error due to missing colon
Step-by-Step Solution
Solution:
  1. Step 1: Check dictionary access

    Function accesses valid[current] without checking if current exists.
  2. Step 2: Identify potential error

    If current state is unknown, KeyError occurs.
  3. Final Answer:

    KeyError if current state not in valid dictionary -> Option C
  4. Quick Check:

    Missing key check causes KeyError [OK]
Quick Trick: Always check dictionary keys before access [OK]
Common Mistakes:
MISTAKES
  • Assuming all states exist in dictionary
  • Ignoring KeyError possibility
  • Confusing syntax errors with runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes