Design a micro-deposit bank account verification state machine

Quick Overview

Design a class that verifies bank account ownership with two micro-deposits, moving each account through pending, sent, verified and failed states. It tests state machine design, order-independent amount matching, attempt limits, and consistent error handling for unknown accounts and invalid calls.

Design a micro-deposit bank account verification state machine

Company: Square

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design and implement a bank account verification system using micro-deposits. This is a common pattern in merchant onboarding: a platform sends two small deposit amounts to a merchant's bank account, and the merchant must confirm the exact amounts to prove ownership. The system manages each bank account through a verification lifecycle with the following states: `PENDING` (account added, deposits not yet sent), `SENT` (deposits sent, awaiting verification), `VERIFIED` (merchant confirmed the correct amounts), and `FAILED` (maximum verification attempts exhausted). The system enforces strict state transitions. An account starts as `PENDING` and moves to `SENT` when deposits are dispatched. From `SENT` it becomes `VERIFIED` on a correct match, or stays `SENT` until its attempts run out, at which point it becomes `FAILED`. The only allowed transitions are `PENDING -> SENT`, `SENT -> VERIFIED` and `SENT -> FAILED`. The merchant may provide the two amounts in any order during verification. Implement a class `MicroDepositVerifier` with these methods: - `MicroDepositVerifier(maxAttempts: int)`: create the verifier. Every new account starts with `maxAttempts` verification attempts. - `addAccount(accountId: str) -> str`: add a new bank account for verification and return its current status. If the account already exists, return its existing status. Otherwise, create it in `PENDING` state. - `sendDeposits(accountId: str, amount1: int, amount2: int) -> str`: send two micro-deposit amounts to the account. Only valid when the account is in `PENDING` state. Return the new status, or an error string (`ACCOUNT_NOT_FOUND` or `INVALID_STATE`). - `verifyDeposits(accountId: str, amount1: int, amount2: int) -> str`: verify the micro-deposit amounts, which can be given in any order. On a match, the account moves to `VERIFIED`. On a mismatch, its remaining attempts go down by one, and it moves to `FAILED` if no attempts remain. Only valid when the account is in `SENT` state. Return the status after the call (`VERIFIED`, `SENT` or `FAILED`), or an error string (`ACCOUNT_NOT_FOUND` or `INVALID_STATE`). - `getStatus(accountId: str) -> str`: return the current verification status of the account, or `ACCOUNT_NOT_FOUND` if the account does not exist. - `getAttemptsLeft(accountId: str) -> int`: return the number of verification attempts remaining for the account, or `-1` if the account does not exist. A method checks for an unknown account first and returns `ACCOUNT_NOT_FOUND` before it looks at the state. A call that returns an error string changes nothing, so it does not use up an attempt. ```hint Model the lifecycle Before writing code, list which method is allowed in which state. Every call outside its allowed state, and every unknown account, should return an error and leave the account untouched. ``` ```hint Comparing the amounts The merchant can enter the amounts in either order. Make sure your comparison treats them as a pair of values that may repeat, so that entering one correct amount twice does not pass. ``` ### Examples #### Example 1 ```text Input: ["MicroDepositVerifier", "addAccount", "sendDeposits", "getStatus", "verifyDeposits", "getStatus"] [[3], ["acct_001"], ["acct_001", 12, 34], ["acct_001"], ["acct_001", 34, 12], ["acct_001"]] Output: [null, "PENDING", "SENT", "SENT", "VERIFIED", "VERIFIED"] ``` ```python verifier = MicroDepositVerifier(3) # max 3 verification attempts verifier.addAccount("acct_001") # "PENDING" (new account created) verifier.sendDeposits("acct_001", 12, 34) # "SENT" (deposits dispatched) verifier.getStatus("acct_001") # "SENT" (awaiting verification) verifier.verifyDeposits("acct_001", 34, 12) # "VERIFIED" (amounts match in another order) verifier.getStatus("acct_001") # "VERIFIED" ``` #### Example 2 ```text Input: ["MicroDepositVerifier", "addAccount", "verifyDeposits", "sendDeposits", "verifyDeposits", "getAttemptsLeft", "verifyDeposits", "getStatus", "verifyDeposits", "addAccount", "getAttemptsLeft"] [[2], ["acct_002"], ["acct_002", 5, 7], ["acct_002", 5, 7], ["acct_002", 5, 8], ["acct_002"], ["acct_002", 7, 6], ["acct_002"], ["acct_002", 5, 7], ["acct_002"], ["acct_404"]] Output: [null, "PENDING", "INVALID_STATE", "SENT", "SENT", 1, "FAILED", "FAILED", "INVALID_STATE", "FAILED", -1] ``` ```python verifier = MicroDepositVerifier(2) # max 2 verification attempts verifier.addAccount("acct_002") # "PENDING" verifier.verifyDeposits("acct_002", 5, 7) # "INVALID_STATE" (nothing sent yet, no attempt used) verifier.sendDeposits("acct_002", 5, 7) # "SENT" verifier.verifyDeposits("acct_002", 5, 8) # "SENT" (mismatch, 1 attempt left) verifier.getAttemptsLeft("acct_002") # 1 verifier.verifyDeposits("acct_002", 7, 6) # "FAILED" (mismatch, 0 attempts left) verifier.getStatus("acct_002") # "FAILED" verifier.verifyDeposits("acct_002", 5, 7) # "INVALID_STATE" (FAILED is final) verifier.addAccount("acct_002") # "FAILED" (existing account is not reset) verifier.getAttemptsLeft("acct_404") # -1 (unknown account) ``` ### Constraints & Assumptions - `1 <= maxAttempts <= 10` - Deposit amounts are positive integers: `1 <= amount <= 99`. - `accountId` is a non-empty string of letters, digits and underscores, unique per account. - At most `10^4` total operations. ### Clarifying Questions to Ask - What does `verifyDeposits` return in each case, and which error wins when an account is both unknown and in the wrong state? - Does a call made in the wrong state, such as verifying before deposits are sent, use up an attempt? - Can the two deposit amounts be equal? - Is `FAILED` final, or can an account be retried with new deposits? ### What a Strong Answer Covers - A per-account record (status, both amounts, attempts left) kept in a hash map, so every operation is constant time. - A state check in every mutating method, with unknown accounts handled before state and error calls leaving the record unchanged. - An order-independent comparison that treats the two amounts as a multiset, including equal amounts. - Attempt accounting that decrements only on a real mismatch in `SENT` and moves to `FAILED` exactly when the count reaches zero. - `VERIFIED` and `FAILED` as terminal states, with re-adding an account unable to reset it. ### Follow-up Questions - The platform should choose the amounts itself. How would you generate them, and how likely is a random guess to pass with 3 attempts? - Unverified deposits should expire after a fixed number of days. How would you add that without a background job? - Two verification requests for the same account arrive at the same moment on different servers. What can go wrong, and how do you prevent it? - Support staff want to restart verification for a `FAILED` account. Which transitions and stored fields change?

