Bird
Raised Fist0

What is the expected time complexity of processing a payment when using the Strategy Pattern with a factory method that selects the appropriate payment strategy at runtime?

medium📊 Complexity Q5 of Q15
OOP & Design Patterns - Strategy Pattern - Replace Conditionals with Polymorphism
What is the expected time complexity of processing a payment when using the Strategy Pattern with a factory method that selects the appropriate payment strategy at runtime?
AO(1)
BO(n) where n is number of payment methods
CO(log n) due to binary search in strategy selection
DO(n^2) due to nested conditional checks
Step-by-Step Solution
Solution:
  1. Step 1: Understand strategy selection mechanism

    The factory method typically uses a map/dictionary lookup to select the strategy.
  2. Step 2: Analyze time complexity

    Lookup and payment processing are constant time operations.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Factory lookup is constant time [OK]
Quick Trick: Factory uses map lookup, so constant time [OK]
Common Mistakes:
MISTAKES
  • Assuming linear search over all strategies
  • Confusing with conditional chains causing O(n)
  • Thinking strategy execution is complex
Trap Explanation:
PITFALL
  • Mistaking factory lookup for linear search inflates complexity
Interviewer Note:
CONTEXT
  • Tests understanding of runtime strategy selection efficiency
Master "Strategy Pattern - Replace Conditionals with Polymorphism" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes