FrontendInterviews.dev

Loading problem…

74. Chunk Array

Easy•
Acceptance: 71.43%
•
🔓3/3 Pro unlocks today

Implement chunk - split an array into contiguous sub-arrays of a fixed size.

Requirements

1) Input

  • Accept an array items and a chunk size size.

2) Output behavior

  • Return a new array of chunks in the same element order.
  • Each chunk has at most size elements.
  • The final chunk may be shorter when there are not enough remaining items.

3) Edge cases

  • If items is empty, return [].
  • If size <= 0, return [].
  • If size is larger than the input length, return one chunk containing all elements.

Constraints

  • Preserve input order inside and across chunks.
  • Return [] for non-positive chunk sizes.
  • Handle empty input without errors.