Implement deposit, withdraw, and transfer in a class
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's competence in concurrent programming, atomic state updates, and class-level account management for basic monetary operations.
Constraints
- 0 <= len(operations) <= 200000
- 1 <= amount <= 10^9 for deposit, withdraw, and transfer operations
- Account IDs are strings
- All operation names are valid and all amounts are positive integers
Examples
Input: [('deposit', 'A', 100), ('deposit', 'B', 40), ('transfer', 'A', 'B', 30), ('getBalance', 'A'), ('getBalance', 'B'), ('withdraw', 'B', 50), ('getBalance', 'B')]
Expected Output: [100, 40, True, 70, 70, True, 20]
Explanation: A gets 100, B gets 40, then 30 is transferred from A to B. After that B successfully withdraws 50, leaving balance 20.
Input: [('withdraw', 'X', 10), ('getBalance', 'X'), ('transfer', 'X', 'Y', 5), ('getBalance', 'Y')]
Expected Output: [False, 0, False, 0]
Explanation: Nonexistent accounts start with balance 0. The withdraw fails, the transfer also fails, and Y still has 0.
Hints
- Use a hash map (dictionary) from account ID to current balance so each operation can be processed in O(1) average time.
- For a transfer, first check whether the source account has enough money. Only update balances if the transfer can fully succeed.