Peeking Iterator - Problem

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

Implement the PeekingIterator class:

  • PeekingIterator(Iterator<int> nums) Initializes the object with the given integer iterator iterator.
  • int next() Returns the next element in the array and moves the pointer to the next element.
  • boolean hasNext() Returns true if there are still elements in the array.
  • int peek() Returns the next element in the array without moving the pointer.

Note: Each language may have a different implementation of the constructor and Iterator, but they all support the int next() and boolean hasNext() functions.

Input & Output

Example 1 — Basic Operations
$ Input: nums = [1,2,3], operations = ["next", "peek", "next", "next", "hasNext"]
Output: [1, 2, 2, 3, false]
💡 Note: next() returns 1 and advances. peek() returns 2 without advancing. next() returns 2 and advances. next() returns 3 and advances. hasNext() returns false as no more elements.
Example 2 — Multiple Peeks
$ Input: nums = [1,2], operations = ["peek", "peek", "next", "hasNext"]
Output: [1, 1, 1, true]
💡 Note: peek() returns 1 without advancing (called twice). next() returns 1 and advances. hasNext() returns true as element 2 remains.
Example 3 — Empty Check
$ Input: nums = [1], operations = ["next", "hasNext", "peek"]
Output: [1, false, null]
💡 Note: next() returns 1 and advances. hasNext() returns false. peek() on empty iterator behavior varies by implementation.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000
  • At most 1000 calls will be made to next, hasNext, and peek

Visualization

Tap to expand
Peeking Iterator - Buffer Next Element INPUT nums array: 1 2 3 [0] [1] [2] Operations: 1. next() 2. peek() 3. next() 4. next() 5. hasNext() Iterator wraps underlying iterator with buffer ALGORITHM STEPS 1 Initialize Buffer Store first element in buffer buf = 1 2 peek() Operation Return buffer (no advance) 3 next() Operation Return buf, refill from iter 4 hasNext() Check Check if buffer has value Buffer Flow: iter buf out peek: read buf only next: output buf, refill FINAL RESULT Step-by-step execution: Operation Result Buffer next() 1 1 --> 2 peek() 2 2 (kept) next() 2 2 --> 3 next() 3 3 --> null hasNext() false null Output Array: [1, 2, 2, 3, false] OK - Complete! Key Insight: The buffer acts as a one-element lookahead cache. peek() reads from buffer without modifying state, while next() returns the buffered value and refills from the underlying iterator. This enables O(1) peek operations without consuming elements. Time: O(1) per operation. Space: O(1) for the buffer. TutorialsPoint - Peeking Iterator | Buffer Next Element Approach
Asked in
Google 23 Amazon 18 Microsoft 15 Facebook 12
28.5K Views
Medium Frequency
~25 min Avg. Time
876 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen