Chunk Array - Problem

Given an array arr and a positive integer size, your task is to divide the array into smaller subarrays (chunks) where each chunk contains exactly size elements.

Think of it like organizing items into boxes: if you have 10 items and want to put 3 items per box, you'd get 3 boxes with 3 items each, and 1 box with the remaining 1 item.

Key Points:

  • Each chunk should have exactly size elements
  • The last chunk may have fewer elements if the array length isn't evenly divisible by size
  • Maintain the original order of elements
  • Return an array of subarrays (chunks)

Example: [1, 2, 3, 4, 5] with size 2 becomes [[1, 2], [3, 4], [5]]

Input & Output

example_1.py โ€” Basic chunking
$ Input: arr = [1, 2, 3, 4, 5], size = 2
โ€บ Output: [[1, 2], [3, 4], [5]]
๐Ÿ’ก Note: The array is divided into chunks of size 2. First chunk gets [1,2], second chunk gets [3,4], and the last chunk gets the remaining element [5].
example_2.py โ€” Perfect division
$ Input: arr = [1, 2, 3, 4, 5, 6], size = 3
โ€บ Output: [[1, 2, 3], [4, 5, 6]]
๐Ÿ’ก Note: The array length (6) is perfectly divisible by chunk size (3), resulting in two complete chunks of equal size.
example_3.py โ€” 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. This creates individual arrays for each element.

Visualization

Tap to expand
Warehouse Packing: Items โ†’ Boxes of Size 2CONVEYOR BELT12345Packing Process:Current Box12FULL!Sealed Box 112โœ“Sealed Box 234โœ“Final Box5PartialShipping Manifest:SHIPMENT SUMMARYBox 1: [1, 2] โœ“Box 2: [3, 4] โœ“Box 3: [5] โš ๏ธ PartialAlgorithm Steps:1. Initialize empty current box2. For each item on conveyor:โ€ข Add item to current boxโ€ข If box full โ†’ seal & start new box3. Seal final box (even if partial)
Understanding the Visualization
1
Setup
Start with an empty box and conveyor belt of items
2
Fill Box
Take items from belt and place them in current box until full
3
New Box
When box is full, seal it and start packing a new box
4
Final Box
When items run out, seal the partially filled box
Key Takeaway
๐ŸŽฏ Key Insight: Sequential processing mirrors real-world packing - we don't need to pre-calculate positions, just fill boxes naturally as items arrive!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, each element is processed exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space needed for the result array containing all elements

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค size โ‰ค 1000
  • 0 โ‰ค arr.length โ‰ค 1000
  • -1000 โ‰ค arr[i] โ‰ค 1000
  • Do not use lodash's _.chunk function
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
25.6K Views
Medium Frequency
~15 min Avg. Time
892 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