Design an account service with per-type fees and rewards using Factory and Strategy patterns

Quick Overview

Design an account service with STANDARD, METAL and BUSINESS account types, where a factory creates accounts and a strategy object applies each type's withdrawal fees and deposit rewards. It tests object-oriented design, the Factory and Strategy patterns, integer fee arithmetic and failure handling that leaves balances unchanged.

Design an account service with per-type fees and rewards using Factory and Strategy patterns

Company: N26

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an account management service that supports three account types: `STANDARD`, `METAL` and `BUSINESS`. Use the Factory pattern to create accounts and the Strategy pattern to apply each account type's fee and rewards rules. Each account type has its own rules: - `STANDARD` charges a flat fee of 1 unit per withdrawal or transfer and earns 1 reward point per 100 units deposited (integer division). - `METAL` charges no fee on withdrawals or transfers and earns 2 reward points per 100 units deposited. - `BUSINESS` charges 0.5% of the amount, floored to an integer, per withdrawal or transfer, with a minimum fee of 2. It earns 3 reward points per 100 units deposited. All monetary amounts are non-negative integers. A withdrawal or transfer must fail (return `false`) if the balance would become negative once the amount and the fee are taken out. Operations on unknown account IDs return `false` or `-1`, as listed below. Implement these methods: - `createAccount(accountId: str, accountType: str) -> bool` creates an account of the given type. It returns true on success, and false if the ID already exists or the type is invalid. - `deposit(accountId: str, amount: int) -> bool` adds the amount to the account and awards reward points under the account's strategy. It returns false if the account does not exist. - `withdraw(accountId: str, amount: int) -> bool` removes the amount plus the strategy's fee. It returns false if the account is missing or the funds are insufficient. - `transfer(fromId: str, toId: str, amount: int) -> bool` moves the amount from one account to another. The sender pays the fee under its own strategy. It returns false if either account is missing or the sender's funds are insufficient. - `getBalance(accountId: str) -> int` returns the current balance, or -1 if the account does not exist. - `getRewards(accountId: str) -> int` returns the accumulated reward points, or -1 if the account does not exist. For example: ```text createAccount("a", "STANDARD") -> true createAccount("b", "BUSINESS") -> true createAccount("a", "METAL") -> false (ID already exists) createAccount("c", "GOLD") -> false (unknown type) deposit("a", 250) -> true (a: balance 250, rewards 2) withdraw("a", 249) -> true (fee 1, a: balance 0) withdraw("a", 1) -> false (needs 2 including the fee) deposit("b", 1000) -> true (b: balance 1000, rewards 30) transfer("b", "a", 600) -> true (fee 3, b: balance 397) withdraw("b", 396) -> false (fee 2, needs 398) getBalance("a") -> 600 getBalance("b") -> 397 getRewards("b") -> 30 getBalance("x") -> -1 ``` The transfer costs `b` a fee of 3 because 0.5% of 600 is 3, which is above the minimum of 2. The last withdrawal fails because 396 plus the minimum fee of 2 is 398, one more than the 397 left in the account. ```hint Where the type rules live List what actually differs between the three account types. Only those values should depend on the type, and the service methods should never branch on the type string. ``` ```hint Order of checks In withdraw and transfer, work out the full amount that leaves the sender before changing any state, and keep the 0.5% calculation in integer arithmetic. ``` ### Constraints & Assumptions - `accountType` is expected to be `"STANDARD"`, `"METAL"` or `"BUSINESS"`. Any other string makes `createAccount` return false. - `accountId` is a non-empty string of at most 32 characters. - `0 <= amount <= 10^9` - At most `10^4` calls in total across all methods. - Calls to `createAccount` normally use fresh IDs, but the duplicate check is still part of the method contract. ### Clarifying Questions to Ask - Does money received through a transfer earn the receiver reward points, the same as a deposit would? - Are reward points computed per deposit, so two deposits of 50 into a `STANDARD` account earn nothing, or on the running total deposited? - Is the fee still charged on a withdrawal or transfer of 0? - What should a transfer from an account to itself do? ### What a Strong Answer Covers - A strategy interface with a fee method and a rewards method, one concrete class per account type, and a factory that maps the type string to a strategy and rejects unknown types. - A service that stores accounts by ID and hands all type-specific math to the account's strategy, so a new account type needs one new class and one factory entry. - Fee and reward arithmetic in integers that matches the rules, including the `BUSINESS` minimum fee, and a funds check against the amount plus the fee. - Failure paths (unknown IDs, duplicate IDs, insufficient funds) that leave every balance and reward total unchanged. - Constant time per operation, and balance storage that cannot overflow at the given limits. ### Follow-up Questions - How would you let fee rules and reward rules vary independently, for example a `METAL` fee schedule combined with `BUSINESS` rewards? - How would you keep transfers correct when many threads move money between the same accounts at once? - How would you add a transaction history and support reversing a transfer? - If rewards depended on how much an account deposited this month, how would the strategy interface change?

