Design a payment wallet class with validated deposits, withdrawals and error codes

Quick Overview

Implement a payment wallet class that stores an owner and an integer balance, with methods to add funds, deduct funds, check the balance and return the owner. It tests input validation, distinct error return codes, keeping the balance non-negative, and choosing an integer type that cannot overflow.

Design a payment wallet class with validated deposits, withdrawals and error codes

Company: PayPal

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a payment wallet class that manages one user's funds. The wallet is created with an owner name and an optional starting balance, which defaults to `0`. The wallet must support adding funds, checking the current balance, deducting funds, and retrieving the owner's name. All monetary operations must validate their inputs carefully: amounts must be positive integers, and a deduction must not exceed the available balance. When an operation fails because of invalid input, return `-1`. When a deduction fails because of insufficient funds, return `-2`. A successful monetary operation returns the new balance after the operation. Implement the following: - `PaymentWallet(owner: str, balance: int = 0)`: create a wallet for `owner` with the given starting balance. - `addBalance(amount: int) -> int`: add funds to the wallet. Returns the new balance on success, or `-1` if the amount is invalid (non-positive). - `checkBalance() -> int`: return the current balance of the wallet. - `deductBalance(amount: int) -> int`: deduct funds from the wallet. Returns the new balance on success, `-1` if the amount is invalid (non-positive), or `-2` if there are insufficient funds. - `getOwner() -> str`: return the owner name of the wallet. Example 1, a wallet with a starting balance: ```text Input: ["PaymentWallet", "getOwner", "addBalance", "deductBalance", "deductBalance", "deductBalance", "checkBalance"] [["Alice", 100], [], [50], [-5], [200], [120], []] Output: [null, "Alice", 150, -1, -2, 30, 30] Explanation: wallet = PaymentWallet("Alice", 100) # owner: Alice, balance: 100 wallet.getOwner() # -> "Alice" wallet.addBalance(50) # -> 150 (balance: 100 + 50 = 150) wallet.deductBalance(-5) # -> -1 (invalid amount, must be positive) wallet.deductBalance(200) # -> -2 (insufficient funds, 200 > 150) wallet.deductBalance(120) # -> 30 (balance: 150 - 120 = 30) wallet.checkBalance() # -> 30 ``` Example 2, a wallet that starts at the default balance: ```text Input: ["PaymentWallet", "checkBalance", "addBalance", "deductBalance", "addBalance", "deductBalance", "checkBalance"] [["Bob"], [], [0], [10], [500], [500], []] Output: [null, 0, -1, -2, 500, 0, 0] Explanation: wallet = PaymentWallet("Bob") # owner: Bob, balance: 0 (default) wallet.checkBalance() # -> 0 (starting balance) wallet.addBalance(0) # -> -1 (invalid amount, must be positive) wallet.deductBalance(10) # -> -2 (insufficient funds, 10 > 0) wallet.addBalance(500) # -> 500 (balance: 0 + 500 = 500) wallet.deductBalance(500) # -> 0 (balance: 500 - 500 = 0) wallet.checkBalance() # -> 0 ``` ```hint Balance range Work out the largest balance the constraints allow before you pick an integer type, and name the one fact about the balance that every method has to keep true. ``` ### Constraints & Assumptions - `owner` is a non-empty string. - The starting balance is between 0 and ${10^6}$, inclusive. - The `amount` passed to `addBalance` and `deductBalance` is between ${-10^6}$ and ${10^6}$; only positive values are valid. - At most ${10^4}$ calls are made to any method. ### Clarifying Questions to Ask - If the constructor receives a negative starting balance despite the constraints, should it clamp to 0, raise an error, or trust the caller? - Will one wallet be used from several threads at once, so that the funds check and the deduction need to be atomic? - Is the balance kept in whole units of the smallest currency denomination (such as cents), or can amounts be fractional? ### What a Strong Answer Covers - The state the class keeps and the invariant that holds after every call. - Validation that runs before any state changes, with the exact return code for each failure. - An integer type wide enough for the largest balance the constraints allow. - Time and space cost per operation. - Why sentinel return codes are safe here and what production code would use instead. ### Follow-up Questions - How would you make `deductBalance` safe when two threads withdraw from the same wallet at the same moment? - How would you add a transfer between two wallets that either fully succeeds or changes nothing, and avoid deadlock when two transfers run in opposite directions? - How would you keep a transaction history so that a balance can be audited or rebuilt?

