Check whether the last k integers in a stream all equal a target value

Quick Overview

Implement a stream class whose method adds an integer and reports whether the last k integers all equal a target value, returning false until k integers have arrived. It tests choosing the minimal state for a streaming check and reasoning about constant time and space per call.

Check whether the last k integers in a stream all equal a target value

Company: Intel

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

For a stream of integers, implement a data structure that checks whether the last `k` integers parsed in the stream are equal to `value`. Implement the `DataStream` class. It is initialized with an empty stream and two integers, `value` and `k`. It should support adding integers to the stream and checking whether the last `k` integers are all equal to `value`. If there are fewer than `k` integers in the stream, the condition does not hold, so return `false`. Implement the following methods: - `DataStream(value: int, k: int)` creates the object with an empty stream. - `consec(num: int) -> bool` adds `num` to the stream of integers. It returns `true` if the last `k` integers are equal to `value`, and `false` otherwise. If there are fewer than `k` integers, it returns `false`. Example 1: ```text Input: ["DataStream", "consec", "consec", "consec", "consec"] [[4, 3], [4], [4], [4], [3]] Output: [null, false, false, true, false] Explanation: ds = DataStream(4, 3) # value = 4, k = 3 ds.consec(4) # -> false (only 1 consecutive 4, need 3) ds.consec(4) # -> false (only 2 consecutive 4s, need 3) ds.consec(4) # -> true (3 consecutive 4s) ds.consec(3) # -> false (streak broken, reset to 0) ``` Example 2: ```text Input: ["DataStream", "consec", "consec", "consec", "consec", "consec"] [[1, 2], [1], [2], [1], [1], [1]] Output: [null, false, false, false, true, true] Explanation: ds = DataStream(1, 2) # value = 1, k = 2 ds.consec(1) # -> false (only 1 consecutive 1, need 2) ds.consec(2) # -> false (streak broken, reset to 0) ds.consec(1) # -> false (only 1 consecutive 1, need 2) ds.consec(1) # -> true (2 consecutive 1s) ds.consec(1) # -> true (3 consecutive 1s, still >= 2) ``` ```hint What to remember Think about which fact about the end of the stream decides the answer, and whether you need to keep the integers themselves to know it. ``` ### Constraints & Assumptions - `1 <= value, num <= 10^9` - `1 <= k <= 10^5` - At most `10^5` calls will be made to `consec`. ### What a Strong Answer Covers - What state is enough to answer each call, and why the last `k` integers do not need to be stored. - How that state changes when `num` matches `value` and when it does not. - The case with fewer than `k` integers so far, and the case where the matching run grows longer than `k`. - The time and space cost per call, compared with keeping a queue of the last `k` integers. ### Follow-up Questions - How would you support a `k` that is passed in with each call, or several `(value, k)` pairs checked on the same stream? - How would you change the design to return `true` when at least `m` of the last `k` integers equal `value`? - If the stream never ends, can your counter overflow, and how would you keep it bounded?

Overview: Implement a stream class whose method adds an integer and reports whether the last k integers all equal a target value, returning false until k integers have arrived. It tests choosing the minimal state for a streaming check and reasoning about constant time and space per call.

Intel logo
Intel
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

For a stream of integers, implement a data structure that checks whether the last k integers parsed in the stream are equal to value.

Implement the DataStream class. It is initialized with an empty stream and two integers, value and k. It should support adding integers to the stream and checking whether the last k integers are all equal to value. If there are fewer than k integers in the stream, the condition does not hold, so return false.

Implement the following methods:

  • DataStream(value: int, k: int) creates the object with an empty stream.
  • consec(num: int) -> bool adds num to the stream of integers. It returns true if the last k integers are equal to value , and false otherwise. If there are fewer than k integers, it returns false .

Example 1:

Input:
["DataStream", "consec", "consec", "consec", "consec"]
[[4, 3],       [4],      [4],      [4],      [3]]

Output: [null, false, false, true, false]

Explanation:
ds = DataStream(4, 3)    # value = 4, k = 3
ds.consec(4)             # -> false  (only 1 consecutive 4, need 3)
ds.consec(4)             # -> false  (only 2 consecutive 4s, need 3)
ds.consec(4)             # -> true   (3 consecutive 4s)
ds.consec(3)             # -> false  (streak broken, reset to 0)

Example 2:

Input:
["DataStream", "consec", "consec", "consec", "consec", "consec"]
[[1, 2],       [1],      [2],      [1],      [1],      [1]]

Output: [null, false, false, false, true, true]

Explanation:
ds = DataStream(1, 2)    # value = 1, k = 2
ds.consec(1)             # -> false  (only 1 consecutive 1, need 2)
ds.consec(2)             # -> false  (streak broken, reset to 0)
ds.consec(1)             # -> false  (only 1 consecutive 1, need 2)
ds.consec(1)             # -> true   (2 consecutive 1s)
ds.consec(1)             # -> true   (3 consecutive 1s, still >= 2)

Constraints & Assumptions

  • 1 <= value, num <= 10^9
  • 1 <= k <= 10^5
  • At most 10^5 calls will be made to consec .

What a Strong Answer Covers Guidance

  • What state is enough to answer each call, and why the last k integers do not need to be stored.
  • How that state changes when num matches value and when it does not.
  • The case with fewer than k integers so far, and the case where the matching run grows longer than k .
  • The time and space cost per call, compared with keeping a queue of the last k integers.

Follow-up Questions Guidance

  • How would you support a k that is passed in with each call, or several (value, k) pairs checked on the same stream?
  • How would you change the design to return true when at least m of the last k integers equal value ?
  • If the stream never ends, can your counter overflow, and how would you keep it bounded?