Track transaction statistics over a sliding 60-second window

Quick Overview

Design a class that records timestamped transactions and returns their sum, average, maximum, minimum and count over the last 60 seconds. It tests sliding-window data structures, expiry and boundary rules, out-of-order arrivals, and the cost of each operation.

Track transaction statistics over a sliding 60-second window

Company: N26

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a class that records financial transactions and exposes aggregate statistics over the most recent 60-second window. It is the logic behind a `GET /statistics` endpoint. Each transaction has an `amount` and a `timestamp` in milliseconds. A statistics query supplies a current time and returns the sum, average, maximum, minimum and count of all transactions whose timestamp falls within the last 60,000 ms of it. Transactions older than 60 seconds are expired and must be excluded from all statistics. Put precisely, a transaction counts toward a query at `currentTime` when `currentTime - 60000 <= timestamp <= currentTime`, so a transaction exactly 60,000 ms old still counts. Implement these methods: - `addTransaction(amount: float, timestamp: int)` records a transaction with the given amount and timestamp in milliseconds. - `getStatistics(currentTime: int) -> dict` returns a dict with keys `sum`, `avg`, `max`, `min` and `count`, aggregated over the transactions within the last 60,000 ms of `currentTime`. The money fields are rounded to 2 decimal places. If no transactions qualify, all numeric fields are 0. - `getCount(currentTime: int) -> int` returns the number of transactions within the last 60,000 ms of `currentTime`. In Example 1 all three transactions are inside the window: ```text Input: ["TransactionStatistics", "addTransaction", "addTransaction", "addTransaction", "getStatistics", "getCount"] [[], [100, 1000], [200, 30000], [300, 50000], [60000], [60000]] Output: [null, null, null, null, {"sum":600,"avg":200,"max":300,"min":100,"count":3}, 3] ``` ```text stats = TransactionStatistics() # empty tracker stats.addTransaction(100.0, 1000) # transaction at t = 1000 ms stats.addTransaction(200.0, 30000) # transaction at t = 30000 ms stats.addTransaction(300.0, 50000) # transaction at t = 50000 ms stats.getStatistics(60000) # {sum: 600, avg: 200, max: 300, min: 100, count: 3}, window is [0, 60000] stats.getCount(60000) # 3 ``` In Example 2 both transactions have expired by the first query: ```text Input: ["TransactionStatistics", "addTransaction", "addTransaction", "getStatistics", "getCount", "getStatistics"] [[], [50, 1000], [150, 5000], [70000], [70000], [120000]] Output: [null, null, null, {"sum":0,"avg":0,"max":0,"min":0,"count":0}, 0, {"sum":0,"avg":0,"max":0,"min":0,"count":0}] ``` ```text stats = TransactionStatistics() # empty tracker stats.addTransaction(50.0, 1000) # transaction at t = 1000 ms stats.addTransaction(150.0, 5000) # transaction at t = 5000 ms stats.getStatistics(70000) # all zeros, window is [10000, 70000] and both are older stats.getCount(70000) # 0 stats.getStatistics(120000) # all zeros, nothing in the window ``` At time 70000 the newer transaction is 65,000 ms old, so it has expired along with the one at 1000 ms. ```hint Where expired entries sit Think about the order in which transactions expire. If you keep them sorted by timestamp, all the expired ones end up in one place. ``` ```hint Boundaries and late arrivals Trace a transaction that is exactly 60,000 ms older than `currentTime`, and one that arrives with a timestamp earlier than the last one you stored. ``` ### Constraints & Assumptions - `0 <= amount <= 10^9` - `0 <= timestamp <= 10^12` (milliseconds since epoch) - At most `10^5` calls to `addTransaction`, `getStatistics` and `getCount` combined. ### Clarifying Questions to Ask - Can transactions arrive out of timestamp order, and if so, how late? - Can `currentTime` go backwards from one query to the next, or is it always the server clock? - Are reads much more frequent than writes, and is a scan of the live window per query acceptable? - Should amounts be stored exactly, as integer cents or a decimal type, or are floats acceptable? ### What a Strong Answer Covers - A structure that keeps transactions ordered by timestamp and removes expired ones cheaply, with the cost of each operation stated. - The exact window rule: the inclusive 60,000 ms boundary, transactions stamped after `currentTime`, and the all-zero result for an empty window. - Correct behavior when a transaction arrives out of timestamp order. - Memory that stays proportional to the live window instead of the full history. - How the design would change if queries had to be faster than a scan of the window. ### Follow-up Questions - `getStatistics` is called far more often than `addTransaction`. How would you answer it in $O(1)$ time? - How would you make the class safe when many threads add transactions and query at once? - The amounts are money. What goes wrong with floats, and what would you store instead? - The service runs on several instances, each receiving part of the traffic. How do you report one set of statistics?

