Implement banking, knapsack, and pagination tasks
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
## Problem A — Banking system command processor
Implement an in-memory banking system that processes a sequence of commands in timestamp order.
### Entities
- **Account** identified by a unique string `accountId`.
- **Balance** is a non-negative integer amount.
### Commands (examples shown; you may define an equivalent input format)
1. `CREATE accountId`
- Create a new account with balance 0.
- If the account already exists, the command should be handled deterministically (e.g., ignore or error).
2. `DEPOSIT accountId amount`
- Add `amount` to the account.
3. `TRANSFER fromId toId amount`
- Move funds from `fromId` to `toId` if sufficient funds exist.
4. `TOP_SPENDERS k`
- Return the top `k` accounts ranked by **total outgoing spend** (e.g., sum of successful `TRANSFER` amounts sent by the account). Define deterministic tie-breaking (e.g., by `accountId`).
5. `SCHEDULE_PAYMENT fromId toId amount executeAt`
- Schedule a delayed transfer to run at time `executeAt`.
- Scheduled payments should execute automatically at/after their scheduled time when processing later commands.
6. `CANCEL_PAYMENT paymentId`
- Cancel a previously scheduled payment if it has not executed.
7. `MERGE targetId sourceId`
- Merge `sourceId` into `targetId`:
- `targetId` receives `sourceId`’s remaining balance.
- Define what happens to scheduled payments involving `sourceId` (e.g., re-point to `targetId`, or cancel), and ensure the behavior is consistent.
8. `BALANCE accountId`
- Output the current balance.
### Output
Return outputs for query-like commands (e.g., `TOP_SPENDERS`, `BALANCE`, possibly errors) in the order encountered.
### Notes / constraints
- Assume up to large numbers of commands; aim for efficient data structures.
- Clearly define edge-case behavior (invalid accounts, negative amounts, cancel-after-execute, merge semantics, etc.).
---
## Problem B — Max-fee transaction packing (0/1 knapsack)
You are given `N` transactions, each with:
- `id` (unique identifier)
- `size` (positive integer)
- `fee` (non-negative integer)
You have a fixed **block size limit of 100** (i.e., total size of chosen transactions must be `<= 100`).
### Task
Choose a subset of transactions to **maximize total fee**.
### Output
- Return the maximum achievable total fee.
- If required, also return one optimal chosen set of `id`s with a deterministic tie-break rule (e.g., smallest number of transactions; then lexicographically smallest list of ids).
---
## Problem C — Design and implement a pagination utility
Design an interface and implement a pagination helper over an in-memory collection.
### Requirements
- Input: a list/array of items (you can assume strings or objects with `id`).
- Support `pageSize` and returning items page-by-page.
- Your design should specify function/class signatures (e.g., `getPage(pageNumber)`, or cursor-based `next(cursor)` returning `(items, nextCursor)`), including inputs/outputs.
### Constraints / considerations
- Handle edge cases (empty list, last page, invalid page token).
- Discuss tradeoffs between offset-based pagination and cursor-based pagination.
- If you choose cursor-based pagination, define how the cursor is generated and validated.
Quick Answer: This multi-part question evaluates stateful system design, ordered event processing and scheduling semantics for an in-memory banking command processor, combinatorial optimization and capacity-constrained selection for the 0/1 knapsack transaction packing, and API/interface design for pagination over in-memory collections.