#124

2D Vector Iterator

MEDIUMDesignPYTHON

Problem Description

A data processing framework needs a lazy iterator that flattens a partitioned dataset — stored as a list of shards (inner lists) — without materializing the full flat array in memory, and supports in-place removal of already-yielded elements. Design the `Vector2D` class. **Input (constructor):** A 2D list `vec` (list of lists of integers). **Methods to implement:** - `next()` — returns the next element in row-major order across all inner lists. - `has_next()` — returns `True` if more elements remain, `False` otherwise. - `remove()` — removes the element most recently returned by `next()` from the underlying data. **Key Rules:** - Empty inner lists are skipped automatically. - `remove()` is only called after at least one `next()` call. - The iterator must not pre-flatten the data; use two-pointer lazy traversal.

Topics

arraytwo pointers

Asked at Companies

airbnb

Solve This Problem

Sign up to access the interactive code editor, run test cases, view the editorial, and get AI-powered feedback on your solution.

Start Solving →