Overview: Design a class that records timestamped transactions and returns their sum, average, maximum, minimum and count over the last 60 seconds. It tests sliding-window data structures, expiry and boundary rules, out-of-order arrivals, and the cost of each operation.

N26 logo
N26
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a class that records financial transactions and exposes aggregate statistics over the most recent 60-second window. It is the logic behind a GET /statistics endpoint.

Each transaction has an amount and a timestamp in milliseconds. A statistics query supplies a current time and returns the sum, average, maximum, minimum and count of all transactions whose timestamp falls within the last 60,000 ms of it. Transactions older than 60 seconds are expired and must be excluded from all statistics. Put precisely, a transaction counts toward a query at currentTime when currentTime - 60000 <= timestamp <= currentTime, so a transaction exactly 60,000 ms old still counts.

Implement these methods:

  • addTransaction(amount: float, timestamp: int) records a transaction with the given amount and timestamp in milliseconds.
  • getStatistics(currentTime: int) -> dict returns a dict with keys sum , avg , max , min and count , aggregated over the transactions within the last 60,000 ms of currentTime . The money fields are rounded to 2 decimal places. If no transactions qualify, all numeric fields are 0.
  • getCount(currentTime: int) -> int returns the number of transactions within the last 60,000 ms of currentTime .

In Example 1 all three transactions are inside the window:

Input:
["TransactionStatistics", "addTransaction", "addTransaction", "addTransaction", "getStatistics", "getCount"]
[[],                      [100, 1000],      [200, 30000],     [300, 50000],     [60000],         [60000]]

Output: [null, null, null, null, {"sum":600,"avg":200,"max":300,"min":100,"count":3}, 3]
stats = TransactionStatistics()      # empty tracker
stats.addTransaction(100.0, 1000)    # transaction at t = 1000 ms
stats.addTransaction(200.0, 30000)   # transaction at t = 30000 ms
stats.addTransaction(300.0, 50000)   # transaction at t = 50000 ms
stats.getStatistics(60000)           # {sum: 600, avg: 200, max: 300, min: 100, count: 3}, window is [0, 60000]
stats.getCount(60000)                # 3

In Example 2 both transactions have expired by the first query:

Input:
["TransactionStatistics", "addTransaction", "addTransaction", "getStatistics", "getCount", "getStatistics"]
[[],                      [50, 1000],       [150, 5000],      [70000],         [70000],    [120000]]

Output: [null, null, null, {"sum":0,"avg":0,"max":0,"min":0,"count":0}, 0, {"sum":0,"avg":0,"max":0,"min":0,"count":0}]
stats = TransactionStatistics()      # empty tracker
stats.addTransaction(50.0, 1000)     # transaction at t = 1000 ms
stats.addTransaction(150.0, 5000)    # transaction at t = 5000 ms
stats.getStatistics(70000)           # all zeros, window is [10000, 70000] and both are older
stats.getCount(70000)                # 0
stats.getStatistics(120000)          # all zeros, nothing in the window

At time 70000 the newer transaction is 65,000 ms old, so it has expired along with the one at 1000 ms.

Constraints & Assumptions

  • 0 <= amount <= 10^9
  • 0 <= timestamp <= 10^12 (milliseconds since epoch)
  • At most 10^5 calls to addTransaction , getStatistics and getCount combined.

Clarifying Questions to Ask Guidance

  • Can transactions arrive out of timestamp order, and if so, how late?
  • Can currentTime go backwards from one query to the next, or is it always the server clock?
  • Are reads much more frequent than writes, and is a scan of the live window per query acceptable?
  • Should amounts be stored exactly, as integer cents or a decimal type, or are floats acceptable?

What a Strong Answer Covers Guidance

  • A structure that keeps transactions ordered by timestamp and removes expired ones cheaply, with the cost of each operation stated.
  • The exact window rule: the inclusive 60,000 ms boundary, transactions stamped after currentTime , and the all-zero result for an empty window.
  • Correct behavior when a transaction arrives out of timestamp order.
  • Memory that stays proportional to the live window instead of the full history.
  • How the design would change if queries had to be faster than a scan of the window.

Follow-up Questions Guidance

  • getStatistics is called far more often than addTransaction . How would you answer it in O(1)O(1) time?
  • How would you make the class safe when many threads add transactions and query at once?
  • The amounts are money. What goes wrong with floats, and what would you store instead?
  • The service runs on several instances, each receiving part of the traffic. How do you report one set of statistics?