Design a thread-safe sensor data aggregator with sliding-window min, max and average

Quick Overview

Design a thread-safe aggregator that ingests high-frequency sensor readings and reports the minimum, maximum and average over a sliding 5-second window with 1-second buckets. It tests bucketed sliding windows, lazy eviction, ring-buffer indexing, out-of-order readings and lock placement for concurrent readers and writers.

Design a thread-safe sensor data aggregator with sliding-window min, max and average

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a thread-safe sensor data aggregator that ingests high-frequency events (10,000+ events per second) and exposes aggregate statistics with low latency. The aggregator maintains the minimum, maximum and average of all sensor readings observed within a sliding 5-second window with 1-second granularity. Each whole second is a bucket. The window covers the five seconds from `latest - 4` through `latest`, where `latest` is the largest timestamp recorded so far. Buckets outside that range are evicted lazily on every read or write. Every reading has an integer `timestamp` (seconds since some epoch) and a floating-point `value`. A query issued when the window holds no readings returns `0.0` for all three statistics. Implement these methods: - `record(timestamp: int, value: float) -> None` records a reading with the given second-resolution timestamp and value. It advances the window when the timestamp is newer than any seen so far and evicts buckets that fall out of it. - `get_min() -> float` returns the minimum value across all readings in the current window, or `0.0` if the window is empty. - `get_max() -> float` returns the maximum value across all readings in the current window, or `0.0` if the window is empty. - `get_average() -> float` returns the arithmetic mean of all readings in the current window, or `0.0` if the window is empty. Example 1: ```text Input: ["SensorDataAggregator", "record", "record", "record", "get_min", "get_max", "get_average"] [[], [100, 10], [100, 20], [101, 30], [], [], []] Output: [null, null, null, null, 10.0, 30.0, 20.0] ``` Seconds 100 and 101 are both inside the window that ends at 101. Bucket 100 holds two readings (10 and 20) and bucket 101 holds one (30), so the minimum is 10, the maximum is 30 and the mean is (10 + 20 + 30) / 3 = 20. Example 2: ```text Input: ["SensorDataAggregator", "record", "record", "record", "get_average", "get_min", "get_max"] [[], [10, 5], [11, 15], [20, 100], [], [], []] Output: [null, null, null, null, 100.0, 100.0, 100.0] ``` Recording at second 20 moves the window to seconds 16-20, which evicts buckets 10 and 11. Only the reading of 100 at second 20 remains, so all three statistics are 100. ```hint What a bucket keeps Decide which few numbers each one-second bucket must store so that several buckets can be combined into a window minimum, maximum and mean without keeping the raw readings. ``` ```hint Shared slots If you index buckets by `timestamp % 5`, a stale second and a live second can map to the same slot. Work out how a slot knows which second it currently holds. ``` ### Constraints & Assumptions - `0 <= timestamp <= 10^9` - `-10^9 <= value <= 10^9` - Up to `10^5` operations per test. - The window length is fixed at 5 seconds with 1-second buckets. - All public methods must be thread-safe. - When the window holds no readings, `get_min`, `get_max` and `get_average` return `0.0`. ### Clarifying Questions to Ask - Can readings arrive out of order, and what should happen to a reading whose second has already left the window? - Is the workload mostly writes, or do reads arrive about as often as writes? That decides how much lock contention matters. - Should the statistics change as wall-clock time passes with no new readings, or only when a newer timestamp is recorded? - Is the window always 5 seconds, or should the design accept other window sizes? ### What a Strong Answer Covers - The per-second summary each bucket stores and why it is enough to combine buckets into an exact window minimum, maximum and mean. - How the window boundary follows the latest timestamp, including out-of-order readings and readings that arrive after their second has been evicted. - How the bucket storage tells a live second from a stale one, and how eviction resets it. - Where the lock goes and why the read methods need it as well as `record`. - Time and space cost per operation, and the empty-window case. ### Follow-up Questions - How would you change the design if the window were thousands of seconds long and reads still had to be fast? - At ten times the event rate, one lock becomes a point of contention. How would you reduce it while keeping the statistics exact? - How would you make the statistics fall to `0.0` when no readings arrive for more than 5 seconds of wall-clock time? - How would you add a p99 query over the same window?

