AlgoMaster Logo
AlgoMasterEvaluate Duration-Based Alertsmedium

Evaluate Duration-Based Alerts

medium

An alert that fires on every one-sample spike creates noise. A duration condition reduces that noise by requiring a threshold breach to persist before the alert begins firing.

Design a DurationAlertMonitor class:

  • DurationAlertMonitor() creates a stateless monitor.
  • boolean[] firingStates(int[] values, int threshold, int forSamples) returns whether the alert is firing at every sample.

Process values in order. A sample is a breach only when it is strictly greater than threshold:

  • A breach extends the current run of consecutive breaches by one.
  • A sample at or below the threshold resets the run to zero.
  • The alert fires at a sample when the current run length is at least forSamples.

The result must have the same length as values. Each method call is independent; no alert state carries over from an earlier call.

Example 1:

Input:

Output:

Explanation: Values 5, 6, and 7 form a breach run. Its length first reaches 2 at value 6, so the alert fires for 6 and 7. Value 2 resets the run. The later values 8 and 9 form another qualifying run.

Example 2:

Input:

Output:

Explanation: All four samples breach the threshold. The run reaches the required length at the third sample and remains long enough at the fourth.

Constraints

  • 1 <= values.length <= 10^5
  • -10^9 <= values[i], threshold <= 10^9
  • 1 <= forSamples <= 10^5
  • A value equal to threshold is not a breach.
  • At most 100 calls are made to firingStates.
Hints

Loading...
CallReturns
new DurationAlertMonitor()null
firingStates([1,5,6,7,2,8,9], 4, 2)[false,false,true,true,false,false,true]

The first run above 4 reaches length 2 at value 6 and ends at value 2. The second run reaches length 2 at value 9.

Run checks these cases. Submit also runs a larger hidden set.