TypeScriptMediumCollections25 min

Split an array into typed chunks

Split values into consecutive chunks of positive size (1 to 10000). Only the final chunk may be shorter; use a generic chunk helper. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: { values: (string | number)[]; size: number }. See the sample for its exact shape.

Output

One compact JSON value of type (string | number)[][]. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

{"values":[1,2,3,4,5],"size":2}

Output

[[1,2],[3,4],[5]]

Example 2 input

{"values":[1,2,3],"size":10}

Output

[[1,2,3]]

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    {"values":[1,2,3,4,5],"size":2}

    Expected output

    [[1,2],[3,4],[5]]
  • Input

    {"values":[],"size":1}

    Expected output

    []
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Slice the array at increments of the requested size

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs