cswork / libraryOpen practice workspace

Coding questions · Coinbase

Implement a Simple Banking Ledger

This question evaluates competence in designing an efficient, stateful ledger API and associated data structures, emphasizing correctness under invalid inputs, performance at scale, integer overflow handling, unit testing for edge cases, and transactional (atomic batch) behavior.

Design and implement a banking ledger for n accounts labeled 1..n with initial balances. Provide an API: deposit(id, amount) -> bool, withdraw(id, amount) -> bool, transfer(fromId, toId, amount) -> bool. Rules: return false for invalid account IDs or insufficient funds; otherwise apply updates and return true. Constraints: up to 200,000 operations, amounts up to 10^12, target O(1) per operation and O(n) space; assume single-threaded execution. Sub-questions: write unit tests for edge cases (invalid IDs, zero/negative amounts, overflow), extend to support atomic batches of operations with rollback on partial failure, and analyze time/space complexity and common pitfalls (e.g., integer overflow).

Simple Banking Ledger

Process deposit, withdraw, and transfer operations on 1-indexed accounts with validation.

Constraints

  • Accounts are labeled 1..n
  • Amounts must be positive

Examples

Example 1

Input: ([100, 50], [('deposit', 1, 25), ('withdraw', 2, 70), ('transfer', 1, 2, 50)])
Output: {'results': [True, False, True], 'balances': [75, 100]}

Explanation: Valid and invalid operations.

Example 2

Input: ([0], [('withdraw', 1, 1), ('deposit', 2, 5), ('deposit', 1, 0)])
Output: {'results': [False, False, False], 'balances': [0]}

Explanation: Invalid ids and nonpositive amounts.

Hints

  1. Validate before mutating so failed operations leave balances unchanged.