Implement Accounts, Ranked Spending, Delayed Refunds, and Merge History
Company: Persona
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement an in-memory banking account system with timestamped operations. Process all refunds due at or before an operation's timestamp before executing that operation. Successful payments receive globally increasing IDs `payment1`, `payment2`, and so on.
### Constraints & Assumptions
Amounts and balances are integer units. Assume positive transaction amounts, nondecreasing operation timestamps, no reuse of an account ID after it has been merged away, and historical queries with `timeAt <= timestamp`. These details require confirmation because the source does not fix them. Express the 24-hour refund delay in the same units as timestamps.
### Clarifying Questions to Ask
- What timestamp unit represents the 24-hour delay?
- When several operations share a timestamp, should history reflect their final processed state?
- Can a removed account ID be recreated, requiring separate account lifetimes?
- After a merge, should completed payment records also become queryable under the surviving account? This practice solution assumes they do; the source specifies only pending-refund reassignment.
### Part 1 — Accounts and transfers
`createAccount(timestamp, accountId)` returns false if the account already exists and otherwise creates a zero-balance account. `deposit` returns an empty optional for a missing account, or the updated balance. `transfer` returns empty if either account is missing, the accounts are identical, or funds are insufficient; otherwise it moves the amount and returns the source's remaining balance.
#### What This Part Should Cover
- Validation before mutation and atomic updates of both balances.
- Separate active-account lookup and retained historical identity.
### Part 2 — Top spenders
`topSpenders(timestamp, n)` returns up to n active accounts, ranked by total outgoing amount descending and account ID lexicographically for ties, formatted as `accountId(totalOutgoing)`. Outgoing amount includes successful transfers and payments, not the number of transactions.
#### What This Part Should Cover
- Updating outgoing totals only after successful debits.
- Deterministic ordering and behavior when n exceeds the account count.
### Part 3 — Payments and refunds
`pay` returns empty for a missing account or insufficient funds. On success, deduct the amount, add it to outgoing spending, and schedule a refund of `floor(amount * 0.02)` after 24 hours. `getStatus` returns empty for a missing account, missing payment, or ownership mismatch; otherwise return `in progress` or `payment completed` depending on refund processing.
#### What This Part Should Cover
- A queue that retains every refund even when due times collide.
- Due refunds processed before all public operations, including reads.
- Stable payment IDs, ownership checks, and exactly-once refunds.
### Part 4 — Merges and historical balances
`mergeAccounts(timestamp, accountId1, accountId2)` merges account 2 into account 1, combining balances and outgoing totals, reassigning pending refunds, and removing account 2 from active use. Return false for missing or identical accounts.
`getBalance(timestamp, accountId, timeAt)` returns the account's historical balance if `timeAt` lies between its creation and merge time, inclusive. A merged-away account queried exactly at its merge timestamp returns its final balance. Return empty outside its lifetime.
#### What This Part Should Cover
- Preserving the removed account's final history rather than zeroing it.
- Redirecting payment ownership across repeated merges.
- Recording delayed refunds at their due times for accurate historical queries.
```hint Event time is not query time
A refund discovered during a later API call still changed the logical balance at its scheduled due time.
```
### What a Strong Answer Covers
- One coherent event-processing order shared by every API.
- Consistency among balances, outgoing totals, payment ownership, and account history.
- Tests for equal due times, merge chains, failed transfers, and inclusive merge-time queries.
### Follow-up Questions
- How would you support reusing a merged-away account ID without corrupting old payment ownership?
- Which operations need transaction boundaries if requests become concurrent?
Overview: Implement timestamped banking accounts with transfers, spending rankings, delayed refunds, account merges, and accurate inclusive historical balances.
Implement an in-memory banking account system with timestamped operations. Process all refunds due at or before an operation's timestamp before executing that operation. Successful payments receive globally increasing IDs payment1, payment2, and so on.
Constraints & Assumptions
Amounts and balances are integer units. Assume positive transaction amounts, nondecreasing operation timestamps, no reuse of an account ID after it has been merged away, and historical queries with timeAt <= timestamp. These details require confirmation because the source does not fix them. Express the 24-hour refund delay in the same units as timestamps.
Clarifying Questions to Ask Guidance
What timestamp unit represents the 24-hour delay?
When several operations share a timestamp, should history reflect their final processed state?
Can a removed account ID be recreated, requiring separate account lifetimes?
After a merge, should completed payment records also become queryable under the surviving account? This practice solution assumes they do; the source specifies only pending-refund reassignment.
Part 1 — Accounts and transfers
createAccount(timestamp, accountId) returns false if the account already exists and otherwise creates a zero-balance account. deposit returns an empty optional for a missing account, or the updated balance. transfer returns empty if either account is missing, the accounts are identical, or funds are insufficient; otherwise it moves the amount and returns the source's remaining balance.
What This Part Should Cover Guidance
Validation before mutation and atomic updates of both balances.
Separate active-account lookup and retained historical identity.
Part 2 — Top spenders
topSpenders(timestamp, n) returns up to n active accounts, ranked by total outgoing amount descending and account ID lexicographically for ties, formatted as accountId(totalOutgoing). Outgoing amount includes successful transfers and payments, not the number of transactions.
What This Part Should Cover Guidance
Updating outgoing totals only after successful debits.
Deterministic ordering and behavior when n exceeds the account count.
Part 3 — Payments and refunds
pay returns empty for a missing account or insufficient funds. On success, deduct the amount, add it to outgoing spending, and schedule a refund of floor(amount * 0.02) after 24 hours. getStatus returns empty for a missing account, missing payment, or ownership mismatch; otherwise return in progress or payment completed depending on refund processing.
What This Part Should Cover Guidance
A queue that retains every refund even when due times collide.
Due refunds processed before all public operations, including reads.
Stable payment IDs, ownership checks, and exactly-once refunds.
Part 4 — Merges and historical balances
mergeAccounts(timestamp, accountId1, accountId2) merges account 2 into account 1, combining balances and outgoing totals, reassigning pending refunds, and removing account 2 from active use. Return false for missing or identical accounts.
getBalance(timestamp, accountId, timeAt) returns the account's historical balance if timeAt lies between its creation and merge time, inclusive. A merged-away account queried exactly at its merge timestamp returns its final balance. Return empty outside its lifetime.
What This Part Should Cover Guidance
Preserving the removed account's final history rather than zeroing it.
Redirecting payment ownership across repeated merges.
Recording delayed refunds at their due times for accurate historical queries.
What a Strong Answer Covers Guidance
One coherent event-processing order shared by every API.
Consistency among balances, outgoing totals, payment ownership, and account history.
Tests for equal due times, merge chains, failed transfers, and inclusive merge-time queries.
Follow-up Questions Guidance
How would you support reusing a merged-away account ID without corrupting old payment ownership?
Which operations need transaction boundaries if requests become concurrent?