Implement a Streaming VMStat Alert
Company: Meta
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Write a command-line program that reads a continuous stream of `vmstat`-style output from standard input and raises an alert based on a sliding time window.
The program is invoked as:
`myscript <metric_name> <threshold> <max_exceed_count> <window_seconds>`
Example:
`vmstat 1 | myscript us 100 50 300`
Requirements:
1. The input begins with a header row containing metric names, such as `us`, `sy`, `id`, and so on.
2. Each subsequent row is one sample collected once per second.
3. Monitor the metric named by `metric_name`.
4. A sample is considered abnormal if the metric value is greater than `threshold`.
5. Maintain a sliding window covering only the most recent `window_seconds` samples.
6. If the number of abnormal samples in the current window becomes greater than `max_exceed_count`, print the abnormal samples from that window.
7. The program should process the stream online without storing unbounded history.
Assume the input is whitespace-delimited and that the target metric always exists in the header.
Quick Answer: This question evaluates streaming data processing skills, sliding-window counting algorithms, and efficient online state management for metric monitoring.
Implement the core logic of a command-line alerting tool for vmstat-style data. For this coding problem, write a function that receives the input stream as a list of strings. The first non-empty line is a whitespace-delimited header containing metric names such as us, sy, id, etc. Each following non-empty line is one sample collected once per second.
Monitor the column named by metric_name. A sample row is abnormal if that metric's value is strictly greater than threshold. Maintain a sliding window containing only the most recent window_seconds samples. Whenever the number of abnormal samples in the current window transitions from less than or equal to max_exceed_count to greater than max_exceed_count, emit an alert containing all abnormal sample rows currently in that window, in chronological order.
Return all emitted alerts. Each alert is a list of the original sample row strings. If no alert is ever triggered, return an empty list.
Your solution should process the stream online and should not keep unbounded history.
Constraints
- 1 <= len(lines) <= 200000, and lines[0] is the header row if present
- 1 <= window_seconds <= 200000
- 0 <= max_exceed_count <= window_seconds
- The target metric always exists in the header
- Each sample row has the same number of whitespace-delimited fields as the header
- Metric values are numeric