Zigzag Iterator
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Build an iterator ZigzagIterator(v1, v2) over two integer lists that hands out their elements alternately: first v1[0], then v2[0], then v1[1], then v2[1], and so on.
When one list runs out, the iterator keeps returning the remaining elements of the other list in order. Either list may be empty from the start.
- next() returns the next element in zigzag order. It is only called when hasNext() would return true.
- hasNext() returns true while at least one element has not been returned yet.
Examples
Constraints
- 0 <= v1.length, v2.length <= 1000
- 0 <= v1.length + v2.length <= 2000
- -2^31 <= v1[i], v2[i] <= 2^31 - 1
- `next` is only called when an element remains
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.