Design a payment module with the Strategy pattern

Quick Overview

Design a payment processor that uses the Strategy pattern to support several built-in payment methods, register new range-based methods at runtime, and report the total processed per method. It tests object-oriented design, the open-closed principle and how failed payments are kept out of the totals.

Design a payment module with the Strategy pattern

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a payment module that supports multiple payment methods such as `CREDIT_CARD`, `PAYPAL`, `UPI` and `CRYPTO`. The module must use the Strategy pattern, so that the core processor delegates payment execution to an interchangeable strategy object, avoiding long `if-else` or `switch` ladders. Each strategy validates and processes a payment of a given `amount` and returns a status string. The processor should allow registering new payment strategies at runtime, switching the active method, and processing payments through whichever strategy is currently active. Adding a new payment type must not require changes to the processor itself. The processor also tracks the total amount successfully processed per payment method, for reporting. A payment is successful only when the amount is positive and the selected strategy approves it. The four built-in methods are available as soon as the processor is created: - `CREDIT_CARD` and `PAYPAL` approve a payment when `amount > 0`. - `UPI` approves a payment when `amount > 0` and `amount <= 100000`. - `CRYPTO` approves a payment when `amount > 0` and `amount >= 10`. Implement the following methods: - `set_method(method: str) -> bool`. Set the active payment method. Return `True` if the method is registered, otherwise `False`. - `pay(amount: int) -> str`. Process a payment of the given amount through the active strategy and return `"SUCCESS"` or `"FAILED"`. Return `"NO_METHOD"` if no active method is set. - `register_method(method: str, min_amount: int, max_amount: int) -> None`. Register a new custom payment strategy that approves payments where `min_amount <= amount <= max_amount`. - `get_total(method: str) -> int`. Return the total amount successfully processed through the given payment method. Return 0 if the method is unknown or has no successful payments. The first example switches between two built-in methods and shows a payment over the UPI limit failing: ```text Input: ["PaymentProcessor", "set_method", "pay", "pay", "set_method", "pay", "pay", "get_total", "get_total"] [[], ["CREDIT_CARD"], [100], [50], ["UPI"], [200], [200000], ["CREDIT_CARD"], ["UPI"]] Output: [null, true, "SUCCESS", "SUCCESS", true, "SUCCESS", "FAILED", 150, 200] Explanation: processor = PaymentProcessor() # registry: CREDIT_CARD, PAYPAL, UPI, CRYPTO processor.set_method('CREDIT_CARD') # -> True (active = CREDIT_CARD) processor.pay(100) # -> 'SUCCESS' (CREDIT_CARD total: 100) processor.pay(50) # -> 'SUCCESS' (CREDIT_CARD total: 150) processor.set_method('UPI') # -> True (active = UPI) processor.pay(200) # -> 'SUCCESS' (UPI total: 200) processor.pay(200000) # -> 'FAILED' (exceeds the UPI limit of 100000) processor.get_total('CREDIT_CARD') # -> 150 processor.get_total('UPI') # -> 200 ``` The second example pays before any method is active, then registers a new method at runtime: ```text Input: ["PaymentProcessor", "pay", "register_method", "set_method", "pay", "pay", "get_total"] [[], [100], ["GIFT_CARD", 1, 500], ["GIFT_CARD"], [250], [600], ["GIFT_CARD"]] Output: [null, "NO_METHOD", null, true, "SUCCESS", "FAILED", 250] Explanation: processor = PaymentProcessor() # no active method yet processor.pay(100) # -> 'NO_METHOD' (no strategy selected) processor.register_method('GIFT_CARD', 1, 500) # plug in a new strategy at runtime processor.set_method('GIFT_CARD') # -> True processor.pay(250) # -> 'SUCCESS' (within [1, 500]) processor.pay(600) # -> 'FAILED' (above the maximum) processor.get_total('GIFT_CARD') # -> 250 ``` ```hint Keep the processor generic List what the processor needs to know about a payment method in order to call it and report its total. Anything beyond that belongs to the method itself, and the processor should be able to find it by name. ``` ### Constraints & Assumptions - `1 <= amount <= 10^9` - Payment method identifiers are non-empty strings of length at most 32. - At most `10^4` total operations will be performed. ### Clarifying Questions to Ask - What should happen when `register_method` is called with a name that is already registered, such as a built-in method or the currently active one? - If `set_method` is called with an unknown method, should the previously active method stay active? - Can a registered range with `min_amount <= 0` approve a zero or negative amount, or does the positive-amount rule always apply? - Do strategies only approve or reject, or will they later call external payment gateways that can time out? ### What a Strong Answer Covers - The strategy interface, and how built-in and runtime-registered methods both implement it. - How the processor registers, selects and calls strategies without branching on the method type. - Where the positive-amount rule and the per-method totals live, and how failed and `NO_METHOD` payments stay out of the totals. - Behavior for unknown methods in `set_method` and `get_total`, and for re-registering an existing name. - The cost of each operation and the integer size the totals need. ### Follow-up Questions - How would you add a step that applies to every method, such as logging or a fee, without editing each strategy? - How would `pay` change if a strategy called an external gateway that can time out, and how would you avoid charging twice on a retry? - How would you unit test the processor without real payment providers? - How would you make the processor safe when many threads call `pay` and `set_method` at the same time?

