Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Custom Array Iterator Implementation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Custom Array Iterator Implementation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Nutanix Prism Central may process arrays of events incrementally rather than loading each result into a separate workflow. Implement a stateful array iterator that supports sequential reads, lookahead, exhaustion checks, and resetting to the beginning.

Create an ArrayIterator class with these methods:

  1. next(): Return the next integer and advance the cursor. Return None when the iterator is exhausted.
  2. peek(): Return the next integer without advancing the cursor. Return None when exhausted.
  3. has_next(): Return True if another element can be read, otherwise False.
  4. reset(): Move the cursor back to the first element. This method returns None.

For automated evaluation, implement run_iterator_operations(values, operations). It should create one iterator, execute each operation in order, and return the result from every operation. Each operation is one of "next", "peek", "has_next", or "reset".

Formal Specification

  • Input: values, a list of integers, and operations, a list of valid operation strings.
  • Output: A list containing each operation's result. Results are integers, booleans, or None.
  • The iterator must not modify values.

Constraints

  • 0 <= len(values) <= 10^5
  • 0 <= len(operations) <= 2 * 10^5
  • Every value in values is an integer
  • Every operation is one of "next", "peek", "has_next", or "reset"
  • The input array must not be modified

Function Signature

def run_iterator_operations(values, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output