Nested List Iterator With Remove

Asked at AirbnbPhone screen

Reported by the problem's creator, not verified

Build NestedListIterator over data, a list of lists of integers. It walks the integers in row-major order: the first inner list from left to right, then the second, and so on. Inner lists can be empty, and so can data.

Besides the usual has_next() and next(), it supports remove(), which deletes from data the integer that the most recent successful next() returned. The deletion happens in the list the caller gave you, in place, not in a copy of it, and the iterator must carry on correctly afterwards: removing an integer never makes it skip or repeat another one.

Examples

  1. Example 1

    Input: init = [[[],[1,2,3],[4,5]]], operations = [["next",[]],["next",[]],["remove",[]],["next",[]],["next",[]],["next",[]],["has_next",[]],["contents",[]]]

    Output: [1,2,null,3,4,5,false,[[],[1,3],[4,5]]]

    Explanation: The first two next() calls return 1 and 2. remove() deletes the 2 from the list, leaving [1, 3] in the middle row, and the iterator carries on with 3, 4, 5. After 5, has_next() is false, and contents() shows the list the iterator was given with the 2 gone.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.