This felt like four questions crammed into one.
Start by defining a generic Iterator<T> interface with hasNext() and next(), then implement ArrayIterator<T> over a fixed array with an index. Wrap it with PeekingIterator<T> that caches the next element for peek() without advancing the underlying iterator. Write minimal unit tests for edge cases and analyze time/space complexity for each component.
Pro tip: Mention that PeekingIterator should handle NoSuchElementException consistently and that peek() should not advance the underlying iterator. Also, note that the wrapper pattern allows adding functionality without modifying the original iterator.
Create a generic Iterator<T> interface with methods boolean hasNext() and T next(). Specify that next() throws NoSuchElementException when no more elements.
Implement ArrayIterator<T> that takes a fixed array in the constructor, maintains an index, and implements hasNext() and next() accordingly.
Implement PeekingIterator<T> that wraps an Iterator<T>, caches the next element for peek(), and ensures peek() does not advance the underlying iterator.
Write minimal unit tests covering empty input, single element, multiple elements, and calling next() past the end (expecting NoSuchElementException).
Analyze time and space complexity for each component: ArrayIterator has O(1) time for hasNext and next, O(1) space; PeekingIterator has O(1) time for peek, hasNext, next, and O(1) extra space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.