EasyPythonJavaScriptTypeScript

Chunk Array

ArraysFunctions

Implement solve(arr, size) that splits arr into consecutive sub-arrays
of at most size elements. The last chunk may be smaller if the array
doesn't divide evenly.

Examples

  • solve([1,2,3,4,5], 2)[[1,2],[3,4],[5]]
  • solve([], 3)[]
  • solve([1,2], 5)[[1,2]]

Constraints

  • size >= 1
  • Don't mutate the input array.

Sample tests

Test #1Empty array
Input: [[],3]
Output: []
Test #2Standard split with remainder
Input: [[1,2,3,4,5],2]
Output: [[1,2],[3,4],[5]]
Test #3Chunk size equals array length
Input: [[1,2,3],3]
Output: [[1,2,3]]
Test #4Size 1 → each element is its own chunk
Input: [[1,2,3,4],1]
Output: [[1],[2],[3],[4]]
Test #5Strings with uneven split
Input: [["a","b","c","d","e","f"],4]
Output: [["a","b","c","d"],["e","f"]]