Peeking Iterator

To solve this coding challenge, we need to implement a custom iterator class called
PeekingIterator
that wraps an existing iterator and adds a
peek()
method. This method allows us to view the next element without consuming it. We'll also need to implement the existing methods
next()
and
hasNext()
such that they work seamlessly with our new
peek()
functionality.

Explanation

Let's break down the problem and the functionalities we need to implement:
  1. Initialization : We need to initialize the
    PeekingIterator
    with an existing iterator. We'll also need a variable to store the peeked value.
  2. Peek Operation : The
    peek()
    method will return the next element without advancing the iterator. If no element has been peeked yet, it will retrieve it from the wrapped iterator and store it for future use.
  3. Next Operation : The
    next()
    method will return the next element from the iterator. If an element has been peeked (using the
    peek()
    method), it should return that element and reset the peeked value.
  4. HasNext Operation : The
    hasNext()
    method will check if there are any elements left in the iterator. It should account for both the peeked value and the state of the wrapped iterator.

Step-by-Step Explanation/Detailed Steps in Pseudocode

Step 1: Initialization
  • Initialize the
    PeekingIterator
    with the given iterator.
  • Set the
    peeked_value
    to
    None
    indicating that no value has been peeked yet.
Step 2: Peek Method
  • Check if there is a
    peeked_value
    already stored. If yes, return it.
  • If no
    peeked_value
    is stored, get the next value from the iterator, store it in
    peeked_value
    , and then return it.
Step 3: Next Method
  • Check if there is a
    peeked_value
    . If yes, return it and reset
    peeked_value
    to
    None
    .
  • If no
    peeked_value
    is stored, directly return the next value from the iterator.
Step 4: HasNext Method
  • Check if there is a
    peeked_value
    which would mean there is at least one element left. If yes, return
    True
    .
  • If
    peeked_value
    is
    None
    , return the result of
    hasNext
    on the wrapped iterator.

Pseudocode with Comments

                                            
# PeekingIterator class to extend standard iterator functionalities
class PeekingIterator:
    # Constructor method to initialize the class with an existing iterator
    method __init__(iterator):
        # Store the provided iterator
        this.iterator = iterator
        # Initialize the peeked_value to None
        this.peeked_value = None

    # Method to peek the next element without moving the iterator
    method peek() -> int:
        # If there is no peeked value yet
        if this.peeked_value is None:
            # Retrieve the next value from the iterator and store in peeked_value
            this.peeked_value = this.iterator.next()
        # Return the peeked value
        return this.peeked_value

    # Method to get the next element and move the iterator
    method next() -> int:
        # If there is a peeked value
        if this.peeked_value is not None:
            # Store the peeked value in a variable to return it
            result = this.peeked_value
            # Reset peeked_value to None
            this.peeked_value = None
            # Return the previous peeked value
            return result
        # If there is no peeked value, return the next value from the iterator
        return this.iterator.next()

    # Method to check if there are more elements in the iterator
    method hasNext() -> bool:
        # Return True if there is a peeked value or if the iterator has more elements
        return this.peeked_value is not None or this.iterator.hasNext()

                                        
By following this pseudocode, we can implement the
PeekingIterator
class that satisfies the requirements laid out in the problem description. This class will be capable of peeking at the next element without consuming it, moving to the next element, and checking if more elements are available in the underlying iterator.