Chunk Array - Problem

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 not use lodash's _.chunk function.

Input & Output

Example 1 — Basic Chunking
$ Input: arr = [1,2,3,4,5], size = 2
Output: [[1,2],[3,4],[5]]
💡 Note: Split array into chunks of size 2: first chunk [1,2], second chunk [3,4], last chunk [5] has only 1 element
Example 2 — Perfect Division
$ Input: arr = [1,2,3,4,5,6], size = 3
Output: [[1,2,3],[4,5,6]]
💡 Note: Array length 6 divides evenly by size 3, creating two complete chunks of 3 elements each
Example 3 — Single Element Chunks
$ Input: arr = [1,2,3], size = 1
Output: [[1],[2],[3]]
💡 Note: Each element becomes its own chunk when size is 1

Constraints

  • 1 ≤ arr.length ≤ 1000
  • 1 ≤ size ≤ arr.length
  • -1000 ≤ arr[i] ≤ 1000

Visualization

Tap to expand
Chunk Array Problem OverviewSplit array into fixed-size subarraysInput Array:12345size = 2Chunked Result:[1,2][3,4][5]Final Output: [[1,2],[3,4],[5]]Last chunk may be smaller if array length not divisible by size
Understanding the Visualization
1
Input
Array [1,2,3,4,5] with chunk size 2
2
Process
Split into chunks of size 2
3
Output
Result [[1,2],[3,4],[5]]
Key Takeaway
🎯 Key Insight: Use array slicing to extract fixed-size chunks efficiently
Asked in
Google 25 Amazon 20 Microsoft 15
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen