Design a transaction store with time-range totals and suspicious-window detection

Quick Overview

Design a class that records timestamped transactions per account, returns the total amount in an inclusive time range, and flags any fixed-length window whose total exceeds a threshold. It tests binary search over append-only sorted data, prefix sums, and a two-pointer sliding window with careful boundary handling.

Design a transaction store with time-range totals and suspicious-window detection

Company: Sap

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a data structure that records financial transactions for multiple accounts and supports efficient querying and anomaly detection. Each transaction has an account ID (string), a timestamp (non-negative integer) and an amount (positive integer). Transactions for the same account are recorded in non-decreasing order of timestamp. The system must support recording a transaction, returning the total amount an account transacted within an inclusive time range `[startTime, endTime]`, and detecting suspicious activity: whether some window of a given duration holds transactions whose total amount exceeds a threshold. Implement a class `FinancialStream` with these methods: - `record(account: str, timestamp: int, amount: int) -> None` records a transaction of `amount` for `account` at `timestamp`. - `query(account: str, startTime: int, endTime: int) -> int` returns the total transaction amount for the account between `startTime` and `endTime`, both inclusive. It returns 0 if the account has no transactions. - `isSuspicious(account: str, windowSize: int, threshold: int) -> bool` returns `True` if there is a window of the given duration in which the account's total exceeds `threshold`. A window covers timestamps `[t, t + windowSize - 1]` for some `t`. Example 1, as a list of calls and their results: ```text Input: ["FinancialStream", "record", "record", "record", "query", "isSuspicious", "query"] [[], ["acme", 10, 1000], ["acme", 20, 2000], ["acme", 25, 3000], ["acme", 10, 25], ["acme", 10, 4500], ["acme", 0, 100]] Output: [null, null, null, null, 6000, true, 6000] ``` The same calls written out: ```text fs = FinancialStream() fs.record("acme", 10, 1000) # acme: [(10,1000)] fs.record("acme", 20, 2000) # acme: [(10,1000),(20,2000)] fs.record("acme", 25, 3000) # acme: [(10,1000),(20,2000),(25,3000)] fs.query("acme", 10, 25) # -> 6000 (all three transactions) fs.isSuspicious("acme", 10, 4500) # -> True (window [20,29]: 2000+3000=5000 > 4500) fs.query("acme", 0, 100) # -> 6000 (all transactions in range) ``` Example 2, with a repeated timestamp and an account that has no transactions: ```text fs = FinancialStream() fs.record("bob", 5, 300) fs.record("bob", 5, 200) # same timestamp as the previous one fs.record("bob", 9, 400) fs.query("bob", 6, 9) # -> 400 (only the transaction at 9) fs.query("carol", 0, 10) # -> 0 (carol has no transactions) fs.isSuspicious("bob", 4, 800) # -> False (a window [t, t+3] cannot hold both 5 and 9; the best total is 300+200=500) fs.isSuspicious("bob", 5, 800) # -> True (window [5,9]: 300+200+400=900 > 800) ``` The second example shows why the window bounds matter: two transactions exactly `windowSize` apart never fall in the same window. ```hint Range totals Each account's transactions are already sorted by time. Think about how to find the first and last transaction inside `[startTime, endTime]` without scanning, and what you could store at `record` time so the sum itself is cheap. ``` ```hint Which windows to check Trying every start `t` up to ${10^9}$ is far too slow. Ask which windows can hold the largest total, and how the set of transactions in a window changes as you move through them in time order. ``` ### Constraints & Assumptions - `1 <= account.length <= 20` - `0 <= timestamp <= 10^9` - `1 <= amount <= 10^6` - For `query`: `startTime <= endTime` - For `isSuspicious`: `windowSize >= 1` and `threshold >= 1` - Transactions for the same account are recorded in non-decreasing order of timestamp. - At most `10^4` calls in total across all methods. ### Clarifying Questions to Ask - Can two transactions for the same account share a timestamp, and should both count toward a range or a window? - Does "exceeds the threshold" mean strictly greater than `threshold`? - What should `isSuspicious` return for an account that has never recorded a transaction? - Is the time ordering guaranteed only per account, so calls for different accounts can interleave in any order? - Which method is called most often, so I know whether to make `record`, `query` or `isSuspicious` the cheapest? ### What a Strong Answer Covers - A per-account store that keeps transactions in arrival order and uses the ordering guarantee instead of sorting. - How `query` finds both inclusive boundaries, and how to make the sum cheaper than adding up every transaction in range. - Which windows `isSuspicious` actually has to examine, with an argument for why that is enough, and a scan that is linear in the account's transactions. - Edge cases: the off-by-one in the window definition, repeated timestamps, unknown accounts, and totals that do not fit in a 32-bit integer. - The time and space cost of each method. ### Follow-up Questions - How would the design change if transactions for an account could arrive out of timestamp order? - If `windowSize` and `threshold` were fixed when the object is created, how would you flag an account as suspicious at the moment `record` is called? - How would you return the window with the largest total, or every window that exceeds the threshold? - The store grows without limit. How would you bound memory if queries only ever look at a recent retention period?

Overview: Design a class that records timestamped transactions per account, returns the total amount in an inclusive time range, and flags any fixed-length window whose total exceeds a threshold. It tests binary search over append-only sorted data, prefix sums, and a two-pointer sliding window with careful boundary handling.

Sap logo
Sap
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a data structure that records financial transactions for multiple accounts and supports efficient querying and anomaly detection.

Each transaction has an account ID (string), a timestamp (non-negative integer) and an amount (positive integer). Transactions for the same account are recorded in non-decreasing order of timestamp.

The system must support recording a transaction, returning the total amount an account transacted within an inclusive time range [startTime, endTime], and detecting suspicious activity: whether some window of a given duration holds transactions whose total amount exceeds a threshold.

Implement a class FinancialStream with these methods:

  • record(account: str, timestamp: int, amount: int) -> None records a transaction of amount for account at timestamp .
  • query(account: str, startTime: int, endTime: int) -> int returns the total transaction amount for the account between startTime and endTime , both inclusive. It returns 0 if the account has no transactions.
  • isSuspicious(account: str, windowSize: int, threshold: int) -> bool returns True if there is a window of the given duration in which the account's total exceeds threshold . A window covers timestamps [t, t + windowSize - 1] for some t .

Example 1, as a list of calls and their results:

Input:
["FinancialStream", "record",           "record",           "record",           "query",          "isSuspicious",     "query"]
[[],                ["acme", 10, 1000], ["acme", 20, 2000], ["acme", 25, 3000], ["acme", 10, 25], ["acme", 10, 4500], ["acme", 0, 100]]

Output: [null, null, null, null, 6000, true, 6000]

The same calls written out:

fs = FinancialStream()
fs.record("acme", 10, 1000)        # acme: [(10,1000)]
fs.record("acme", 20, 2000)        # acme: [(10,1000),(20,2000)]
fs.record("acme", 25, 3000)        # acme: [(10,1000),(20,2000),(25,3000)]
fs.query("acme", 10, 25)           # -> 6000 (all three transactions)
fs.isSuspicious("acme", 10, 4500)  # -> True (window [20,29]: 2000+3000=5000 > 4500)
fs.query("acme", 0, 100)           # -> 6000 (all transactions in range)

Example 2, with a repeated timestamp and an account that has no transactions:

fs = FinancialStream()
fs.record("bob", 5, 300)
fs.record("bob", 5, 200)           # same timestamp as the previous one
fs.record("bob", 9, 400)
fs.query("bob", 6, 9)              # -> 400 (only the transaction at 9)
fs.query("carol", 0, 10)           # -> 0 (carol has no transactions)
fs.isSuspicious("bob", 4, 800)     # -> False (a window [t, t+3] cannot hold both 5 and 9; the best total is 300+200=500)
fs.isSuspicious("bob", 5, 800)     # -> True (window [5,9]: 300+200+400=900 > 800)

The second example shows why the window bounds matter: two transactions exactly windowSize apart never fall in the same window.

Constraints & Assumptions

  • 1 <= account.length <= 20
  • 0 <= timestamp <= 10^9
  • 1 <= amount <= 10^6
  • For query : startTime <= endTime
  • For isSuspicious : windowSize >= 1 and threshold >= 1
  • Transactions for the same account are recorded in non-decreasing order of timestamp.
  • At most 10^4 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • Can two transactions for the same account share a timestamp, and should both count toward a range or a window?
  • Does "exceeds the threshold" mean strictly greater than threshold ?
  • What should isSuspicious return for an account that has never recorded a transaction?
  • Is the time ordering guaranteed only per account, so calls for different accounts can interleave in any order?
  • Which method is called most often, so I know whether to make record , query or isSuspicious the cheapest?

What a Strong Answer Covers Guidance

  • A per-account store that keeps transactions in arrival order and uses the ordering guarantee instead of sorting.
  • How query finds both inclusive boundaries, and how to make the sum cheaper than adding up every transaction in range.
  • Which windows isSuspicious actually has to examine, with an argument for why that is enough, and a scan that is linear in the account's transactions.
  • Edge cases: the off-by-one in the window definition, repeated timestamps, unknown accounts, and totals that do not fit in a 32-bit integer.
  • The time and space cost of each method.

Follow-up Questions Guidance

  • How would the design change if transactions for an account could arrive out of timestamp order?
  • If windowSize and threshold were fixed when the object is created, how would you flag an account as suspicious at the moment record is called?
  • How would you return the window with the largest total, or every window that exceeds the threshold?
  • The store grows without limit. How would you bound memory if queries only ever look at a recent retention period?