Lazy Filter over a Generator
Updated
Problem statement
A generator hands out values one at a time, and only when it is asked for the next one. Here, a source generator hands out the integers in values, in order. Each value it hands out counts as one read.
Write your own lazy filter that takes its values from the source. The filter keeps a value x only if it passes the test named by rule:
"GREATER_THAN": keepxwhenx > k."LESS_THAN": keepxwhenx < k."DIVISIBLE_BY": keepxwhenxis a multiple ofk. This includes0and negative multiples."NOT_DIVISIBLE_BY": keepxwhenxis not a multiple ofk.
Each time the filter is asked for its next value, it reads from the source one value at a time and returns the first value that passes. It never reads further than that. If the source runs out first, the filter has no more values.
A caller asks the filter for values until it has received limit values or the filter runs out. Then it stops, so it never asks for a value it will not use.
Return one row [x, reads] for each value x the caller receives, in the order received. reads is the number of values the source has handed out at the moment the filter returns x. If the caller receives no values, return an empty array.
Function
lazyFilter(values: int[], rule: String, k: int, limit: int) → int[][]Examples
Example 1
values = [3,8,-4,7,10,6]rule = "DIVISIBLE_BY"k = 2limit = 2return = [[8,2],[-4,3]]- The first request reads
3, which is odd, so the filter skips it and reads8.8is kept after2reads:[8, 2]. - The second request reads
-4, which is divisible by2. It is kept after3reads:[-4, 3]. - The caller now has
limit = 2values and stops, so7,10and6are never read.
Example 2
values = [5,4,12,0,9]rule = "GREATER_THAN"k = 4limit = 10return = [[5,1],[12,3],[9,5]]5, 12 and 9 are greater than 4; they are read 1st, 3rd and 5th. 4 is not kept, because it is not greater than itself. The source runs out after three kept values, fewer than limit = 10, so the answer has three rows.
Example 3
values = [-7,6,-2,5]rule = "NOT_DIVISIBLE_BY"k = 2limit = 1return = [[-7,1]]-7 is not divisible by 2, so it is kept on the very first read. The caller wanted only one value, so nothing else is read.
Constraints
0 <= values.length <= 1000-10^6 <= values[i] <= 10^6ruleis one of"GREATER_THAN","LESS_THAN","DIVISIBLE_BY"or"NOT_DIVISIBLE_BY".-10^6 <= k <= 10^6- If
ruleis"DIVISIBLE_BY"or"NOT_DIVISIBLE_BY", then1 <= k. 0 <= limit <= 1000
More Squarepoint Capital Phone Screen questions
- Moving Average of a Data StreamPHONE SCREEN · Seen Nov 2026
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
- Probability of Consecutive HeadsPHONE SCREEN · Seen Nov 2026
- Three Points in a Semicircle: Monte Carlo EstimatePHONE SCREEN · Seen Nov 2026
- Combine Two Index SequencesPHONE SCREEN · Seen Aug 2026
- Parse, Normalize, and Combine PolynomialsPHONE SCREEN · Seen Aug 2026
- Sorted Insertion SnapshotsPHONE SCREEN · Seen Aug 2026
- Strong and Weak Reference LifecyclePHONE SCREEN · Seen Aug 2026
- Target Sum Across Two Sorted ArraysPHONE SCREEN · Seen Aug 2026
- Classify Heap Addresses from a Memory SnapshotPHONE SCREEN · Seen Nov 2025
Similar questions at other companies
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Stepped IteratorBridge · PHONE SCREENBridge interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Count Events in Rolling Minute WindowsMeta · PHONE SCREENMeta interview questions
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions