Level 1 Walking a jagged 2D list
Build an iterator over a list of rows of integers that hands out the numbers one at a time: all of row 0 from left to right, then row 1, and so on. Rows can have different lengths, and any row may be empty.
class Vector2D:
def __init__(self, rows: list[list[int]]): ...
def has_next(self) -> bool: ...
def next(self) -> int: ...
has_next()tells whether another number is left. Calling it repeatedly must not skip anything.next()returns the next number and advances. It is only called whenhas_next()would returnTrue.- Don't build a flattened copy (use O(1) extra space), and don't modify the caller's lists.
⭐ Bonus: rescanning from the first row each time still passes; a speed test with 300,000 mostly empty rows (calling has_next() before every next()) earns a star if both methods are amortised O(1).
it = Vector2D([[1, 2], [], [3], [], []])
it.next() # 1
it.next() # 2
it.has_next() # True
it.has_next() # True
it.next() # 3
it.has_next() # False: trailing empty rows hold nothing
Show hint
Keep two indexes (row, col) into rows. Write a helper that moves (row, col) forward past exhausted and empty rows, and call it from both methods.