Design a Memory Alert Analyzer for Usage Above a Threshold for N Consecutive Minutes
Company: Instacart
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a class that analyzes timestamped system memory metrics and triggers alerts when memory usage exceeds a defined threshold for more than N consecutive minutes.
The system ingests log entries one at a time, each containing a timestamp (in minutes from epoch) and a memory usage percentage. When memory usage stays strictly above the threshold for more than N consecutive minutes, an alert becomes active. The alert remains active until memory drops to or below the threshold.
The system should support querying whether an alert was active during a given time range, retrieving the current alert status, tracking the peak memory usage, and reporting the current consecutive count of minutes above the threshold.
Implement a `MemoryAlertAnalyzer` class with the following methods:
- `MemoryAlertAnalyzer(threshold: float, consecutive_minutes: int)`: Create an analyzer with the threshold percentage and N.
- `ingest_log_entry(timestamp: int, memory_usage: float)`: Ingest a single log entry with a timestamp (in minutes) and memory usage percentage. Entries arrive in non-decreasing timestamp order.
- `is_alert_active(start_time: int, end_time: int) -> bool`: Return `True` if an alert was active at any point during the time range `[start_time, end_time]`, `False` otherwise.
- `get_current_status() -> str`: Return `"ALERT"` if an alert is currently active, or `"OK"` otherwise.
- `get_peak_memory() -> float`: Return the peak (maximum) memory usage percentage observed across all ingested log entries.
- `get_consecutive_count() -> int`: Return the current count of consecutive minutes where memory usage has been above the threshold.
Each entry stands for one minute.
Example 1:
```text
Input:
["MemoryAlertAnalyzer", "ingest_log_entry", "ingest_log_entry", "ingest_log_entry", "get_current_status", "get_consecutive_count", "ingest_log_entry", "is_alert_active", "is_alert_active", "get_current_status", "get_peak_memory"]
[[80, 2], [10, 85], [11, 90], [12, 95], [], [], [13, 70], [10, 11], [11, 12], [], []]
Output: [null, null, null, null, "ALERT", 3, null, false, true, "OK", 95.0]
Explanation:
analyzer = MemoryAlertAnalyzer(80.0, 2) # threshold=80%, N=2
analyzer.ingest_log_entry(10, 85.0) # above 80, consecutive=1
analyzer.ingest_log_entry(11, 90.0) # above 80, consecutive=2
analyzer.ingest_log_entry(12, 95.0) # consecutive=3 > 2, alert active from minute 12
analyzer.get_current_status() # -> "ALERT"
analyzer.get_consecutive_count() # -> 3
analyzer.ingest_log_entry(13, 70.0) # at/below 80, alert ends (it covered minute 12), consecutive=0
analyzer.is_alert_active(10, 11) # -> False (the alert had not triggered yet)
analyzer.is_alert_active(11, 12) # -> True (minute 12 is inside the alert)
analyzer.get_current_status() # -> "OK"
analyzer.get_peak_memory() # -> 95.0
```
Example 2:
```text
Input:
["MemoryAlertAnalyzer", "ingest_log_entry", "ingest_log_entry", "ingest_log_entry", "get_current_status", "is_alert_active", "get_peak_memory", "get_consecutive_count"]
[[70, 2], [1, 75], [2, 60], [3, 75], [], [1, 3], [], []]
Output: [null, null, null, null, "OK", false, 75.0, 1]
Explanation:
analyzer = MemoryAlertAnalyzer(70.0, 2) # threshold=70%, N=2
analyzer.ingest_log_entry(1, 75.0) # above 70, consecutive=1
analyzer.ingest_log_entry(2, 60.0) # at/below 70, consecutive resets to 0
analyzer.ingest_log_entry(3, 75.0) # above 70, consecutive=1
analyzer.get_current_status() # -> "OK" (never exceeded 2 consecutive)
analyzer.is_alert_active(1, 3) # -> False (no alert was ever active)
analyzer.get_peak_memory() # -> 75.0
analyzer.get_consecutive_count() # -> 1
```
Example 3:
```text
Input:
["MemoryAlertAnalyzer", "ingest_log_entry", "ingest_log_entry", "ingest_log_entry", "get_consecutive_count", "ingest_log_entry", "get_current_status", "is_alert_active"]
[[50, 1], [1, 60], [2, 70], [2, 65], [], [5, 80], [], [3, 4]]
Output: [null, null, null, null, 2, null, "OK", false]
Explanation:
analyzer = MemoryAlertAnalyzer(50.0, 1) # threshold=50%, N=1
analyzer.ingest_log_entry(1, 60.0) # above 50, consecutive=1
analyzer.ingest_log_entry(2, 70.0) # consecutive=2 > 1, alert active from minute 2
analyzer.ingest_log_entry(2, 65.0) # same minute again, consecutive stays 2
analyzer.get_consecutive_count() # -> 2
analyzer.ingest_log_entry(5, 80.0) # minutes 3 and 4 missing: alert ends at minute 2, consecutive=1
analyzer.get_current_status() # -> "OK"
analyzer.is_alert_active(3, 4) # -> False (the alert covered only minute 2)
```
```hint Think in states
Each entry either extends the current high-memory run, restarts it, or ends it. Work out the smallest set of fields that lets you handle every entry in constant time.
```
```hint Alert history
Alerts happen one after another and never overlap. Decide what you store for each alert and how that ordering helps when many alerts have accumulated.
```
### Constraints & Assumptions
- `1 <= threshold <= 100`
- `1 <= consecutive_minutes <= 1440`
- `0 <= memory_usage <= 100`
- Timestamps are non-negative integers representing minutes.
- Log entries are ingested in non-decreasing timestamp order.
- At most `10^5` total method calls.
- For `is_alert_active`, `start_time <= end_time`.
- A run of k above-threshold entries at back-to-back minutes counts as k minutes, so with N = 2 the alert triggers on the third such minute.
- An alert is active from the minute it triggers through the last minute of its run that was above the threshold. An alert that is still running counts as active through the latest ingested minute.
- A missing minute breaks the run. When an above-threshold entry arrives more than one minute after the previous entry of the run, the count restarts at 1 and any running alert ends at the previous entry's minute.
- A second entry with the same timestamp is another reading of that minute. Above the threshold it does not add to the count. At or below the threshold it ends the run and any running alert, like any other low reading.
- `get_peak_memory` returns 0.0 before any entry has been ingested.
### Clarifying Questions to Ask
- Which call dominates the workload: ingestion or range queries? That decides whether faster queries are worth extra bookkeeping on each alert.
- Should the analyzer keep alert history forever, or only for a retention window such as the last day?
### What a Strong Answer Covers
- A precise trigger and end rule, applied the same way to the count, the current status and the stored alert history.
- Constant-time ingestion with minimal state, and why each piece of state is needed.
- How missing minutes and repeated timestamps affect the run and any running alert.
- An alert history that answers range-overlap queries correctly at the interval boundaries, ideally faster than scanning every alert.
- Time and space complexity of each method.
### Follow-up Questions
- Entries can now arrive out of order by up to a few minutes. How would you change ingestion?
- How would you run this for many hosts in one service, each with its own threshold and N?
- To stop alerts from flapping, the on-call team wants an alert to clear only after memory stays at or below the threshold for M minutes. What changes?
- How would you report how many minutes an alert was active between `start_time` and `end_time`?
Overview: Design a class that ingests timestamped memory readings, raises an alert when usage stays above a threshold for more than N consecutive minutes, and answers status, peak and alert history range queries. It tests state machine design, edge cases such as missing minutes and repeated timestamps, and interval queries.