Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Rotate a queue by N steps

Rotate the elements of a queue (list) by N steps to the left.

Python practice8 minStacks & QueuesBeginnerLast updated March 27, 2026

Problem statement

Given a list representing the contents of a queue (front at index 0), rotate it left by k steps and return the resulting list. A single left rotation removes the front element and appends it to the back. If k is larger than the queue length, rotation wraps around. Handle empty lists and k values of zero.

Task

Implement rotate_queue(lst, k) that performs k left rotations (front element moves to back) efficiently.

Examples

Rotate by 2

Input

rotate_queue([1,2,3,4,5], 2)

Output

[3, 4, 5, 1, 2]

Two left rotations move 1 and 2 to the back in order.

Input format

Two arguments: a list of values (the queue) and a non-negative integer k.

Output format

A list representing the queue after k left rotations.

Constraints

- 0 <= k - 0 <= len(lst) <= 10^5 (rotate should handle k larger than len(lst) efficiently) - Values can be any valid Python objects (tests will use ints).

Samples

Sample 1

Input

rotate_queue([1,2,3], 4)

Output

[2, 3, 1]

4 mod 3 = 1 left rotation -> [2,3,1].