Bird
0
0

Given this order state machine code snippet:

medium📝 Analysis Q4 of 15
LLD - Design — Online Shopping Cart

Given this order state machine code snippet:

states = ['Pending', 'Processing', 'Shipped']
transitions = {('Pending', 'Processing'), ('Processing', 'Shipped')}
current_state = 'Pending'
next_state = 'Shipped'
if (current_state, next_state) in transitions:
    print('Valid transition')
else:
    print('Invalid transition')

What will be printed?

AInvalid transition
BRuntime error
CSyntax error
DValid transition
Step-by-Step Solution
Solution:
  1. Step 1: Check if ('Pending', 'Shipped') is in transitions

    Transitions only include ('Pending', 'Processing') and ('Processing', 'Shipped').
  2. Step 2: Determine if the condition is true

    Since ('Pending', 'Shipped') is not in transitions, the else branch runs.
  3. Final Answer:

    Invalid transition -> Option A
  4. Quick Check:

    Transition check = Invalid transition [OK]
Quick Trick: Check if tuple exists in transitions set [OK]
Common Mistakes:
  • Assuming direct jump allowed
  • Confusing tuple membership syntax
  • Ignoring transition set contents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes