L2 · Working engineerLists & arrays~8 min · 6 tests#7

Rotate a List Right by K Steps

Rotate a Python list to the right by k positions, including k larger than the list. Slicing vs the reversal trick, with O(n) analysis.

The problem

Rotate nums to the right by k steps and return the rotated list.

k can be larger than len(nums), and the list can be empty.

Examples

  1. Example 1

    Input

    rotate_right([1, 2, 3, 4, 5, 6], 3)

    Expected output

    [4, 5, 6, 1, 2, 3]
  2. Example 2

    Input

    rotate_right([1, 2, 3], 1)

    Expected output

    [3, 1, 2]

+ 4 hidden tests on Submit — k == len, k > len, empty.

Edge cases to ask about

  • k = 0
  • k equal to length
  • k larger than length
  • Empty list

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · rotate_right
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Rotate one step k timesO(n·k)O(n)insert(0, …) shifts every element, k times over.
    Slice and joinO(n)O(n)After k %= n, the last k elements move to the front.
    bestThree reversals (in place)O(n)O(1)Reverse all, reverse the first k, reverse the rest.
    Walkthrough of the optimal approach (try it yourself first)

    First reduce k with k %= len(nums) — rotating a full lap changes nothing. Then the answer is the last k elements followed by the rest: nums[-k:] + nums[:-k].

    Watch the k == 0 trap: nums[-0:] is the whole list. For an in-place O(1)-space answer, reverse the whole list, then reverse the first k, then the remaining n − k.

    Complexity: O(n) time, O(n) space. Slicing copies each element once. The in-place three-reversal version gets space down to O(1).

    Reveal the reference solution
    def rotate_right(nums, k):
        if not nums:
            return []
        k %= len(nums)
        return nums[-k:] + nums[:-k] if k else nums[:]

    The brute force, for comparison

    def rotate_right(nums, k):
        out = nums[:]
        for _ in range(k):
            if out:
                out.insert(0, out.pop())    # one step at a time
        return out

    Follow-ups interviewers ask

    • Do it in place with O(1) extra memory.
    • Rotate left instead.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Rotate a List Right by K Steps in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. Slicing copies each element once. The in-place three-reversal version gets space down to O(1).

    What is the brute-force approach, and how do you optimise it?

    Rotate one step k times: O(n·k) time, O(n) space. insert(0, …) shifts every element, k times over. Slice and join: O(n) time, O(n) space. After k %= n, the last k elements move to the front. Three reversals (in place): O(n) time, O(1) space. Reverse all, reverse the first k, reverse the rest.

    What follow-up questions do interviewers ask about Rotate a List Right by K Steps?

    Do it in place with O(1) extra memory. Rotate left instead.