Watch 10 video solutions for Chunk Array, a easy level problem. This walkthrough by NeetCodeIO has 5,145 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 + 1Problem Overview: You’re given an array arr and a chunk size size. The goal is to split the array into smaller arrays where each chunk contains at most size elements. If the final group has fewer elements than size, it should still be included as the last chunk.
Approach 1: Iterative Slicing (Time: O(n), Space: O(n))
The most direct solution walks through the array in steps of size and extracts subarrays using slicing. Start at index 0, take a slice from i to i + size, append it to the result, then move forward by size. This continues until the end of the array. Because every element is copied into exactly one chunk, the algorithm runs in O(n) time. The output array stores all elements again in nested form, so the extra space usage is also O(n).
This approach is easy to reason about and maps directly to how the problem is described. Languages like Python and JavaScript make it especially clean because slicing operations are built in. If you’re comfortable with basic array traversal and iteration, this is usually the first solution you’d implement in an interview or production code.
Approach 2: Using Modulo and Remainder Logic (Time: O(n), Space: O(n))
Another method processes the array one element at a time while dynamically building the current chunk. Maintain a temporary list representing the current group. As you iterate through the array, push each element into this list. When the index satisfies i % size == 0 (or when the current chunk reaches the desired length), start a new chunk and append the previous one to the result.
The key idea relies on modulo arithmetic to determine chunk boundaries. Every size elements, a new group begins. This avoids slicing operations and instead builds chunks incrementally. Time complexity remains O(n) because each element is processed once. Space complexity is O(n) due to storing the resulting chunked structure.
This version can be useful in languages where slicing is expensive or unavailable. It also provides more control if chunk construction involves additional logic such as filtering, transformation, or streaming data processing.
Recommended for interviews: Interviewers generally expect the iterative slicing solution because it’s concise and clearly communicates intent. Starting with the straightforward iteration approach demonstrates strong understanding of array traversal. Mentioning the modulo-based grouping afterward shows deeper awareness of alternative implementations and edge-case handling.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Slicing | O(n) | O(n) | Best for readability and languages with built-in slicing like Python or JavaScript |
| Modulo and Remainder Logic | O(n) | O(n) | When building chunks incrementally or when slicing operations are not ideal |