Overview: Design a class that verifies bank account ownership with two micro-deposits, moving each account through pending, sent, verified and failed states. It tests state machine design, order-independent amount matching, attempt limits, and consistent error handling for unknown accounts and invalid calls.

Square logo
Square
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a bank account verification system using micro-deposits. This is a common pattern in merchant onboarding: a platform sends two small deposit amounts to a merchant's bank account, and the merchant must confirm the exact amounts to prove ownership.

The system manages each bank account through a verification lifecycle with the following states: PENDING (account added, deposits not yet sent), SENT (deposits sent, awaiting verification), VERIFIED (merchant confirmed the correct amounts), and FAILED (maximum verification attempts exhausted).

The system enforces strict state transitions. An account starts as PENDING and moves to SENT when deposits are dispatched. From SENT it becomes VERIFIED on a correct match, or stays SENT until its attempts run out, at which point it becomes FAILED. The only allowed transitions are PENDING -> SENT, SENT -> VERIFIED and SENT -> FAILED. The merchant may provide the two amounts in any order during verification.

Implement a class MicroDepositVerifier with these methods:

  • MicroDepositVerifier(maxAttempts: int) : create the verifier. Every new account starts with maxAttempts verification attempts.
  • addAccount(accountId: str) -> str : add a new bank account for verification and return its current status. If the account already exists, return its existing status. Otherwise, create it in PENDING state.
  • sendDeposits(accountId: str, amount1: int, amount2: int) -> str : send two micro-deposit amounts to the account. Only valid when the account is in PENDING state. Return the new status, or an error string ( ACCOUNT_NOT_FOUND or INVALID_STATE ).
  • verifyDeposits(accountId: str, amount1: int, amount2: int) -> str : verify the micro-deposit amounts, which can be given in any order. On a match, the account moves to VERIFIED . On a mismatch, its remaining attempts go down by one, and it moves to FAILED if no attempts remain. Only valid when the account is in SENT state. Return the status after the call ( VERIFIED , SENT or FAILED ), or an error string ( ACCOUNT_NOT_FOUND or INVALID_STATE ).
  • getStatus(accountId: str) -> str : return the current verification status of the account, or ACCOUNT_NOT_FOUND if the account does not exist.
  • getAttemptsLeft(accountId: str) -> int : return the number of verification attempts remaining for the account, or -1 if the account does not exist.

A method checks for an unknown account first and returns ACCOUNT_NOT_FOUND before it looks at the state. A call that returns an error string changes nothing, so it does not use up an attempt.

Examples

Example 1

Input:
["MicroDepositVerifier", "addAccount", "sendDeposits", "getStatus", "verifyDeposits", "getStatus"]
[[3], ["acct_001"], ["acct_001", 12, 34], ["acct_001"], ["acct_001", 34, 12], ["acct_001"]]

Output:
[null, "PENDING", "SENT", "SENT", "VERIFIED", "VERIFIED"]
verifier = MicroDepositVerifier(3)              # max 3 verification attempts
verifier.addAccount("acct_001")                 # "PENDING" (new account created)
verifier.sendDeposits("acct_001", 12, 34)       # "SENT" (deposits dispatched)
verifier.getStatus("acct_001")                  # "SENT" (awaiting verification)
verifier.verifyDeposits("acct_001", 34, 12)     # "VERIFIED" (amounts match in another order)
verifier.getStatus("acct_001")                  # "VERIFIED"

Example 2

Input:
["MicroDepositVerifier", "addAccount", "verifyDeposits", "sendDeposits", "verifyDeposits", "getAttemptsLeft", "verifyDeposits", "getStatus", "verifyDeposits", "addAccount", "getAttemptsLeft"]
[[2], ["acct_002"], ["acct_002", 5, 7], ["acct_002", 5, 7], ["acct_002", 5, 8], ["acct_002"], ["acct_002", 7, 6], ["acct_002"], ["acct_002", 5, 7], ["acct_002"], ["acct_404"]]

Output:
[null, "PENDING", "INVALID_STATE", "SENT", "SENT", 1, "FAILED", "FAILED", "INVALID_STATE", "FAILED", -1]
verifier = MicroDepositVerifier(2)              # max 2 verification attempts
verifier.addAccount("acct_002")                 # "PENDING"
verifier.verifyDeposits("acct_002", 5, 7)       # "INVALID_STATE" (nothing sent yet, no attempt used)
verifier.sendDeposits("acct_002", 5, 7)         # "SENT"
verifier.verifyDeposits("acct_002", 5, 8)       # "SENT" (mismatch, 1 attempt left)
verifier.getAttemptsLeft("acct_002")            # 1
verifier.verifyDeposits("acct_002", 7, 6)       # "FAILED" (mismatch, 0 attempts left)
verifier.getStatus("acct_002")                  # "FAILED"
verifier.verifyDeposits("acct_002", 5, 7)       # "INVALID_STATE" (FAILED is final)
verifier.addAccount("acct_002")                 # "FAILED" (existing account is not reset)
verifier.getAttemptsLeft("acct_404")            # -1 (unknown account)

Constraints & Assumptions

  • 1 <= maxAttempts <= 10
  • Deposit amounts are positive integers: 1 <= amount <= 99 .
  • accountId is a non-empty string of letters, digits and underscores, unique per account.
  • At most 10^4 total operations.

Clarifying Questions to Ask Guidance

  • What does verifyDeposits return in each case, and which error wins when an account is both unknown and in the wrong state?
  • Does a call made in the wrong state, such as verifying before deposits are sent, use up an attempt?
  • Can the two deposit amounts be equal?
  • Is FAILED final, or can an account be retried with new deposits?

What a Strong Answer Covers Guidance

  • A per-account record (status, both amounts, attempts left) kept in a hash map, so every operation is constant time.
  • A state check in every mutating method, with unknown accounts handled before state and error calls leaving the record unchanged.
  • An order-independent comparison that treats the two amounts as a multiset, including equal amounts.
  • Attempt accounting that decrements only on a real mismatch in SENT and moves to FAILED exactly when the count reaches zero.
  • VERIFIED and FAILED as terminal states, with re-adding an account unable to reset it.

Follow-up Questions Guidance

  • The platform should choose the amounts itself. How would you generate them, and how likely is a random guess to pass with 3 attempts?
  • Unverified deposits should expire after a fixed number of days. How would you add that without a background job?
  • Two verification requests for the same account arrive at the same moment on different servers. What can go wrong, and how do you prevent it?
  • Support staff want to restart verification for a FAILED account. Which transitions and stored fields change?