Peeking Iterator
A medium-tier problem at 60% community acceptance, tagged with Array, Design, Iterator. Reported in interviews at Google and 0 others.
Peeking Iterator is a medium-difficulty design problem that Google has asked. The core challenge: implement an iterator wrapper that lets you see the next element without consuming it. You're not just iterating; you're building a thin abstraction layer on top of an existing iterator. This pattern shows up in parsing, stream processing, and any system where you need one-element lookahead. The trick is managing state cleanly so peek() and next() don't fight each other. With a 60% acceptance rate, it's easier than the hardest problems but trips up candidates who overthink the state machine. If this hits your live OA and you blank on the peek-cache logic, StealthCoder surfaces a working solution invisible to the proctor.
Companies that ask "Peeking Iterator"
Peeking Iterator is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe real pattern here is simple but fragile: cache the next element on first peek, then serve from cache until next() is called. Most candidates start wrong by trying to peek without consuming from the underlying iterator, or they break when next() is called after multiple peeks. The algorithm is straightforward: store a flag and a value. Peek checks the flag; if you haven't cached yet, pull from the iterator and cache it. Next returns the cached value if it exists, otherwise pulls fresh. The pitfall is handling the edge case where someone peeks past the end, or calls next after hasNext returns false. The problem teaches you that design questions aren't about algorithmic wizardry; they're about state management and clear contracts. It's a good hedge problem because it looks simple on the surface but forces you to think about invariants. If you hit it under time pressure in the assessment, StealthCoder eliminates the 10-minute debug loop and gets you a clean, testable implementation in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Peeking Iterator recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Peeking Iterator interview FAQ
Is Peeking Iterator still actively asked at Google?+
Yes. It appears in Google's interview rotation and is reported as an active problem. The 60% acceptance rate suggests it's positioned as a gating problem for design chops, not a screening weed-out. If you're interviewing there, assume it's in the pool.
What's the actual trick to this problem?+
Cache the next element on peek, and don't fetch again until next() consumes it. Most wrong answers fetch twice or lose track of whether they've peeked. The trick is a single boolean flag plus one value. It sounds trivial once you see it, which is why people blank on it live.
How does Peeking Iterator relate to broader iterator design?+
It teaches you the single-lookahead pattern used in parsing, recursive descent, and stream APIs. Understanding peek() here makes you dangerous in string processing and tree traversal problems. It's foundational design thinking, not a one-off trick.
Can I solve this in Python without an explicit cache class?+
Yes. Use instance variables: one boolean for 'cached', one for 'cached_value'. In next(), check if cached is true; if so, return cached_value and reset cached to false. Otherwise pull from the underlying iterator. Clean and testable.
Why does this problem have a 60% pass rate if the idea is so simple?+
Because people rush, forget edge cases (empty iterator, peeking twice, calling hasNext correctly), or over-design the state machine. Under OA pressure, 'simple' becomes fragile. That's where clarity of code and test thinking matter most.
Want the actual problem statement? View "Peeking Iterator" on LeetCode →