Code RoomReverse blocks of k
MediumPrep Room Coding #619

Reverse blocks of k

CodingAlgorithms & data structuresEntry–Mid~13 min

Given an integer array nums and a positive integer k, reverse every consecutive block of k elements in place: the first k elements get reversed, then the next k, and so on. If fewer than k elements remain at the end, reverse that shorter block too. Return the array. Example: nums = [1, 2, 3, 4, 5], k = 2 gives [2, 1, 4, 3, 5] — and with k = 3 it gives [3, 2, 1, 5, 4].

Implement
reverse_blocks(nums: list[int], k: int) → list[int]
Examples
in[[1,2,3,4,5],2]out[2,1,4,3,5]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 13 min
InputExpectedGot
[[1,2,3,4,5],2][2,1,4,3,5]not run yetsample