Given a multi-dimensional array arr
and a depth n
, return a flattened version of that array.
A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.
A flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than n
. The depth of the elements in the first array are considered to be 0
.
Please solve it without the built-in Array.flat
method.
Example 1:
Input arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 0 Output [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] Explanation Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened.
Example 2:
Input arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 1 Output [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15] Explanation The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.
Example 3:
Input arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 2 Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] Explanation The maximum depth of any subarray is 1. Thus, all of them are flattened.
Constraints:
0 <= count of numbers in arr <= 105
0 <= count of subarrays in arr <= 105
maxDepth <= 1000
-1000 <= each number <= 1000
0 <= n <= 1000
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:
Imagine you have a container that can hold only single items, but inside you have boxes, and some of those boxes might have more boxes inside them. To flatten the structure, we're going to unpack everything, level by level, until we only have single items in the main container. We will check each element and if the element is a box, we open it, and repeat.
Here's how the algorithm would work step-by-step:
def flatten_deeply_nested_array(nested_array):
flattened_array = []
stack_of_arrays = [nested_array]
while stack_of_arrays:
current_array = stack_of_arrays.pop()
for element in current_array:
# Check if the current element is a list
if isinstance(element, list):
# Add current element (which is a list) to the stack to process
stack_of_arrays.append(element)
else:
#If it is not a list add it to the result
flattened_array.append(element)
return flattened_array
The best way to flatten a deeply nested array is to go through each element one by one and, if you find an array, open it up and repeat the process on its contents. This continues until you're only left with individual, non-array elements. It's like peeling an onion layer by layer until you reach the core.
Here's how the algorithm would work step-by-step:
def flatten_deeply_nested_array(input_array):
flattened_result = []
def process_element(element):
# Check if the current element is a list (array).
if isinstance(element, list):
# Recursively process each item within the list.
for item in element:
process_element(item)
else:
# Append non-list elements to the flattened result.
flattened_result.append(element)
# Iterate through the input array to start the process.
for element in input_array:
process_element(element)
return flattened_result
Case | How to Handle |
---|---|
Input array is null or undefined | Return an empty array to gracefully handle invalid input. |
Input array is empty | Return the empty array directly as there's nothing to flatten. |
Depth n is 0 | Return the original array without any flattening. |
Depth n is a very large number or infinity | Treat n as fully flattening the array until no sub-arrays are left. |
Input array contains non-array elements at the top level (e.g., numbers, strings) | Treat non-array elements as already flattened and include them in the output array. |
Input array contains nested empty arrays | Handle nested empty arrays by skipping them during flattening. |
Input array contains mixed data types within nested arrays (numbers, strings, booleans, objects) | Include all elements of any type during flattening. |
Deeply nested array exceeding maximum call stack size (if using recursion) | Consider an iterative approach using a stack to avoid stack overflow errors. |