Observe a Lazy Generator
Updated
Problem statement
Model the observable behavior of a lazy value generator. For this exercise, assume it yields the supplied integers in values, in their original order, one at a time. This is a practice fixture, not a request to invent a generated sequence.
Process operations in order and return one observation string per operation:
NEXT: yield the next unused value asvalue:<integer>. If no value remains, or the generator is closed, returndone.CLOSE: permanently close the generator and returnclosed. Repeating this operation is harmless and still returnsclosed.
A successful NEXT consumes exactly one value. A failed NEXT does not restart the generator. Closing it discards access to all remaining values. With no operations, return an empty array.
Keep iteration lazy: do not make a second full copy of the yielded sequence. You may use a native generator or an equivalent cursor or closure that has the same behavior.
Function
observeGenerator(values: int[], operations: String[]) → String[]Examples
Example 1
values = [4,9]operations = ["NEXT","NEXT","NEXT"]return = ["value:4","value:9","done"]The first two pulls consume 4, then 9. A third pull observes exhaustion.
Example 2
values = [7,8]operations = ["NEXT","CLOSE","NEXT","CLOSE"]return = ["value:7","closed","done","closed"]The first pull yields 7. Closing prevents 8 from being yielded. Later pulls stay done; repeated close remains closed.
Example 3
values = []operations = ["NEXT","CLOSE","NEXT"]return = ["done","closed","done"]An empty generator is exhausted immediately and stays unavailable after close.
Constraints
0 <= values.length <= 500.-1000000 <= values[i] <= 1000000.0 <= operations.length <= 500.- Each operation is exactly
NEXTorCLOSE.
More Squarepoint Capital interview questions
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Maximum SubarrayONSITE INTERVIEW · Seen Aug 2026
- Product of Array Except SelfONSITE INTERVIEW · Seen Aug 2026
- Estimate Pi with a Finite SeriesONSITE INTERVIEW · Seen Oct 2025
- In-Memory Directory and File OperationsONSITE INTERVIEW · Seen Oct 2025
- Count C++ Constructor and Assignment CallsONSITE INTERVIEW · Seen Jun 2025
- Order Matching on ExecuteONSITE INTERVIEW · Seen Dec 2024
- Accumulate Numeric Value ObjectsONSITE INTERVIEW · Seen Sep 2024
- Differentiate a PolynomialONSITE INTERVIEW · Seen Aug 2023
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Copy File Bytes with Partial WritesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Policy Violation Set ChecksPinterest · ONSITE INTERVIEWPinterest interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions