Stepped Iterator
Updated
Problem statement
Simulate a stepped iterator over the integer array values for the ordered operation sequence operations. Return one string result for every operation, in the same order.
For a positive step, the iterator starts before the first element and its next zero-based index is step - 1. Process each operation as follows:
HAS_NEXTreturns"true"when the next index is insidevalues, or"false"otherwise. It never changes the iterator state.NEXTreturns the integer at the next index as a decimal string, then advances the next index bystep.- An exhausted
NEXTreturns"ERROR_EXHAUSTED"and leaves the state unchanged. - An operation other than
HAS_NEXTorNEXTreturns"ERROR_INVALID_OPERATION"and leaves the state unchanged.
If step is not positive, every operation returns "ERROR_INVALID_STEP" and no iterator state is created.
Function
runSteppedIterator(values: int[], step: int, operations: String[]) → String[]Examples
Example 1
values = [1,2,3,4,5,6]step = 2operations = ["NEXT","NEXT","NEXT"]return = ["2","4","6"]The successive next positions are 2, 4, and 6 in one-based order, so the three NEXT operations emit 2, 4, and 6.
Example 2
values = [10,20,30,40,50]step = 3operations = ["HAS_NEXT","NEXT","HAS_NEXT","NEXT","HAS_NEXT"]return = ["true","30","false","ERROR_EXHAUSTED","false"]The first HAS_NEXT is true and NEXT emits 30. The next stepped position is outside the array, so later checks are false and the exhausted NEXT reports its error without changing state.
Example 3
values = [7,8]step = 1operations = ["UNKNOWN","HAS_NEXT","NEXT"]return = ["ERROR_INVALID_OPERATION","true","7"]The unknown operation reports an error without moving the iterator. The following HAS_NEXT remains true, and NEXT still emits the first value.
Example 4
values = [7,8]step = 0operations = ["HAS_NEXT","NEXT"]return = ["ERROR_INVALID_STEP","ERROR_INVALID_STEP"]A non-positive step is invalid from the start, so every operation returns the invalid-step error.
Constraints
0 <= values.length.- Values less than or equal to
0forstepfollow the invalid-step rule. 0 <= operations.length, and every operation is a non-null string.
More Bridge interview questions
Similar questions at other companies
- Lazy Filter over a GeneratorSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- 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