Overview: Design a payment processor that uses the Strategy pattern to support several built-in payment methods, register new range-based methods at runtime, and report the total processed per method. It tests object-oriented design, the open-closed principle and how failed payments are kept out of the totals.

Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a payment module that supports multiple payment methods such as CREDIT_CARD, PAYPAL, UPI and CRYPTO. The module must use the Strategy pattern, so that the core processor delegates payment execution to an interchangeable strategy object, avoiding long if-else or switch ladders.

Each strategy validates and processes a payment of a given amount and returns a status string. The processor should allow registering new payment strategies at runtime, switching the active method, and processing payments through whichever strategy is currently active. Adding a new payment type must not require changes to the processor itself.

The processor also tracks the total amount successfully processed per payment method, for reporting. A payment is successful only when the amount is positive and the selected strategy approves it.

The four built-in methods are available as soon as the processor is created:

  • CREDIT_CARD and PAYPAL approve a payment when amount > 0 .
  • UPI approves a payment when amount > 0 and amount <= 100000 .
  • CRYPTO approves a payment when amount > 0 and amount >= 10 .

Implement the following methods:

  • set_method(method: str) -> bool . Set the active payment method. Return True if the method is registered, otherwise False .
  • pay(amount: int) -> str . Process a payment of the given amount through the active strategy and return "SUCCESS" or "FAILED" . Return "NO_METHOD" if no active method is set.
  • register_method(method: str, min_amount: int, max_amount: int) -> None . Register a new custom payment strategy that approves payments where min_amount <= amount <= max_amount .
  • get_total(method: str) -> int . Return the total amount successfully processed through the given payment method. Return 0 if the method is unknown or has no successful payments.

The first example switches between two built-in methods and shows a payment over the UPI limit failing:

Input:
["PaymentProcessor", "set_method",    "pay", "pay", "set_method", "pay", "pay",    "get_total",     "get_total"]
[[],                 ["CREDIT_CARD"], [100], [50],  ["UPI"],      [200], [200000], ["CREDIT_CARD"], ["UPI"]]

Output: [null, true, "SUCCESS", "SUCCESS", true, "SUCCESS", "FAILED", 150, 200]

Explanation:
processor = PaymentProcessor()              # registry: CREDIT_CARD, PAYPAL, UPI, CRYPTO
processor.set_method('CREDIT_CARD')         # -> True       (active = CREDIT_CARD)
processor.pay(100)                          # -> 'SUCCESS'  (CREDIT_CARD total: 100)
processor.pay(50)                           # -> 'SUCCESS'  (CREDIT_CARD total: 150)
processor.set_method('UPI')                 # -> True       (active = UPI)
processor.pay(200)                          # -> 'SUCCESS'  (UPI total: 200)
processor.pay(200000)                       # -> 'FAILED'   (exceeds the UPI limit of 100000)
processor.get_total('CREDIT_CARD')          # -> 150
processor.get_total('UPI')                  # -> 200

The second example pays before any method is active, then registers a new method at runtime:

Input:
["PaymentProcessor", "pay", "register_method",     "set_method",  "pay", "pay", "get_total"]
[[],                 [100], ["GIFT_CARD", 1, 500], ["GIFT_CARD"], [250], [600], ["GIFT_CARD"]]

Output: [null, "NO_METHOD", null, true, "SUCCESS", "FAILED", 250]

Explanation:
processor = PaymentProcessor()                       # no active method yet
processor.pay(100)                                   # -> 'NO_METHOD'  (no strategy selected)
processor.register_method('GIFT_CARD', 1, 500)       # plug in a new strategy at runtime
processor.set_method('GIFT_CARD')                    # -> True
processor.pay(250)                                   # -> 'SUCCESS'    (within [1, 500])
processor.pay(600)                                   # -> 'FAILED'     (above the maximum)
processor.get_total('GIFT_CARD')                     # -> 250

Constraints & Assumptions

  • 1 <= amount <= 10^9
  • Payment method identifiers are non-empty strings of length at most 32.
  • At most 10^4 total operations will be performed.

Clarifying Questions to Ask Guidance

  • What should happen when register_method is called with a name that is already registered, such as a built-in method or the currently active one?
  • If set_method is called with an unknown method, should the previously active method stay active?
  • Can a registered range with min_amount <= 0 approve a zero or negative amount, or does the positive-amount rule always apply?
  • Do strategies only approve or reject, or will they later call external payment gateways that can time out?

What a Strong Answer Covers Guidance

  • The strategy interface, and how built-in and runtime-registered methods both implement it.
  • How the processor registers, selects and calls strategies without branching on the method type.
  • Where the positive-amount rule and the per-method totals live, and how failed and NO_METHOD payments stay out of the totals.
  • Behavior for unknown methods in set_method and get_total , and for re-registering an existing name.
  • The cost of each operation and the integer size the totals need.

Follow-up Questions Guidance

  • How would you add a step that applies to every method, such as logging or a fee, without editing each strategy?
  • How would pay change if a strategy called an external gateway that can time out, and how would you avoid charging twice on a retry?
  • How would you unit test the processor without real payment providers?
  • How would you make the processor safe when many threads call pay and set_method at the same time?