Given an array arr and a chunk size size, return a chunked array.
A chunked array contains the original elements in arr, but consists of subarrays each of length size. The length of the last subarray may be less than size if arr.length is not evenly divisible by size.
You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
Please solve it without using lodash's _.chunk function.
Example 1:
Input: arr = [1,2,3,4,5], size = 1 Output: [[1],[2],[3],[4],[5]] Explanation: The arr has been split into subarrays each with 1 element.
Example 2:
Input: arr = [1,9,6,3,2], size = 3 Output: [[1,9,6],[3,2]] Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
Example 3:
Input: arr = [8,5,3,2,6], size = 6 Output: [[8,5,3,2,6]] Explanation: Size is greater than arr.length thus all elements are in the first subarray.
Example 4:
Input: arr = [], size = 1 Output: [] Explanation: There are no elements to be chunked so an empty array is returned.
Constraints:
arr is a valid JSON array2 <= JSON.stringify(arr).length <= 1051 <= size <= arr.length + 1The goal of #2677 Chunk Array is to divide a given array into smaller groups (chunks) of a specified size. Each chunk should contain at most size elements, and the last chunk may contain fewer elements if the array length is not perfectly divisible.
A common approach is to iterate through the array while grouping elements into subarrays. You can move through the array in steps of size and collect a slice of elements for each step. Each slice becomes a new chunk that is added to the result list. This method ensures that elements are processed sequentially and placed into the correct group.
Another variation is to build chunks dynamically by adding elements until the current chunk reaches the required size, then pushing it into the result. Both strategies rely on simple array traversal.
Since each element is visited only once, the algorithm is efficient with linear time complexity. The extra space used corresponds to the output chunks created during processing.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterative chunking using slicing or step traversal | O(n) | O(n) |
Sahil & Sarra
This approach involves iterating over the array using a loop and extracting subarrays using slicing. The loop increments by the chunk size in each iteration, thus effectively slicing the array into chunks of the desired size.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), storing the entire chunked array requires space proportional to the input size.
1def chunk_array(arr, size):
2 return [arr[i:i + size] for i in range(0, len(arr), size)]The chunk_array function uses list comprehension which iterates over the array with a step size of size. It slices arr from index i to i + size in each iteration, creating subarrays of the desired chunk size.
This approach uses simple arithmetic operations to determine when to create a new subarray. Using the modulus operator allows checking if the number of currently collected elements is equal to the chunk size, upon which a new subarray is started.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), because the additional space needed is proportional to the input size.
1import
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, variations of array chunking problems appear in coding interviews, especially for testing basic array manipulation and iteration skills. They are common in entry-level or easy-level interview questions.
The optimal approach is to iterate through the array and group elements into subarrays of the given size. This can be done using index stepping or array slicing. Each element is processed once, making the time complexity linear.
A dynamic array or list is ideal for storing the resulting chunks. Each chunk is itself a subarray that holds up to the specified number of elements. This structure makes it easy to append and manage grouped elements.
The time complexity is O(n) because the algorithm processes each element in the input array exactly once. Additional operations like slicing or pushing chunks are proportional to the number of elements.
As elements are iterated over from arr, they are added to chunk. When chunk reaches the designated size, it is added to chunked and reset for new elements. Any elements remaining after completing array iteration form a final chunk.