1058. Streak Watcher

A factory sensor reports one integer reading per second, and an alarm must fire whenever the machine has shown the same fault code value for k consecutive readings. Readings arrive one by one and old readings are not stored by the caller.

Implement StreakWatcher. The constructor receives the fault code value and the streak length k. Each call consec(num) records a new reading num and returns true when the most recent k readings are all equal to value; it returns false if fewer than k readings exist or any of them differs. Each call must take O(1) time and O(1) extra space.

Example 1

Input:
operations = ["StreakWatcher","consec","consec","consec","consec","consec"]arguments = [[7,3],[7],[7],[7],[4],[7]]
Output:
[null,false,false,true,false,false]
Explanation:

Only the third call sees three 7s in a row; the 4 breaks the streak and the final 7 starts it afresh.

Example 2

Input:
operations = ["StreakWatcher","consec","consec","consec","consec"]arguments = [[0,1],[5],[0],[0],[2]]
Output:
[null,false,true,true,false]
Explanation:

With k = 1 the answer only depends on whether the newest number equals 0.

Example 3

Input:
operations = ["StreakWatcher","consec","consec","consec","consec","consec"]arguments = [[9,2],[9],[1],[9],[9],[9]]
Output:
[null,false,false,false,true,true]
Explanation:

The window of two newest numbers is all 9s from the fourth call onwards.

Constraints

1 ≤ k ≤ 105

0 ≤ value, num ≤ 109

At most 104 calls to consec

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Input
Each case is an operation log. operations names the class first and then each method call; arguments holds the arguments for each, in the same order. Your answer is one list with a result per operation - null for the constructor and for methods that return nothing.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(1) per call
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…

operations names the class, then each method to call; arguments holds one list of arguments per operation, in the same order.