Transaction Aggregator: Highest-Volume Merchant in a 24-Hour Window
Company: Square
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a class that processes a stream of transaction objects and supports querying the merchant with the highest total transaction volume in the last 24 hours (86400 seconds).
Each transaction consists of a merchant name, a transaction amount, and a timestamp (in seconds since epoch). The system must be thread-safe, supporting concurrent reads and writes.
When querying the top merchant, only transactions whose timestamps fall within the half-open range `(queryTime - 86400, queryTime]` should be considered. If multiple merchants are tied for the highest volume, return the lexicographically smallest merchant name. If no transactions exist in the window, return an empty string.
Implement the following methods:
- `addTransaction(merchant: str, amount: int, timestamp: int)` adds a transaction for the given merchant with the specified amount at the given timestamp (seconds since epoch).
- `getTopMerchant(queryTime: int) -> str` returns the merchant with the highest total transaction volume in the 24-hour window ending at `queryTime`. If tied, it returns the lexicographically smallest name. If no transactions exist in the window, it returns an empty string.
Example 1:
```text
Input:
["TransactionAggregator", "addTransaction", "addTransaction", "addTransaction", "getTopMerchant", "addTransaction", "getTopMerchant", "getTopMerchant"]
[[], ["amazon", 500, 1000], ["google", 300, 2000], ["amazon", 200, 3000], [3000], ["google", 500, 4000], [4000], [90000]]
Output: [null, null, null, null, "amazon", null, "google", "google"]
Explanation:
agg = TransactionAggregator() # initialize aggregator
agg.addTransaction("amazon", 500, 1000) # amazon total: 500
agg.addTransaction("google", 300, 2000) # google total: 300
agg.addTransaction("amazon", 200, 3000) # amazon total: 700
agg.getTopMerchant(3000) # -> "amazon" (amazon=700, google=300)
agg.addTransaction("google", 500, 4000) # google total: 800
agg.getTopMerchant(4000) # -> "google" (google=800, amazon=700)
agg.getTopMerchant(90000) # -> "google" (window is (3600, 90000]; only google's 500 at t=4000 is left)
```
Example 2:
```text
Input:
["TransactionAggregator", "getTopMerchant", "addTransaction", "getTopMerchant", "getTopMerchant"]
[[], [100], ["shop", 100, 200], [200], [86700]]
Output: [null, "", null, "shop", ""]
Explanation:
agg = TransactionAggregator() # initialize aggregator
agg.getTopMerchant(100) # -> "" (no transactions yet)
agg.addTransaction("shop", 100, 200) # shop total: 100
agg.getTopMerchant(200) # -> "shop" (shop=100)
agg.getTopMerchant(86700) # -> "" (transaction at t=200 expired, 86700-86400=86300 >= 200)
```
```hint Sliding window
Timestamps never go backward, so the transactions that leave the window are always the oldest ones you still hold. Pick a structure that drops items from one end in constant time, and adjust each merchant's total as items leave.
```
```hint Locking pitfall
A query is not read-only here: it has to discard expired transactions. Account for that when you decide which operations need exclusive access.
```
### Constraints & Assumptions
- `1 <= amount <= 10^6`
- Timestamps are non-negative integers representing seconds since epoch.
- Merchant names are non-empty strings of lowercase English letters.
- `addTransaction` timestamps are non-decreasing.
- Calls arrive in time order: each `queryTime` is `>=` every previously added transaction timestamp and `>=` the previous `queryTime`.
- At most `10^5` total calls to `addTransaction` and `getTopMerchant`.
### Clarifying Questions to Ask
- Is one lock around both methods acceptable, or do queries need to run in parallel with each other and with writes?
- How many distinct merchants can be active in one window, and how often are queries issued compared with adds?
- Can two threads call `addTransaction` at nearly the same moment so that timestamps reach the class slightly out of order?
### What a Strong Answer Covers
- A layout that evicts expired transactions in amortized constant time and keeps each merchant's total in step with the window.
- The exact window boundary and the empty-window case.
- A deterministic tie-break when totals are equal, independent of hash map iteration order.
- A locking scheme that stays correct even though queries modify state, with a short argument for why.
- Time and space cost per operation, and how query cost grows with the number of distinct merchants.
### Follow-up Questions
- With many merchants and frequent queries, how would you make `getTopMerchant` faster than a scan over every merchant?
- What changes if racing writer threads can deliver transactions out of timestamp order?
- How would you return the top k merchants instead of one?
- How would you answer a query for an arbitrary earlier time, after later queries have already run?
Overview: Design a thread-safe class that ingests a stream of merchant transactions and returns the merchant with the highest total volume over a sliding 24-hour window, breaking ties by name. It tests sliding-window bookkeeping, per-key aggregation, locking when queries also modify state, and complexity trade-offs.