← Questions

Raise alerts from a streaming vmstat feed

0
MetaCodingLast asked Apr 2026

A coding interview question asked in Meta's technical phone screen, last asked Apr 2026. It tests streams, parsing and monitoring.

vmstat prints a header and then one line of system statistics per interval, forever. Write a program that reads this output as it streams, parses the columns, and raises an alert when a chosen metric stays above a threshold. Alert on sustained problems, not on every noisy sample.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • monitor(lines: Iterable[str], column: str, threshold: float, window: int, min_breaches: int) -> Iterator[str] — reads lines one at a time; uses the header to find column (for example wa for I/O wait or us for user CPU); yields an alert message whenever at least min_breaches of the last window samples exceed threshold.
  • Emit one alert when the condition starts, and optionally one when it clears, rather than one per sample.

Example

column = "wa", threshold = 20, window = 5, min_breaches = 3
samples of wa:  5, 30, 25, 10, 40, 8, 3, 2
after 40 (the 5th sample): window [5, 30, 25, 10, 40] has 3 breaches  ->  ALERT wa > 20 in 3 of last 5
after 8:        window [30, 25, 10, 40, 8] still has 3                ->  no new alert (already firing)
after 3:        window [25, 10, 40, 8, 3] has 2                        ->  CLEARED

Constraints

  • The input never ends: use constant memory per metric and process each line as it arrives.
  • Header lines can repeat in the stream; malformed lines must not crash the program.

Clarifying questions

  • Which metrics and thresholds? One metric or several at once?
  • What counts as sustained: a count in a window, a moving average, or consecutive samples?
  • Should an alert repeat while the condition holds, or fire once until it clears?
  • Where do alerts go: stdout, a log, a paging system?

What a strong answer covers

  • Parsing: map column names from the header line to positions, skip repeated headers, and tolerate extra whitespace and bad lines.
  • A fixed-size window (a deque of booleans plus a running count) so each sample is O(1) work and memory is bounded.
  • Alert state with hysteresis: fire on entering the condition, clear on leaving it, so noise doesn't flap.
  • Reading line by line from a pipe (vmstat 1 | monitor) with line buffering, and handling the stream closing.
  • Tests: feed a canned list of lines, check exact alert points, a header mid-stream, a malformed line.

Follow-ups

  • Monitor several metrics with different rules from one config.
  • Use a moving average or an exponentially weighted average instead of a breach count.
  • Alerts should go to a remote service; what if it is down?

Where it's asked

  • Meta · Technical phone screen · Last asked Apr 202645 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

streams · parsing · monitoring

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding