Find Consecutive Integers from a Data Stream — Solution
Find Consecutive Integers from a Data Stream solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Find Consecutive Integers from a Data Stream is a medium coding problem on hash table, counting and queue, asked in Intuit interviews.
- Difficulty: Medium
- Topics: Hash Table, Counting, Queue
- Asked at: Intuit
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Integers arrive one at a time. After each arrival you must say whether the last k integers of the stream are all equal to a fixed value.
Write the class DataStream:
DataStream(int value, int k)starts an empty stream watching forkcopies ofvaluein a row.boolean consec(int num)appendsnumto the stream and returnstrueif the lastkintegers are all equal tovalue. If fewer thankintegers have arrived so far, it returnsfalse.
The judge calls the methods in the order listed in the first input line, with the arguments in the second; the expected output holds each call's result, null for the constructor and for every method that returns nothing.
Example 1
Input: ["DataStream","consec","consec","consec","consec","consec"]
[[4,3],[4],[4],[4],[3],[4]]
Output: [null,false,false,true,false,false]
Explanation: The first two calls see fewer than 3 integers. The third completes 4, 4, 4. Then 3 arrives, and the last three are 4, 4, 3; after the next 4 they are 4, 3, 4.
Example 2
Input: ["DataStream","consec","consec","consec"]
[[1000000000,1],[1000000000],[7],[1000000000]]
Output: [null,true,false,true]
Explanation: With k = 1, each answer is just whether the newest integer equals value.
Constraints
1 <= value, num <= 10^91 <= k <= 10^5At most 10^5 calls are made to consec
Hints for Find Consecutive Integers from a Data Stream
Hint 1
Keeping the last k integers and checking them all costs O(k) per call.
Hint 2
Only one thing about the window matters: how long the current run of
valueat the end of the stream is.Hint 3
Keep a counter: reset it to 0 when
num != value, otherwise add 1. The answer iscounter >= k.
How to solve Find Consecutive Integers from a Data Stream
The last k integers are all value exactly when the stream ends in a run of at least k copies of value. A single counter of that trailing run answers every call in O(1).
Approach
- Keep
run = 0. consec(num): ifnum == value, increaserun; otherwise setrun = 0.- Return
run >= k.
Why it works
After each call run is the number of consecutive copies of value at the end of the stream: a match extends that suffix by one, anything else ends it. The last k integers are all value if and only if that suffix has length at least k (and it cannot when fewer than k integers have arrived).
Complexity
- Time —
O(1) per call - Space —
O(1)
Pitfalls
- Return
falseuntil at leastkintegers have arrived — the counter handles that automatically because it cannot exceed the number of calls. - Do not store the stream: 10^5 calls with k up to 10^5 make a window scan quadratic.
Reference solution
Python
class DataStream:
def __init__(self, value: int, k: int):
self.value = value
self.k = k
self.run = 0 # copies of value at the end of the stream
def consec(self, num: int) -> bool:
self.run = self.run + 1 if num == self.value else 0
return self.run >= self.kJava
class DataStream {
private int value, k, run = 0; // run: copies of value at the end of the stream
public DataStream(int value, int k) {
this.value = value;
this.k = k;
}
public boolean consec(int num) {
run = num == value ? run + 1 : 0;
return run >= k;
}
}C++
class DataStream {
int value, k, run = 0; // run: copies of value at the end of the stream
public:
DataStream(int value, int k) : value(value), k(k) {}
bool consec(int num) {
run = num == value ? run + 1 : 0;
return run >= k;
}
};JavaScript
var DataStream = function(value, k) {
this.value = value;
this.k = k;
this.run = 0; // copies of value at the end of the stream
};
DataStream.prototype.consec = function(num) {
this.run = num === this.value ? this.run + 1 : 0;
return this.run >= this.k;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 439 hash table problems · the whole catalogue
Learn the technique: Hashing: Hash Maps and Hash Sets · Queues and Deques