Design an iterator to flatten a 2D vector. Implement the Vector2D
class with the following methods:
Vector2D(int[][] vec)
initializes the object with the 2D vector vec
.int next()
returns the next element from the flattened 2D vector and moves the iterator ahead. You may assume that next()
call is always valid.boolean hasNext()
returns true
if there are still some elements in the vector, and false
otherwise.Example:
Input:
vec = [[1,2],[3],[4,5,6]]
Output:
[1,2,3,4,5,6]
Explanation:
Vector2D i = new Vector2D([[1,2],[3],[4,5,6]]);
i.next(); // return 1
i.next(); // return 2
i.next(); // return 3
i.hasNext(); // return true
i.hasNext(); // return true
i.next(); // return 4
i.next(); // return 5
i.next(); // return 6
i.hasNext(); // return false
Constraints:
0 <= vec.length <= 200
0 <= vec[i].length <= 500
-500 <= vec[i][j] <= 500
10^5
calls will be made to next
and hasNext
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach completely unpacks the nested structure into a single, long list. It precomputes and stores everything from the start to avoid doing any calculations later when asked for the next element.
Here's how the algorithm would work step-by-step:
class Vector2D:
def __init__(self, vector_of_vectors):
# Flatten the 2D vector into a single list upon initialization.
self.flattened_list = []
for vector in vector_of_vectors:
for element in vector:
self.flattened_list.append(element)
# Keep track of the current index in the flattened list.
self.current_index = 0
def next(self):
# Return the next element in the flattened list and advance the index.
next_element = self.flattened_list[self.current_index]
self.current_index += 1
return next_element
def hasNext(self):
# Check if there are more elements in the flattened list.
return self.current_index < len(self.flattened_list)
We want to create a way to go through a list of lists as if it were one single list. The trick is to keep track of where we are in both the outer list and the inner list, advancing as needed.
Here's how the algorithm would work step-by-step:
class Vector2D:
def __init__(self, vector_of_vectors):
self.vector_of_vectors = vector_of_vectors
self.outer_index = 0
self.inner_index = 0
def next(self):
return self.vector_of_vectors[self.outer_index][self.inner_index]
def hasNext(self):
# Iterate to the next valid position
while self.outer_index < len(self.vector_of_vectors):
if self.inner_index < len(self.vector_of_vectors[self.outer_index]):
return True
# Move to the next vector if current is exhausted
else:
self.outer_index += 1
self.inner_index = 0
return False
Case | How to Handle |
---|---|
Input vector is null or empty. | The constructor should handle the null or empty input by initializing iterators to the end, so hasNext returns false immediately. |
Input vector contains empty inner vectors. | The hasNext method should advance the outer iterator past empty inner vectors before returning true, and next() should gracefully handle this. |
Input vector contains only one inner vector, which is also empty. | hasNext should return false immediately because there are no elements. |
Input vector with a very large number of inner vectors and elements, potentially exceeding memory limits for eager flattening. | The iterator approach handles this well as it only keeps track of current iterators and not a flattened copy. |
Consecutive empty inner vectors. | The hasNext() method must correctly skip over multiple consecutive empty inner vectors. |
Integer overflow if inner vectors contain very large integers. | The problem does not explicitly require dealing with integer overflow as it only asks for iteration, so we proceed assuming integer values are in range. |
All inner vectors are of size 1 | The iterator should be initialized properly and iterate through each of the single elements. |
Maximum size of the input 2D vector based on system memory limits | The iterative nature of the solution allows handling large inputs, but excessive memory allocation may cause issues. |