← Questions

Count errors in a log stream with a moving average

0
MicrosoftSystem designLast asked Dec 2025

A system design interview question asked in Microsoft's HackerRank assessment, last asked Dec 2025. It tests streaming, logs and metrics.

Many upstream services emit log events continuously. Each event carries at least service_id, timestamp, log_level and message. Design the streaming component that picks out the error logs, keeps a live error count per service, keeps a moving average of each service's error count over a sliding time window, and raises an alarm when a service crosses a threshold.

Requirements

  • Functional:
    • Filter the stream and output only error-level events.
    • Maintain a real-time error count per service.
    • Maintain a moving average of errors per service over a sliding window (for example, errors per minute averaged over the last 10 minutes).
    • Alarm when a service's error rate or moving average crosses a threshold, without alarming again every second while it stays above.
  • Non-functional: many services and a high event rate, alerts within seconds, bounded memory per service, no lost state when a worker dies.

Clarifying questions

  • How many services, and what peak event rate?
  • Is the window defined on event time or processing time, and how late can events arrive?
  • Is the threshold fixed per service, or relative to the service's own baseline?
  • Who receives the alarm, and what does it need to contain?

What a strong answer covers

  • The pipeline: producers into a partitioned log (partition by service_id), stateless filtering of error events, then a keyed stateful operator per service.
  • State per service: a ring of per-second or per-minute buckets, so the window sum and average update in O(1) and memory is bounded by the window length.
  • Event-time windows with watermarks; how late events are added to their bucket, and what happens to events later than the allowed lateness.
  • Alarm logic: thresholds, hysteresis or a cooldown so one incident gives one alarm, and the alarm's payload.
  • Fault tolerance: checkpointed state with offsets so a restarted worker resumes exactly; scaling by adding partitions, and handling one very noisy service.

Follow-ups

  • Replace the fixed threshold with anomaly detection against each service's normal error rate.
  • Count errors per error message signature, not only per service.
  • Show the counts on a dashboard with history: what do you store and where?

Where it's asked

What it tests

streaming · logs · metrics

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing