Implement a Banking System
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates skills in designing and implementing a stateful in-memory banking system, covering account modeling, transfer lifecycle and expiration handling, balance management, and tracking aggregate outgoing amounts.
Constraints
- 1 <= len(operations) <= 2000
- 0 <= timestamp <= 10^12, and timestamps are non-decreasing
- Account ids are non-empty strings
- 0 <= amount <= 10^9
- 0 <= n <= number of operations
- TRANSFER_EXPIRATION_MS = 86,400,000
- A transfer created at t expires before processing operations with timestamp > t + TRANSFER_EXPIRATION_MS
Examples
Input: ([('create_account', 1, 'alice'), ('create_account', 2, 'alice'), ('deposit', 3, 'bob', 50), ('create_account', 4, 'bob'), ('deposit', 5, 'alice', 100), ('deposit', 6, 'bob', 100), ('top_outgoing', 7, 3), ('top_outgoing', 8, 0)],)
Expected Output: [True, False, None, True, 100, 100, ['alice', 'bob'], []]
Explanation: Duplicate account creation fails, depositing to a missing account returns None, and accounts with equal outgoing totals are sorted lexicographically. Requesting the top 0 accounts returns an empty list.
Input: ([('create_account', 1, 'A'), ('create_account', 2, 'B'), ('deposit', 3, 'A', 500), ('transfer', 4, 'A', 'B', 200), ('top_outgoing', 5, 2), ('accept_transfer', 6, 'A', 'transfer1'), ('accept_transfer', 7, 'B', 'transfer1'), ('top_outgoing', 8, 2), ('deposit', 9, 'B', 50), ('accept_transfer', 10, 'B', 'transfer1')],)
Expected Output: [True, True, 500, 'transfer1', ['A', 'B'], False, True, ['A', 'B'], 250, False]
Explanation: The pending transfer does not count as outgoing before acceptance. Accepting with the wrong target fails. After B accepts, B receives 200 and A's outgoing total becomes 200. Accepting the same transfer again fails.
Hints
- Use dictionaries to store account state and transfer state. For each account, track both available balance and completed outgoing total.
- Remember that pending transfers withhold money immediately, but they only affect outgoing totals when accepted. Expiration and merging both need to update pending transfers carefully.