Search⌘ K
AI Features

Solution: Zigzag Iterator

C# solution for the Zigzag Iterator problem using the K-way Merge pattern.

Statement

Design an iterator class ZigzagIterator that takes two integer arrays v1 and v2 and iterates through their elements by alternating between the arrays.

The iterator must support the following methods:

  • next() returns the next integer in the alternating traversal.

  • hasNext() returns true if there is at least one remaining element to return, otherwise returns false.

When one of the arrays runs out of elements, the iterator should continue returning the remaining elements from the other array in order.

Note: For the follow up, if more than two arrays are provided, interpret the order as cyclic across the arrays, skipping any array that has no remaining elements.

Constraints:

  • 00 \leq ...