Skip to interview prep
Apexed

24. Reverse the First K Items of a Queue

EasyQueues

A queue is given as an array whose index 0 is the front. Reverse the order of the first k items and keep the rest in their original order. Return the resulting queue as an array, front first.

Use only queue operations (remove from the front, add to the back) and one stack.

Example 1

Input: queue = [1, 2, 3, 4, 5], k = 3
Output: [3, 2, 1, 4, 5]

Constraints

  • 0 ≤ k ≤ queue.length ≤ 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Reverse the First K Items of a Queue — coding interview practice — Apexed