Design an in-memory request metrics store with range queries and p90 latency

Quick Overview

Design an in-memory request metrics store that records out-of-order events and answers count, average latency, throughput, error rate and nearest-rank p90 latency over half-open time ranges. It tests sorted storage with binary search, exact ratio arithmetic, and careful handling of duplicates, boundaries and empty ranges.

Design an in-memory request metrics store with range queries and p90 latency

Company: CRED

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory store for request metrics. Each recorded event has a `timestamp` in seconds, a `latency` in milliseconds and a `success` flag. Events are immutable, may arrive in any order, and events with equal timestamps (or fully identical events) each count separately. Old events never expire. Every query takes a half-open range `[start, end)` and includes exactly the events with `start <= timestamp < end`. Queries report the event count, the average latency (total latency divided by count), the throughput (count divided by `end - start`, in requests per second, even if some seconds are idle), the error rate (failed count divided by total count, as a fraction) and the p90 latency, defined as the latency at 1-based rank `ceil(0.9 * count)` in ascending order. For an empty range, count, average latency, throughput and error rate are all 0, and p90 latency is -1. Return ratios using double-precision floating-point arithmetic without rounding intermediate or final results. Implement the following methods: - `recordEvent(timestamp: int, latency: int, success: bool)`: record one immutable request event. Events may arrive in any order and duplicates count separately. - `count(start: int, end: int) -> int`: return the number of events with `start <= timestamp < end`. - `averageLatency(start: int, end: int) -> float`: return total latency divided by event count in the range, or 0 if the range is empty. - `throughput(start: int, end: int) -> float`: return event count divided by `end - start` in requests per second, or 0 if the range is empty. - `errorRate(start: int, end: int) -> float`: return failed event count divided by total event count as a fraction, or 0 if the range is empty. - `p90Latency(start: int, end: int) -> int`: return the latency at 1-based rank `ceil(0.9 * count)` in ascending order, or -1 if the range is empty. For example, after these calls: ```text recordEvent(10, 100, True) recordEvent(12, 300, False) recordEvent(10, 100, True) recordEvent(15, 50, True) recordEvent(11, 200, True) ``` the queries return: ```text count(10, 15) -> 4 averageLatency(10, 15) -> 175.0 throughput(10, 15) -> 0.8 errorRate(10, 15) -> 0.25 p90Latency(10, 15) -> 300 count(15, 16) -> 1 p90Latency(20, 30) -> -1 ``` The range `[10, 15)` holds both events at timestamp 10 (the repeat counts separately), the event at 11 and the failed event at 12. The event at 15 is left out because the end is exclusive, and it is the only event in `[15, 16)`. The average is 700 / 4 = 175.0, the throughput is 4 events over 5 seconds, one of the four events failed, and the p90 rank is ceil(3.6) = 4, which picks the largest of 100, 100, 200 and 300. Nothing was recorded in `[20, 30)`. ```hint Where the range lives Ask what storage order turns every half-open range into one contiguous block of events, and how to find that block's two ends without scanning everything. ``` ```hint Check the rank Compute ceil(0.9 * count) with integer arithmetic and test it by hand for counts 1, 9, 10 and 11 before indexing into the sorted latencies. ``` ### Constraints & Assumptions - `0 <= timestamp <= 10^9` and `0 <= latency <= 10^9`, both integers. - `0 <= start < end <= 10^9 + 1`, so `end - start` is always at least 1. - At most `5000` total operations across all methods. - Ratios are double-precision results with no rounding of intermediate or final values. - Events never change or expire, and they can arrive out of timestamp order. ### Clarifying Questions to Ask - How many `recordEvent` calls are there compared with queries, and do they interleave? That decides whether a linear-time insert is acceptable. - Must p90 be exact, or would an approximate percentile within a stated error be acceptable at larger scale? - Will the store be called from more than one thread at a time? ### What a Strong Answer Covers - A storage layout that keeps duplicates and out-of-order arrivals, and an exact way to map `[start, end)` to the selected events. - Correct behavior at the exclusive end boundary and for every empty-range return value. - Exact arithmetic for the sums and for the p90 rank, with no integer division or overflow in the ratio methods. - The time and space cost of each method under the stated limits, and what would change at much higher volume. ### Follow-up Questions - If `recordEvent` ran millions of times per second, how would you change the storage so inserts are no longer linear time? - How would you answer count, average latency and error rate in logarithmic time per query while still accepting out-of-order inserts? - How would you support any percentile, and what changes if an approximate answer is acceptable? - How would you make the store safe for concurrent writers and readers?