Overview: Implement a payment wallet class that stores an owner and an integer balance, with methods to add funds, deduct funds, check the balance and return the owner. It tests input validation, distinct error return codes, keeping the balance non-negative, and choosing an integer type that cannot overflow.

PayPal logo
PayPal
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a payment wallet class that manages one user's funds. The wallet is created with an owner name and an optional starting balance, which defaults to 0.

The wallet must support adding funds, checking the current balance, deducting funds, and retrieving the owner's name. All monetary operations must validate their inputs carefully: amounts must be positive integers, and a deduction must not exceed the available balance.

When an operation fails because of invalid input, return -1. When a deduction fails because of insufficient funds, return -2. A successful monetary operation returns the new balance after the operation.

Implement the following:

  • PaymentWallet(owner: str, balance: int = 0) : create a wallet for owner with the given starting balance.
  • addBalance(amount: int) -> int : add funds to the wallet. Returns the new balance on success, or -1 if the amount is invalid (non-positive).
  • checkBalance() -> int : return the current balance of the wallet.
  • deductBalance(amount: int) -> int : deduct funds from the wallet. Returns the new balance on success, -1 if the amount is invalid (non-positive), or -2 if there are insufficient funds.
  • getOwner() -> str : return the owner name of the wallet.

Example 1, a wallet with a starting balance:

Input:
["PaymentWallet", "getOwner", "addBalance", "deductBalance", "deductBalance", "deductBalance", "checkBalance"]
[["Alice", 100],  [],         [50],         [-5],            [200],           [120],           []]

Output: [null, "Alice", 150, -1, -2, 30, 30]

Explanation:
wallet = PaymentWallet("Alice", 100)   # owner: Alice, balance: 100
wallet.getOwner()                      # -> "Alice"
wallet.addBalance(50)                  # -> 150   (balance: 100 + 50 = 150)
wallet.deductBalance(-5)               # -> -1    (invalid amount, must be positive)
wallet.deductBalance(200)              # -> -2    (insufficient funds, 200 > 150)
wallet.deductBalance(120)              # -> 30    (balance: 150 - 120 = 30)
wallet.checkBalance()                  # -> 30

Example 2, a wallet that starts at the default balance:

Input:
["PaymentWallet", "checkBalance", "addBalance", "deductBalance", "addBalance", "deductBalance", "checkBalance"]
[["Bob"],         [],             [0],          [10],            [500],        [500],           []]

Output: [null, 0, -1, -2, 500, 0, 0]

Explanation:
wallet = PaymentWallet("Bob")          # owner: Bob, balance: 0 (default)
wallet.checkBalance()                  # -> 0     (starting balance)
wallet.addBalance(0)                   # -> -1    (invalid amount, must be positive)
wallet.deductBalance(10)               # -> -2    (insufficient funds, 10 > 0)
wallet.addBalance(500)                 # -> 500   (balance: 0 + 500 = 500)
wallet.deductBalance(500)              # -> 0     (balance: 500 - 500 = 0)
wallet.checkBalance()                  # -> 0

Constraints & Assumptions

  • owner is a non-empty string.
  • The starting balance is between 0 and 106{10^6} , inclusive.
  • The amount passed to addBalance and deductBalance is between −106{-10^6} and 106{10^6} ; only positive values are valid.
  • At most 104{10^4} calls are made to any method.

Clarifying Questions to Ask Guidance

  • If the constructor receives a negative starting balance despite the constraints, should it clamp to 0, raise an error, or trust the caller?
  • Will one wallet be used from several threads at once, so that the funds check and the deduction need to be atomic?
  • Is the balance kept in whole units of the smallest currency denomination (such as cents), or can amounts be fractional?

What a Strong Answer Covers Guidance

  • The state the class keeps and the invariant that holds after every call.
  • Validation that runs before any state changes, with the exact return code for each failure.
  • An integer type wide enough for the largest balance the constraints allow.
  • Time and space cost per operation.
  • Why sentinel return codes are safe here and what production code would use instead.

Follow-up Questions Guidance

  • How would you make deductBalance safe when two threads withdraw from the same wallet at the same moment?
  • How would you add a transfer between two wallets that either fully succeeds or changes nothing, and avoid deadlock when two transfers run in opposite directions?
  • How would you keep a transaction history so that a balance can be audited or rebuilt?