Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Iterator Implementation and Usage

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

Your question is Iterator Implementation and Usage. 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

Foursquare Explore may return a sequence of ranked venue results that a caller consumes one at a time. Implement an iterator supporting next() and hasNext() without modifying the underlying results.

Your solution must provide a run_iterator(values, operations) function. It receives a list of result identifiers and a list of operations, then executes them in order. The operation "hasNext" returns whether another result is available. The operation "next" returns the next result and advances the iterator. If next() is called after exhaustion, return None.

The iterator should maintain only its current position and must not remove elements from the input list. Return a list containing the result of every operation, preserving operation order.

Formal Specification

  • Input values: a list of integers representing Explore result identifiers.
  • Input operations: a list containing only the strings "next" and "hasNext".
  • Output: a list of booleans, integers, or None, with one entry per operation.

Constraints

  • 0 <= len(values) <= 10^5
  • 1 <= len(operations) <= 2 * 10^5
  • Every operation is either "next" or "hasNext".
  • Values may be negative or repeated.

Function Signature

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