Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Rotate a Queue

Queuesmedium

Implement `rotateQueue(values, k)` that dequeues and re-enqueues the front `k` times, returning the resulting queue. `k` may exceed the length.

Sample tests

Input: rotateQueue([1,2,3,4], 1)
Output: [2,3,4,1]
Input: rotateQueue([1,2,3], 0)
Output: [1,2,3]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Not reducing k modulo length does needless extra work.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

Moving the front to the back k times rotates the queue left by k (after reducing k modulo the length).

Loading...
⌘/Ctrl + Enter

Run your code to see results.