For a stream of integers, implement a data structure that checks if the last k integers parsed in the stream are equal to value.
Implement the DataStream class:
DataStream(int value, int k) Initializes the object with an empty integer stream and the two integers value and k.boolean consec(int num) Adds num to the stream of integers. Returns true if the last k integers are equal to value, and false otherwise. If there are less than k integers, the condition does not hold true, so returns false.Example 1:
Input
["DataStream", "consec", "consec", "consec", "consec"]
[[4, 3], [4], [4], [4], [3]]
Output
[null, false, false, true, false]
Explanation
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
dataStream.consec(4); // Only 1 integer is parsed, so returns False.
dataStream.consec(4); // Only 2 integers are parsed.
// Since 2 is less than k, returns False.
dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
// Since 3 is not equal to value, it returns False.
Constraints:
1 <= value, num <= 1091 <= k <= 105105 calls will be made to consec.The key idea in #2526 Find Consecutive Integers from a Data Stream is to efficiently track whether the last k numbers in a stream match a specific value. Since numbers arrive continuously, storing the entire stream is unnecessary and inefficient.
A practical approach is to maintain a running counter that tracks how many consecutive occurrences of the target value have appeared. Each time a new number arrives through the stream method, compare it with the target value. If it matches, increment the counter; otherwise, reset the counter to zero. Once the counter reaches or exceeds k, the condition for k consecutive integers is satisfied.
An alternative design uses a small queue or sliding window of the last k elements and checks whether all elements equal the target value. However, the counter-based solution is more space-efficient.
This design ensures constant-time updates for each incoming number while keeping memory usage minimal.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Counting consecutive matches | O(1) per stream update | O(1) |
| Queue / sliding window of size k | O(1) per stream update | O(k) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
Keep track of the last integer which is not equal to <code>value</code>.
Use a queue-type data structure to store the last <code>k</code> integers.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Counting works well because the problem only requires tracking consecutive occurrences of a single value. Instead of storing previous elements, you only maintain the current streak length, which keeps both time and space complexity constant.
Yes, similar data stream design problems frequently appear in technical interviews at large tech companies. They test understanding of streaming data, state maintenance, and designing efficient real-time checks.
A counter variable combined with simple conditional logic is typically sufficient and most efficient. While a queue or sliding window can track the last k elements, it uses more space than the counter-based approach.
The optimal approach uses a simple counter to track how many consecutive times the target value appears in the stream. When a new number arrives, increment the counter if it matches the value or reset it otherwise. If the counter reaches k, the function returns true.