Find Consecutive Integers from a Data Stream — LeetCode 2526 Python Solution

MediumDesignQueueHash TableCountingData Stream
Problem
#2526
Pattern
Stack
Reading time
3 min

The problem

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.

Example

Input
["DataStream", "consec", "consec", "consec", "consec"]
Output
[null, false, false, true, false]
Explanation
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3

Python solution

Python
class DataStream:
    def __init__(self, value: int, k: int):
        self.val, self.k = value, k
        self.cnt = 0

    def consec(self, num: int) -> bool:
        self.cnt = 0 if num != self.val else self.cnt + 1
        return self.cnt >= self.k


# Your DataStream object will be instantiated and called as such:
# obj = DataStream(value, k)
# param_1 = obj.consec(num)

Complexity

MeasureComplexity
TimeO(1)
SpaceO(1) auxiliary

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 2526. Find Consecutive Integers from a Data Stream is filed here because LeetCode tags it Queue, which is the vocabulary this hub collects.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2526. Find Consecutive Integers from a Data Stream?
LeetCode 2526. Find Consecutive Integers from a Data Stream is rated Medium on LeetCode.
What is the time complexity of LeetCode 2526. Find Consecutive Integers from a Data Stream?
The Python solution on this page runs in O(1).
What is the space complexity of LeetCode 2526. Find Consecutive Integers from a Data Stream?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2526. Find Consecutive Integers from a Data Stream cover?
LeetCode 2526. Find Consecutive Integers from a Data Stream is tagged Design, Queue, Hash Table, Counting and Data Stream on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview