Magicsheet logo

Zigzag Iterator

Medium
96.4%
Updated 6/1/2025

Zigzag Iterator

What is this problem about?

The Zigzag Iterator coding problem is a structural design challenge that requires creating a specialized data retrieval mechanism. Given two or more lists, you must implement an iterator that yields elements in an alternating fashion. For example, given two lists, you would return the first element of the first list, then the first of the second, then the second of the first, and so on. The core difficulty lies in managing the internal state when lists are of unequal lengths. Once one list is exhausted, the iterator must gracefully continue with the remaining lists, focusing on the Iterator design pattern and state management.

Why is this asked in interviews?

Interviewers at Google and Amazon use this Zigzag Iterator interview question to evaluate a candidate's ability to design clean, stateful classes. It moves beyond simple algorithms and into the realm of software engineering and API design. It tests how you manage multiple pointers and how you handle the lifecycle of data sources. The problem specifically highlights your ability to use appropriate data structures—like queues—to manage the "round-robin" selection process efficiently, demonstrating a level of technical maturity highly valued in systems engineering roles.

Algorithmic pattern used

The most effective algorithmic pattern for this problem is the Array, Design, Iterator, Queue pattern. A robust solution involves using a Queue to store iterators for each non-empty list. For the next() operation, you dequeue the iterator at the front, retrieve its element, and if it still has remaining items, you enqueue it back at the end. This naturally implements the zigzag behavior. The hasNext() operation simply checks if the queue is empty. This design is highly scalable, easily handling k input lists without changing the core logic.

Example explanation

Consider List A = [1, 2] and List B = [3, 4, 5, 6].

  1. Initially, our queue holds iterators for A and B.
  2. next() dequeues A, returns 1, and re-enqueues A.
  3. next() dequeues B, returns 3, and re-enqueues B.
  4. next() dequeues A, returns 2. A is now empty, so it's not re-enqueued.
  5. The iterator continues with only B in the queue, returning 4, 5, and then 6. The final sequence is [1, 3, 2, 4, 5, 6], correctly alternating until A is exhausted.

Common mistakes candidates make

A common mistake is trying to manage state with too many conditional "if-else" statements and pointers. This leads to brittle code that fails when lists are empty or of drastically different lengths. Another error is failing to handle empty input lists correctly during initialization; an empty list should not be added to the queue at all. Some candidates also forget to advance internal pointers after returning a value, leading to infinite loops or repetitive data retrieval.

Interview preparation tip

To excel in the Zigzag Iterator interview question, practice implementing the Iterator pattern in your language (e.g., Iterator in Java or generators in Python). Focus on the Queue-based approach, as it is the most robust and scalable solution. During the interview, explain why you chose a queue over simple pointers—this shows you are thinking about clean design. Also, consider edge cases like all lists being empty, as these are common follow-up questions.

Similar Questions