The Find Consecutive Integers from a Data Stream interview question asks you to design a class that receives a stream of integers one by one. The class is initialized with a value and a k. You need to implement a consec(num) method that returns true if the last k integers received were all equal to the target value.
Intel and other tech companies use this to test your ability to handle Data Stream interview patterns. It evaluates how you maintain state over time. While you could store all numbers in a queue, a more optimized approach only requires a single integer to track the "streak" of the target value, testing your ability to find space-efficient solutions for streaming data.
This is a State Tracking or Counting problem. You don't actually need to store the last integers. You only need to know:
value.k.count that tracks how many consecutive times the target value has appeared at the end of the stream.When a new number num arrives:
num == value, increment count.num != value, reset count to 0.count >= k.Target value = 5, k = 3.
consec(5): num matches value. count = 1. Returns 1 >= 3 (false).consec(5): num matches value. count = 2. Returns 2 >= 3 (false).consec(5): num matches value. count = 3. Returns 3 >= 3 (true).consec(3): num doesn't match! Reset count = 0. Returns 0 >= 3 (false).Streaming problems often allow for "summarization." Instead of keeping raw data, ask yourself: "What is the minimum information I need to answer the next query?" In this case, just the length of the current streak is sufficient.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Detect Squares | Medium | Solve | |
| First Unique Number | Medium | Solve | |
| Logger Rate Limiter | Easy | Solve | |
| Number of Recent Calls | Easy | Solve | |
| Design an Array Statistics Tracker | Hard | Solve |