#2526
Medium Algorithms Find consecutive integers from a data stream
Hash Table Design Queue Counting Data Stream
50.8% acceptance
Feb 23, 2026
339
39
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.
Solution
Rust
Time O(1)
Space O(1)
pub struct DataStream {
value: i32,
k: i32,
count: i32,
}
impl DataStream {
pub fn new(value: i32, k: i32) -> Self {
DataStream { value, k, count: 0 }
}
pub fn consec(&mut self, num: i32) -> bool {
if num == self.value {
self.count += 1;
} else {
self.count = 0;
}
self.count >= self.k
}
}