Design a Data Source Difference Tracker for Two Sensor Streams

Quick Overview

Design a tracker that records readings from two sensors at integer timestamps and reports the current, maximum and average absolute difference between them. It tests pairing readings by timestamp, constant-time queries, out-of-order input and the empty-state edge cases.

Design a Data Source Difference Tracker for Two Sensor Streams

Company: Citadel

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a data source difference tracker that monitors numeric readings from two independent sources (for example, a primary sensor `A` and a backup sensor `B`) recorded at integer timestamps. The tracker must support recording readings and answering queries about the absolute difference between the two sources, including the current difference, the maximum observed difference, and the average difference across timestamps where both sources have reported. Only timestamps where both sources have submitted a reading contribute to `maxDifference` and `averageDifference`. The `currentDifference` is based on the most recent reading of each source individually. Implement the following methods: - `recordA(timestamp: int, value: int)`: Record a reading from source A at the given timestamp. - `recordB(timestamp: int, value: int)`: Record a reading from source B at the given timestamp. - `currentDifference() -> float`: Return the absolute difference between the latest readings from A and B. Returns 0 if either source has no readings yet. - `maxDifference() -> float`: Return the maximum absolute difference observed at any timestamp where both sources have reported. Returns 0 if no such timestamp exists. - `averageDifference() -> float`: Return the average absolute difference across all timestamps where both sources have reported. Returns 0 if no such timestamp exists. Example 1: ```text Input: ["DifferenceTracker", "recordA", "recordB", "currentDifference", "recordA", "recordB", "maxDifference", "averageDifference"] [[], [1, 10], [1, 7], [], [2, 5], [2, 15], [], []] Output: [null, null, null, 3.0, null, null, 10.0, 6.5] Explanation: tracker = DifferenceTracker() # empty tracker tracker.recordA(1, 10) # A: {1→10}, latestA=10 tracker.recordB(1, 7) # B: {1→7}, pair at t=1: |10-7|=3 tracker.currentDifference() # → 3.0 (|10 - 7|) tracker.recordA(2, 5) # A: {1→10, 2→5}, latestA=5 tracker.recordB(2, 15) # pair at t=2: |5-15|=10 tracker.maxDifference() # → 10.0 (max of 3, 10) tracker.averageDifference() # → 6.5 ((3 + 10) / 2) ``` Example 2: ```text Input: ["DifferenceTracker", "currentDifference", "maxDifference", "recordA", "currentDifference", "recordB", "currentDifference", "averageDifference"] [[], [], [], [5, 100], [], [5, 80], [], []] Output: [null, 0.0, 0.0, null, 0.0, null, 20.0, 20.0] Explanation: tracker = DifferenceTracker() # empty tracker tracker.currentDifference() # → 0.0 (no readings yet) tracker.maxDifference() # → 0.0 (no paired timestamps) tracker.recordA(5, 100) # A: {5→100}, B still empty tracker.currentDifference() # → 0.0 (B has no readings) tracker.recordB(5, 80) # pair at t=5: |100-80|=20 tracker.currentDifference() # → 20.0 (|100 - 80|) tracker.averageDifference() # → 20.0 (only one paired timestamp) ``` ```hint When a timestamp pairs A timestamp joins the paired set at one moment only: when the second of the two sources reports it. Work out what you need to look up at that moment. ``` ```hint Query cost With up to 10⁵ calls, scanning every stored reading on each query is too slow. Ask which totals can be updated as each new pair forms. ``` ### Constraints & Assumptions - `1 <= timestamp <= 10⁹` - `-10⁹ <= value <= 10⁹` - At most `10⁵` calls in total to `recordA`, `recordB` and the three query methods. - Each `(source, timestamp)` pair is recorded at most once. - Query methods return `0` when no paired timestamps exist (for `maxDifference` and `averageDifference`) or when either source has no readings (for `currentDifference`). ### Clarifying Questions to Ask - Can readings arrive out of timestamp order? If so, is the "most recent" reading of a source the one with the largest timestamp or the one recorded last? - Should `averageDifference` be rounded to a fixed number of decimals, or returned at full floating-point precision? - Must the tracker keep unpaired readings forever, or is there a memory budget or retention window? - Will the tracker be called from more than one thread? ### What a Strong Answer Covers - How a timestamp gets paired exactly once, whichever source reports first, and what state makes that check cheap. - How `maxDifference` and `averageDifference` are answered without rescanning stored readings, with the time and space cost of every method. - A stated definition of the latest reading when timestamps arrive out of order, and how `currentDifference` uses it. - The zero-return cases: no readings at all, only one source reporting, and no paired timestamps. - The numeric range of differences and sums, and what it means for integer types and division outside Python. ### Follow-up Questions - How would you report the maximum and average difference over only the last `W` time units? - If a source may send a correction that overwrites an earlier reading, what changes in the aggregates, and which one becomes hard to maintain? - One sensor goes silent for a week while the other keeps reporting. How do you keep memory bounded? - How would you make the tracker safe to call from several threads?

Overview: Design a tracker that records readings from two sensors at integer timestamps and reports the current, maximum and average absolute difference between them. It tests pairing readings by timestamp, constant-time queries, out-of-order input and the empty-state edge cases.

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

Design a data source difference tracker that monitors numeric readings from two independent sources (for example, a primary sensor A and a backup sensor B) recorded at integer timestamps.

The tracker must support recording readings and answering queries about the absolute difference between the two sources, including the current difference, the maximum observed difference, and the average difference across timestamps where both sources have reported.

Only timestamps where both sources have submitted a reading contribute to maxDifference and averageDifference. The currentDifference is based on the most recent reading of each source individually.

Implement the following methods:

  • recordA(timestamp: int, value: int) : Record a reading from source A at the given timestamp.
  • recordB(timestamp: int, value: int) : Record a reading from source B at the given timestamp.
  • currentDifference() -> float : Return the absolute difference between the latest readings from A and B. Returns 0 if either source has no readings yet.
  • maxDifference() -> float : Return the maximum absolute difference observed at any timestamp where both sources have reported. Returns 0 if no such timestamp exists.
  • averageDifference() -> float : Return the average absolute difference across all timestamps where both sources have reported. Returns 0 if no such timestamp exists.

Example 1:

Input:
["DifferenceTracker", "recordA", "recordB", "currentDifference", "recordA", "recordB", "maxDifference", "averageDifference"]
[[],                  [1, 10],   [1, 7],    [],                  [2, 5],    [2, 15],   [],              []]

Output: [null, null, null, 3.0, null, null, 10.0, 6.5]

Explanation:
tracker = DifferenceTracker()             # empty tracker
tracker.recordA(1, 10)                    # A: {1→10}, latestA=10
tracker.recordB(1, 7)                     # B: {1→7},  pair at t=1: |10-7|=3
tracker.currentDifference()               # → 3.0     (|10 - 7|)
tracker.recordA(2, 5)                     # A: {1→10, 2→5}, latestA=5
tracker.recordB(2, 15)                    # pair at t=2: |5-15|=10
tracker.maxDifference()                   # → 10.0    (max of 3, 10)
tracker.averageDifference()               # → 6.5     ((3 + 10) / 2)

Example 2:

Input:
["DifferenceTracker", "currentDifference", "maxDifference", "recordA", "currentDifference", "recordB", "currentDifference", "averageDifference"]
[[],                  [],                  [],              [5, 100],  [],                  [5, 80],   [],                  []]

Output: [null, 0.0, 0.0, null, 0.0, null, 20.0, 20.0]

Explanation:
tracker = DifferenceTracker()             # empty tracker
tracker.currentDifference()               # → 0.0     (no readings yet)
tracker.maxDifference()                   # → 0.0     (no paired timestamps)
tracker.recordA(5, 100)                   # A: {5→100}, B still empty
tracker.currentDifference()               # → 0.0     (B has no readings)
tracker.recordB(5, 80)                    # pair at t=5: |100-80|=20
tracker.currentDifference()               # → 20.0    (|100 - 80|)
tracker.averageDifference()               # → 20.0    (only one paired timestamp)

Constraints & Assumptions

  • 1 <= timestamp <= 10⁹
  • -10⁹ <= value <= 10⁹
  • At most 10⁵ calls in total to recordA , recordB and the three query methods.
  • Each (source, timestamp) pair is recorded at most once.
  • Query methods return 0 when no paired timestamps exist (for maxDifference and averageDifference ) or when either source has no readings (for currentDifference ).

Clarifying Questions to Ask Guidance

  • Can readings arrive out of timestamp order? If so, is the "most recent" reading of a source the one with the largest timestamp or the one recorded last?
  • Should averageDifference be rounded to a fixed number of decimals, or returned at full floating-point precision?
  • Must the tracker keep unpaired readings forever, or is there a memory budget or retention window?
  • Will the tracker be called from more than one thread?

What a Strong Answer Covers Guidance

  • How a timestamp gets paired exactly once, whichever source reports first, and what state makes that check cheap.
  • How maxDifference and averageDifference are answered without rescanning stored readings, with the time and space cost of every method.
  • A stated definition of the latest reading when timestamps arrive out of order, and how currentDifference uses it.
  • The zero-return cases: no readings at all, only one source reporting, and no paired timestamps.
  • The numeric range of differences and sums, and what it means for integer types and division outside Python.

Follow-up Questions Guidance

  • How would you report the maximum and average difference over only the last W time units?
  • If a source may send a correction that overwrites an earlier reading, what changes in the aggregates, and which one becomes hard to maintain?
  • One sensor goes silent for a week while the other keeps reporting. How do you keep memory bounded?
  • How would you make the tracker safe to call from several threads?