Bird
Raised Fist0

Which design ensures scalability and prevents invalid transitions?Choose the best approach:Use a dictionary mapping each state to allowed next states.Hardcode all transitions in if-else blocks.Allow any state to transition to any other state.Use a single variable without validation.

hard📝 Trade-off Q15 of Q15
LLD - Design — Online Shopping Cart

You are designing an order state machine for an online store. The order states are Pending, Confirmed, Shipped, Delivered, and Cancelled. Which design ensures scalability and prevents invalid transitions?

Choose the best approach:

  1. Use a dictionary mapping each state to allowed next states.
  2. Hardcode all transitions in if-else blocks.
  3. Allow any state to transition to any other state.
  4. Use a single variable without validation.
AUse a single variable without validation
BUse a dictionary mapping each state to allowed next states
CAllow any state to transition to any other state
DHardcode all transitions in if-else blocks
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate scalability and validation needs

    Hardcoding transitions is error-prone and hard to maintain; allowing any transition breaks rules.
  2. Step 2: Choose dictionary mapping

    Mapping states to allowed next states centralizes rules, making it easy to update and validate transitions.
  3. Final Answer:

    Use a dictionary mapping each state to allowed next states -> Option B
  4. Quick Check:

    Dictionary mapping = scalable, validated transitions [OK]
Quick Trick: Map states to allowed next states for clean validation [OK]
Common Mistakes:
MISTAKES
  • Hardcoding transitions everywhere
  • Skipping validation of transitions
  • Allowing invalid state jumps

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes