Magicsheet logo

Find Consecutive Integers from a Data Stream

Medium
100%
Updated 6/1/2025

Find Consecutive Integers from a Data Stream

What is this problem about?

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.

Why is this asked in interviews?

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.

Algorithmic pattern used

This is a State Tracking or Counting problem. You don't actually need to store the last kk integers. You only need to know:

  1. The target value.
  2. The target length k.
  3. A counter count that tracks how many consecutive times the target value has appeared at the end of the stream.

When a new number num arrives:

  • If num == value, increment count.
  • If num != value, reset count to 0.
  • Return count >= k.

Example explanation

Target value = 5, k = 3.

  1. consec(5): num matches value. count = 1. Returns 1 >= 3 (false).
  2. consec(5): num matches value. count = 2. Returns 2 >= 3 (false).
  3. consec(5): num matches value. count = 3. Returns 3 >= 3 (true).
  4. consec(3): num doesn't match! Reset count = 0. Returns 0 >= 3 (false).

Common mistakes candidates make

  • Using a Queue: Storing kk elements in a queue and checking them all every time, which takes O(K)O(K) per call instead of O(1)O(1).
  • Not resetting correctly: Failing to set the counter back to zero when a different number appears.
  • Memory leakage: Storing the entire history of the stream when only the most recent kk elements (or just their count) matter.

Interview preparation tip

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.

Similar Questions