Check whether every number in a growing stream is divisible by k

Quick Overview

Process a list of add and check operations on a stream of integers and report, for each check, whether every number added so far is divisible by k. Tests whether you can replace a scan of the stream with a running summary value and handle the empty-stream case.

Check whether every number in a growing stream is divisible by k

Company: Disney

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are processing a stream of integers and need to answer divisibility queries efficiently. You are given an array `queries`, where each query is one of two types: - `[1, x]`: add the integer `x` to the running stream. - `[2, k]`: answer `True` if every number currently in the stream is divisible by `k`, otherwise `False`. If the stream is empty, answer `True`. Return a list with the answer to each type-2 query, in the order the queries appear. ### Function Signature ```python def stream_divisibility(queries: list[list[int]]) -> list[bool]: ``` ### Rules - Each query is a list of exactly two integers: the query type (1 or 2) followed by its value. - Type-1 queries produce no output. The returned list has one boolean per type-2 query. - A query of type 2 on an empty stream answers `True`. ### Constraints - `1 <= len(queries) <= 10^4` - For a query `[1, x]`: `1 <= x <= 10^9` - For a query `[2, k]`: `1 <= k <= 100` ### Examples ```text Input: queries = [[1, 12], [1, 18], [2, 6], [2, 4], [1, 24], [2, 6], [2, 12]] Output: [True, False, True, False] ``` After adding 12 and 18, both are multiples of 6 but 18 is not a multiple of 4. Adding 24 keeps 6 as a common divisor, and 12 fails because 18 is not a multiple of 12. ```text Input: queries = [[2, 5], [1, 10], [2, 5], [2, 3], [1, 15], [2, 5]] Output: [True, True, False, True] ``` The first query sees an empty stream and answers `True`. With only 10 in the stream, 5 passes and 3 fails. After adding 15, both 10 and 15 are multiples of 5.

Overview: Process a list of add and check operations on a stream of integers and report, for each check, whether every number added so far is divisible by k. Tests whether you can replace a scan of the stream with a running summary value and handle the empty-stream case.

Disney logo
Disney
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are processing a stream of integers and need to answer divisibility queries efficiently.

You are given an array queries, where each query is one of two types:

  • [1, x] : add the integer x to the running stream.
  • [2, k] : answer True if every number currently in the stream is divisible by k , otherwise False . If the stream is empty, answer True .

Return a list with the answer to each type-2 query, in the order the queries appear.

Function Signature

def stream_divisibility(queries: list[list[int]]) -> list[bool]:

Rules

  • Each query is a list of exactly two integers: the query type (1 or 2) followed by its value.
  • Type-1 queries produce no output. The returned list has one boolean per type-2 query.
  • A query of type 2 on an empty stream answers True .

Constraints

  • 1 <= len(queries) <= 10^4
  • For a query [1, x] : 1 <= x <= 10^9
  • For a query [2, k] : 1 <= k <= 100

Examples

Input: queries = [[1, 12], [1, 18], [2, 6], [2, 4], [1, 24], [2, 6], [2, 12]]
Output: [True, False, True, False]

After adding 12 and 18, both are multiples of 6 but 18 is not a multiple of 4. Adding 24 keeps 6 as a common divisor, and 12 fails because 18 is not a multiple of 12.

Input: queries = [[2, 5], [1, 10], [2, 5], [2, 3], [1, 15], [2, 5]]
Output: [True, True, False, True]

The first query sees an empty stream and answers True. With only 10 in the stream, 5 passes and 3 fails. After adding 15, both 10 and 15 are multiples of 5.