Implement banking, knapsack, and pagination tasks
Problem A — In-Memory 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: A non-negative integer amount.
Commands (examples shown; you may define an equivalent input format)
CREATE accountId- Creates a new account with a balance of 0.
- If the account already exists, the command must be handled deterministically (e.g., ignore or error).
DEPOSIT accountId amount- Adds
amountto the specified account.amountis a non-negative integer.
- Adds
TRANSFER fromId toId amount- Moves
amountfromfromIdtotoIdiffromIdhas sufficient funds.amountis a non-negative integer.
- Moves
TOP_SPENDERS k- Returns the top
kaccounts ranked by total outgoing spend (sum of successfulTRANSFERamounts sent by the account). - Define a deterministic tie-breaking rule (e.g., by
accountId).
- Returns the top
SCHEDULE_PAYMENT fromId toId amount executeAt- Schedules a delayed transfer to run at the specified
executeAttimestamp. - Scheduled payments must execute automatically at or after their scheduled time when processing later commands.
- Schedules a delayed transfer to run at the specified
CANCEL_PAYMENT paymentId- Cancels a previously scheduled payment if it has not yet executed.
MERGE targetId sourceId- Merges
sourceIdintotargetId:targetIdreceivessourceId’s remaining balance.- Define what happens to scheduled payments involving
sourceId(e.g., re-point totargetId, or cancel), and ensure the behavior is consistent.
- Merges
BALANCE accountId- Outputs the current balance of the specified account.
Output
Return outputs for query-like commands (e.g., TOP_SPENDERS, BALANCE, and potentially errors) in the order they are encountered.
Notes / Constraints
- Assume up to large numbers of commands; aim for efficient data structures.
- Clearly define edge-case behavior (e.g., invalid accounts, negative amounts, cancelling an already executed payment, merge semantics).
Problem B — Max-Fee Transaction Packing (0/1 Knapsack)
You are given N transactions, each with a unique id, a positive integer size, and a non-negative integer fee.
You have a fixed block size limit of 100 (i.e., the total size of chosen transactions must be <= 100).
Task
Choose a subset of transactions to maximize the total fee.
Output
- Return the maximum achievable total fee.
- If required, also return one optimal chosen set of
ids 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 an
id). - Support
pageSizeand returning items page-by-page. - Your design should specify function/class signatures (e.g.,
getPage(pageNumber)or cursor-basednext(cursor)returning(items, nextCursor)), including inputs/outputs.
Constraints / Considerations
- Handle edge cases (e.g., 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.
AI interview prep
Core dilemma
Balancing efficient, ordered processing of diverse, state-modifying commands (including scheduled and merging operations) in an in-memory system, while maintaining data integrity, consistency, and query performance for dynamic aggregates like 'TOP_SPENDERS' under large scale.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement banking, knapsack, and pagination tasks
Problem A — In-Memory 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: A non-negative integer amount.
Commands (examples shown; you may define an equivalent input format)
CREATE accountId- Creates a new account with a balance of 0.
- If the account already exists, the command must be handled deterministically (e.g., ignore or error).
DEPOSIT accountId amount- Adds
amountto the specified account.amountis a non-negative integer.
- Adds
TRANSFER fromId toId amount- Moves
amountfromfromIdtotoIdiffromIdhas sufficient funds.amountis a non-negative integer.
- Moves
TOP_SPENDERS k- Returns the top
kaccounts ranked by total outgoing spend (sum of successfulTRANSFERamounts sent by the account). - Define a deterministic tie-breaking rule (e.g., by
accountId).
- Returns the top
SCHEDULE_PAYMENT fromId toId amount executeAt- Schedules a delayed transfer to run at the specified
executeAttimestamp. - Scheduled payments must execute automatically at or after their scheduled time when processing later commands.
- Schedules a delayed transfer to run at the specified
CANCEL_PAYMENT paymentId- Cancels a previously scheduled payment if it has not yet executed.
MERGE targetId sourceId- Merges
sourceIdintotargetId:targetIdreceivessourceId’s remaining balance.- Define what happens to scheduled payments involving
sourceId(e.g., re-point totargetId, or cancel), and ensure the behavior is consistent.
- Merges
BALANCE accountId- Outputs the current balance of the specified account.
Output
Return outputs for query-like commands (e.g., TOP_SPENDERS, BALANCE, and potentially errors) in the order they are encountered.
Notes / Constraints
- Assume up to large numbers of commands; aim for efficient data structures.
- Clearly define edge-case behavior (e.g., invalid accounts, negative amounts, cancelling an already executed payment, merge semantics).
Problem B — Max-Fee Transaction Packing (0/1 Knapsack)
You are given N transactions, each with a unique id, a positive integer size, and a non-negative integer fee.
You have a fixed block size limit of 100 (i.e., the total size of chosen transactions must be <= 100).
Task
Choose a subset of transactions to maximize the total fee.
Output
- Return the maximum achievable total fee.
- If required, also return one optimal chosen set of
ids 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 an
id). - Support
pageSizeand returning items page-by-page. - Your design should specify function/class signatures (e.g.,
getPage(pageNumber)or cursor-basednext(cursor)returning(items, nextCursor)), including inputs/outputs.
Constraints / Considerations
- Handle edge cases (e.g., 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.