Wrap an iterator to support peek without advancing, alongside next and hasNext

Quick Overview

Design an iterator over a list of integers that adds a peek operation, which returns the next element without advancing, alongside next and hasNext. It tests reading ahead on a forward-only source, tracking state between calls, and keeping every operation constant time.

Wrap an iterator to support peek without advancing, alongside next and hasNext

Company: Yahoo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and `next` operations. The iterator is initialized with a list of integers. Calling `peek()` should return the next element without advancing the iterator, while `next()` returns the next element and advances the iterator. `hasNext()` reports whether more elements remain. Implement the following: - `PeekingIterator(nums: list[int])`: create the iterator over `nums`. - `peek() -> int`: return the next element without advancing the iterator. - `next() -> int`: return the next element and advance the iterator. - `hasNext() -> bool`: return whether there are more elements to iterate. Example 1: ```text Input: ["PeekingIterator", "next", "peek", "next", "next", "hasNext"] [[[1, 2, 3]], [], [], [], [], []] Output: [null, 1, 2, 2, 3, false] Explanation: it = PeekingIterator([1, 2, 3]) it.next() # -> 1 it.peek() # -> 2 (does not advance) it.next() # -> 2 it.next() # -> 3 it.hasNext() # -> false (iterator exhausted) ``` Example 2: ```text Input: ["PeekingIterator", "hasNext", "peek", "peek", "next", "hasNext", "next", "hasNext"] [[[7, 9]], [], [], [], [], [], [], []] Output: [null, true, 7, 7, 7, true, 9, false] Explanation: it = PeekingIterator([7, 9]) it.hasNext() # -> true it.peek() # -> 7 (no advance) it.peek() # -> 7 (still no advance) it.next() # -> 7 it.hasNext() # -> true it.next() # -> 9 it.hasNext() # -> false ``` ```hint Forward-only source An iterator can only move forward, so finding out the next element means consuming it. Decide where that element lives between a `peek()` and the `next()` that follows it. ``` ### Constraints & Assumptions - `1 <= nums.length <= 1000` - `1 <= nums[i] <= 1000` - All calls to `next` and `peek` are valid, meaning `hasNext()` would return `true` right before the call. - At most 1000 calls are made in total to `next`, `hasNext`, and `peek`. ### Clarifying Questions to Ask - Should the class wrap any iterator (a generator or a stream), or is indexing into the given list acceptable? - What should `peek()` or `next()` do if they are called after the iterator is exhausted? - Can the source ever yield a value such as `None` or `null` that could be mistaken for an empty buffer? ### What a Strong Answer Covers - How `peek()` reads ahead on a forward-only source without losing the element. - The state kept between calls and how each method updates it. - How `hasNext()` tells an empty buffer apart from a buffered value. - Time and extra space per call. ### Follow-up Questions - How would you make the class generic so it wraps an iterator of any type, including one that yields `None`? - The constructor pulls the first element right away. When is that a problem, and how would you fetch lazily instead? - How would you support peeking `k` elements ahead?

Overview: Design an iterator over a list of integers that adds a peek operation, which returns the next element without advancing, alongside next and hasNext. It tests reading ahead on a forward-only source, tracking state between calls, and keeping every operation constant time.

Yahoo logo
Yahoo
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and next operations.

The iterator is initialized with a list of integers. Calling peek() should return the next element without advancing the iterator, while next() returns the next element and advances the iterator. hasNext() reports whether more elements remain.

Implement the following:

  • PeekingIterator(nums: list[int]) : create the iterator over nums .
  • peek() -> int : return the next element without advancing the iterator.
  • next() -> int : return the next element and advance the iterator.
  • hasNext() -> bool : return whether there are more elements to iterate.

Example 1:

Input:
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]],       [],     [],     [],     [],     []]

Output: [null, 1, 2, 2, 3, false]

Explanation:
it = PeekingIterator([1, 2, 3])
it.next()                         # -> 1
it.peek()                         # -> 2     (does not advance)
it.next()                         # -> 2
it.next()                         # -> 3
it.hasNext()                      # -> false (iterator exhausted)

Example 2:

Input:
["PeekingIterator", "hasNext", "peek", "peek", "next", "hasNext", "next", "hasNext"]
[[[7, 9]],          [],        [],     [],     [],     [],        [],     []]

Output: [null, true, 7, 7, 7, true, 9, false]

Explanation:
it = PeekingIterator([7, 9])
it.hasNext()                      # -> true
it.peek()                         # -> 7     (no advance)
it.peek()                         # -> 7     (still no advance)
it.next()                         # -> 7
it.hasNext()                      # -> true
it.next()                         # -> 9
it.hasNext()                      # -> false

Constraints & Assumptions

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • All calls to next and peek are valid, meaning hasNext() would return true right before the call.
  • At most 1000 calls are made in total to next , hasNext , and peek .

Clarifying Questions to Ask Guidance

  • Should the class wrap any iterator (a generator or a stream), or is indexing into the given list acceptable?
  • What should peek() or next() do if they are called after the iterator is exhausted?
  • Can the source ever yield a value such as None or null that could be mistaken for an empty buffer?

What a Strong Answer Covers Guidance

  • How peek() reads ahead on a forward-only source without losing the element.
  • The state kept between calls and how each method updates it.
  • How hasNext() tells an empty buffer apart from a buffered value.
  • Time and extra space per call.

Follow-up Questions Guidance

  • How would you make the class generic so it wraps an iterator of any type, including one that yields None ?
  • The constructor pulls the first element right away. When is that a problem, and how would you fetch lazily instead?
  • How would you support peeking k elements ahead?