Overview: Design a thread-safe aggregator that ingests high-frequency sensor readings and reports the minimum, maximum and average over a sliding 5-second window with 1-second buckets. It tests bucketed sliding windows, lazy eviction, ring-buffer indexing, out-of-order readings and lock placement for concurrent readers and writers.

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

Design a thread-safe sensor data aggregator that ingests high-frequency events (10,000+ events per second) and exposes aggregate statistics with low latency.

The aggregator maintains the minimum, maximum and average of all sensor readings observed within a sliding 5-second window with 1-second granularity. Each whole second is a bucket. The window covers the five seconds from latest - 4 through latest, where latest is the largest timestamp recorded so far. Buckets outside that range are evicted lazily on every read or write.

Every reading has an integer timestamp (seconds since some epoch) and a floating-point value. A query issued when the window holds no readings returns 0.0 for all three statistics.

Implement these methods:

  • record(timestamp: int, value: float) -> None records a reading with the given second-resolution timestamp and value. It advances the window when the timestamp is newer than any seen so far and evicts buckets that fall out of it.
  • get_min() -> float returns the minimum value across all readings in the current window, or 0.0 if the window is empty.
  • get_max() -> float returns the maximum value across all readings in the current window, or 0.0 if the window is empty.
  • get_average() -> float returns the arithmetic mean of all readings in the current window, or 0.0 if the window is empty.

Example 1:

Input:
["SensorDataAggregator", "record",  "record",  "record",  "get_min", "get_max", "get_average"]
[[],                     [100, 10], [100, 20], [101, 30], [],        [],        []]

Output: [null, null, null, null, 10.0, 30.0, 20.0]

Seconds 100 and 101 are both inside the window that ends at 101. Bucket 100 holds two readings (10 and 20) and bucket 101 holds one (30), so the minimum is 10, the maximum is 30 and the mean is (10 + 20 + 30) / 3 = 20.

Example 2:

Input:
["SensorDataAggregator", "record", "record", "record",  "get_average", "get_min", "get_max"]
[[],                     [10, 5],  [11, 15], [20, 100], [],            [],        []]

Output: [null, null, null, null, 100.0, 100.0, 100.0]

Recording at second 20 moves the window to seconds 16-20, which evicts buckets 10 and 11. Only the reading of 100 at second 20 remains, so all three statistics are 100.

Constraints & Assumptions

  • 0 <= timestamp <= 10^9
  • -10^9 <= value <= 10^9
  • Up to 10^5 operations per test.
  • The window length is fixed at 5 seconds with 1-second buckets.
  • All public methods must be thread-safe.
  • When the window holds no readings, get_min , get_max and get_average return 0.0 .

Clarifying Questions to Ask Guidance

  • Can readings arrive out of order, and what should happen to a reading whose second has already left the window?
  • Is the workload mostly writes, or do reads arrive about as often as writes? That decides how much lock contention matters.
  • Should the statistics change as wall-clock time passes with no new readings, or only when a newer timestamp is recorded?
  • Is the window always 5 seconds, or should the design accept other window sizes?

What a Strong Answer Covers Guidance

  • The per-second summary each bucket stores and why it is enough to combine buckets into an exact window minimum, maximum and mean.
  • How the window boundary follows the latest timestamp, including out-of-order readings and readings that arrive after their second has been evicted.
  • How the bucket storage tells a live second from a stale one, and how eviction resets it.
  • Where the lock goes and why the read methods need it as well as record .
  • Time and space cost per operation, and the empty-window case.

Follow-up Questions Guidance

  • How would you change the design if the window were thousands of seconds long and reads still had to be fast?
  • At ten times the event rate, one lock becomes a point of contention. How would you reduce it while keeping the statistics exact?
  • How would you make the statistics fall to 0.0 when no readings arrive for more than 5 seconds of wall-clock time?
  • How would you add a p99 query over the same window?