Overview: Design an account service with STANDARD, METAL and BUSINESS account types, where a factory creates accounts and a strategy object applies each type's withdrawal fees and deposit rewards. It tests object-oriented design, the Factory and Strategy patterns, integer fee arithmetic and failure handling that leaves balances unchanged.

N26 logo
N26
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an account management service that supports three account types: STANDARD, METAL and BUSINESS. Use the Factory pattern to create accounts and the Strategy pattern to apply each account type's fee and rewards rules.

Each account type has its own rules:

  • STANDARD charges a flat fee of 1 unit per withdrawal or transfer and earns 1 reward point per 100 units deposited (integer division).
  • METAL charges no fee on withdrawals or transfers and earns 2 reward points per 100 units deposited.
  • BUSINESS charges 0.5% of the amount, floored to an integer, per withdrawal or transfer, with a minimum fee of 2. It earns 3 reward points per 100 units deposited.

All monetary amounts are non-negative integers. A withdrawal or transfer must fail (return false) if the balance would become negative once the amount and the fee are taken out. Operations on unknown account IDs return false or -1, as listed below.

Implement these methods:

  • createAccount(accountId: str, accountType: str) -> bool creates an account of the given type. It returns true on success, and false if the ID already exists or the type is invalid.
  • deposit(accountId: str, amount: int) -> bool adds the amount to the account and awards reward points under the account's strategy. It returns false if the account does not exist.
  • withdraw(accountId: str, amount: int) -> bool removes the amount plus the strategy's fee. It returns false if the account is missing or the funds are insufficient.
  • transfer(fromId: str, toId: str, amount: int) -> bool moves the amount from one account to another. The sender pays the fee under its own strategy. It returns false if either account is missing or the sender's funds are insufficient.
  • getBalance(accountId: str) -> int returns the current balance, or -1 if the account does not exist.
  • getRewards(accountId: str) -> int returns the accumulated reward points, or -1 if the account does not exist.

For example:

createAccount("a", "STANDARD")   -> true
createAccount("b", "BUSINESS")   -> true
createAccount("a", "METAL")      -> false   (ID already exists)
createAccount("c", "GOLD")       -> false   (unknown type)
deposit("a", 250)                -> true    (a: balance 250, rewards 2)
withdraw("a", 249)               -> true    (fee 1, a: balance 0)
withdraw("a", 1)                 -> false   (needs 2 including the fee)
deposit("b", 1000)               -> true    (b: balance 1000, rewards 30)
transfer("b", "a", 600)          -> true    (fee 3, b: balance 397)
withdraw("b", 396)               -> false   (fee 2, needs 398)
getBalance("a")                  -> 600
getBalance("b")                  -> 397
getRewards("b")                  -> 30
getBalance("x")                  -> -1

The transfer costs b a fee of 3 because 0.5% of 600 is 3, which is above the minimum of 2. The last withdrawal fails because 396 plus the minimum fee of 2 is 398, one more than the 397 left in the account.

Constraints & Assumptions

  • accountType is expected to be "STANDARD" , "METAL" or "BUSINESS" . Any other string makes createAccount return false.
  • accountId is a non-empty string of at most 32 characters.
  • 0 <= amount <= 10^9
  • At most 10^4 calls in total across all methods.
  • Calls to createAccount normally use fresh IDs, but the duplicate check is still part of the method contract.

Clarifying Questions to Ask Guidance

  • Does money received through a transfer earn the receiver reward points, the same as a deposit would?
  • Are reward points computed per deposit, so two deposits of 50 into a STANDARD account earn nothing, or on the running total deposited?
  • Is the fee still charged on a withdrawal or transfer of 0?
  • What should a transfer from an account to itself do?

What a Strong Answer Covers Guidance

  • A strategy interface with a fee method and a rewards method, one concrete class per account type, and a factory that maps the type string to a strategy and rejects unknown types.
  • A service that stores accounts by ID and hands all type-specific math to the account's strategy, so a new account type needs one new class and one factory entry.
  • Fee and reward arithmetic in integers that matches the rules, including the BUSINESS minimum fee, and a funds check against the amount plus the fee.
  • Failure paths (unknown IDs, duplicate IDs, insufficient funds) that leave every balance and reward total unchanged.
  • Constant time per operation, and balance storage that cannot overflow at the given limits.

Follow-up Questions Guidance

  • How would you let fee rules and reward rules vary independently, for example a METAL fee schedule combined with BUSINESS rewards?
  • How would you keep transfers correct when many threads move money between the same accounts at once?
  • How would you add a transaction history and support reversing a transfer?
  • If rewards depended on how much an account deposited this month, how would the strategy interface change?