Stepped Iterator
Learn this problemProblem 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 at initialization, so every requested operation receives the deterministic invalid-step result.
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.