Overview: Design an in-memory request metrics store that records out-of-order events and answers count, average latency, throughput, error rate and nearest-rank p90 latency over half-open time ranges. It tests sorted storage with binary search, exact ratio arithmetic, and careful handling of duplicates, boundaries and empty ranges.

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

Design an in-memory store for request metrics. Each recorded event has a timestamp in seconds, a latency in milliseconds and a success flag. Events are immutable, may arrive in any order, and events with equal timestamps (or fully identical events) each count separately. Old events never expire.

Every query takes a half-open range [start, end) and includes exactly the events with start <= timestamp < end. Queries report the event count, the average latency (total latency divided by count), the throughput (count divided by end - start, in requests per second, even if some seconds are idle), the error rate (failed count divided by total count, as a fraction) and the p90 latency, defined as the latency at 1-based rank ceil(0.9 * count) in ascending order.

For an empty range, count, average latency, throughput and error rate are all 0, and p90 latency is -1. Return ratios using double-precision floating-point arithmetic without rounding intermediate or final results.

Implement the following methods:

  • recordEvent(timestamp: int, latency: int, success: bool) : record one immutable request event. Events may arrive in any order and duplicates count separately.
  • count(start: int, end: int) -> int : return the number of events with start <= timestamp < end .
  • averageLatency(start: int, end: int) -> float : return total latency divided by event count in the range, or 0 if the range is empty.
  • throughput(start: int, end: int) -> float : return event count divided by end - start in requests per second, or 0 if the range is empty.
  • errorRate(start: int, end: int) -> float : return failed event count divided by total event count as a fraction, or 0 if the range is empty.
  • p90Latency(start: int, end: int) -> int : return the latency at 1-based rank ceil(0.9 * count) in ascending order, or -1 if the range is empty.

For example, after these calls:

recordEvent(10, 100, True)
recordEvent(12, 300, False)
recordEvent(10, 100, True)
recordEvent(15, 50, True)
recordEvent(11, 200, True)

the queries return:

count(10, 15)          -> 4
averageLatency(10, 15) -> 175.0
throughput(10, 15)     -> 0.8
errorRate(10, 15)      -> 0.25
p90Latency(10, 15)     -> 300
count(15, 16)          -> 1
p90Latency(20, 30)     -> -1

The range [10, 15) holds both events at timestamp 10 (the repeat counts separately), the event at 11 and the failed event at 12. The event at 15 is left out because the end is exclusive, and it is the only event in [15, 16). The average is 700 / 4 = 175.0, the throughput is 4 events over 5 seconds, one of the four events failed, and the p90 rank is ceil(3.6) = 4, which picks the largest of 100, 100, 200 and 300. Nothing was recorded in [20, 30).

Constraints & Assumptions

  • 0 <= timestamp <= 10^9 and 0 <= latency <= 10^9 , both integers.
  • 0 <= start < end <= 10^9 + 1 , so end - start is always at least 1.
  • At most 5000 total operations across all methods.
  • Ratios are double-precision results with no rounding of intermediate or final values.
  • Events never change or expire, and they can arrive out of timestamp order.

Clarifying Questions to Ask Guidance

  • How many recordEvent calls are there compared with queries, and do they interleave? That decides whether a linear-time insert is acceptable.
  • Must p90 be exact, or would an approximate percentile within a stated error be acceptable at larger scale?
  • Will the store be called from more than one thread at a time?

What a Strong Answer Covers Guidance

  • A storage layout that keeps duplicates and out-of-order arrivals, and an exact way to map [start, end) to the selected events.
  • Correct behavior at the exclusive end boundary and for every empty-range return value.
  • Exact arithmetic for the sums and for the p90 rank, with no integer division or overflow in the ratio methods.
  • The time and space cost of each method under the stated limits, and what would change at much higher volume.

Follow-up Questions Guidance

  • If recordEvent ran millions of times per second, how would you change the storage so inserts are no longer linear time?
  • How would you answer count, average latency and error rate in logarithmic time per query while still accepting out-of-order inserts?
  • How would you support any percentile, and what changes if an approximate answer is acceptable?
  • How would you make the store safe for concurrent writers and readers?