> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Peeking Iterator Python Solution with Tests

> Tested Python solution for LeetCode 284 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 284, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Design](/catalog/topics/design), Iterator. [View on LeetCode](https://leetcode.com/problems/peeking-iterator/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 284   # by problem number
lcpy gen -s peeking_iterator   # by problem name
```

## Problem

Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the 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.

### Examples

```
Input
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 2, 2, 3, false]

Explanation
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next();    // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek();    // return 2, the pointer does not move [1,2,3].
peekingIterator.next();    // return 2, the pointer moves to the next element [1,2,3]
peekingIterator.next();    // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False
```

### Constraints

* `1 <= nums.length <= 1000`
* `1 <= nums[i] <= 1000`
* All the calls to `next` and `peek` are valid.
* At most `1000` calls will be made to `next`, `hasNext`, and `peek`.

**Follow up:** How would you extend your design to be generic and work with all types, not just integer?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/peeking_iterator/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/peeking_iterator/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Iterator:
    def __init__(self, nums: list[int]) -> None:
        self._nums: list[int] = list(nums)
        self._index: int = 0

    def next(self) -> int:
        value = self._nums[self._index]
        self._index += 1
        return value

    def has_next(self) -> bool:
        return self._index < len(self._nums)


class PeekingIterator(Iterator):
    # Time: O(1) per call
    # Space: O(1)
    def __init__(self, iterator: Iterator) -> None:
        self._iterator = iterator
        self._peeked = 0
        self._has_peeked = False

    def peek(self) -> int:
        if not self._has_peeked:
            self._peeked = self._iterator.next()
            self._has_peeked = True
        return self._peeked

    def next(self) -> int:
        if self._has_peeked:
            self._has_peeked = False
            return self._peeked
        return self._iterator.next()

    def has_next(self) -> bool:
        return self._has_peeked or self._iterator.has_next()
```

## Complexity

| Time | Space |
| - | - |
| O(1) per call | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.