KatInterview

Replay Operations on a Skip-Ahead Iterator

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designiteratorsimulationstate-management

Last updated

Simulate a forward-only iterator that yields every k-th element, with an idempotent has-next check, and return the results of a scripted sequence of operations.

Skip-Ahead Iterator Replay

You are modeling an iterator that reads a source sequence in a single forward pass and exposes only every step-th element. Instead of building a class, you write one function that replays a scripted list of operations against such an iterator and reports what each operation produced.

Behavior of the iterator

The iterator is created over values with a positive integer step. It can only move forward through the source. It must not index into the source or use its length.

  • The elements it exposes are those at positions 0, step, 2*step, 3*step, ... of the source, in that order. Positions that do not exist are never exposed.
  • hasNext reports whether at least one more exposed element is available. It returns True or False. Calling it repeatedly without an intervening next must give the same answer every time and must not change which element is exposed next.
  • next returns the next exposed element and moves past it. If no exposed element remains, it returns None instead (this stands in for raising an end-of-iteration error).

The first next returns the element at position 0. Each later next returns the element step positions after the previous one. If fewer than step elements remain after the last exposed one, the iterator is finished.

Task

Given values, step, a list operations, and a list arguments, process the operations in order, each as described above. Each entry of operations is either "hasNext" or "next". The entry at the same index in arguments is an empty list for every operation and can be ignored.

Return a list with one result per operation, in order:

  • For "hasNext", append True or False.
  • For "next", append the returned element, or None if nothing is left.

Constraints

  • 1 <= step <= 10^9
  • 0 <= len(values) <= 10^6
  • len(operations) == len(arguments)
  • Every operation is either "hasNext" or "next".
  • Process the source in one forward pass, keeping only O(1) extra memory beyond the result list (at most one buffered element).
  • A very large step on a short list must not cause a loop of step iterations: stop as soon as the source runs out.

Examples

Example 1

Input: replay_skip_iterator([10, 20, 30, 40, 50, 60, 70], 2, ['hasNext', 'next', 'hasNext', 'next', 'next', 'next', 'hasNext', 'next'], [[], [], [], [], [], [], [], []])

Output: [True, 10, True, 30, 50, 70, False, None]

Example 2

Input: replay_skip_iterator([1, 2, 3, 4, 5], 3, ['next', 'next', 'hasNext', 'next'], [[], [], [], []])

Output: [1, 4, False, None]

Hints

Hint 1

A forward-only source cannot tell you whether another element exists unless you actually read it, so keep a single lookahead slot.

Hint 2

After capturing the element at a target position, discard the next step - 1 elements so the source sits at the following target position. If the source ends during the discarding, the captured element is still valid.

Hint 3

Use a separate boolean to mark whether the lookahead slot is filled, plus a flag for source exhaustion, so hasNext stays idempotent and repeated calls never read further.