Left Rotation on an Array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A left rotation moves the first part of an array to the end while preserving order. For example, rotating [1, 2, 3, 4, 5] left by 2 gives [3, 4, 5, 1, 2].
This is a common interview problem because it tests indexing, modular arithmetic, and awareness of time and space tradeoffs.
Normalize the Rotation Count
If the array has length n, rotating left by k is the same as rotating by k % n. That matters because rotating by the full length should leave the array unchanged.
A clean Python version looks like this:
This prints:
Why Slicing Works
After normalization, the array can be split into two parts:
- the suffix starting at index
k - the prefix before index
k
Concatenating them in that order produces the rotated result. This approach is simple and correct, which makes it a great default in high-level languages.
In-Place Rotation with Reversal
If you need an in-place algorithm instead of building a new array, the classic solution is three reversals:
- reverse the first
kelements - reverse the remaining
n - kelements - reverse the whole array
Here is a Python implementation:
This runs in O(n) time and uses O(1) extra space.
Use Modular Indexing for a Read-Only View
Sometimes you do not need to physically rotate the array at all. You only need to read elements as if it had been rotated. In that case, modular indexing is enough:
This is useful in ring buffers and queue-like data structures where moving elements would be wasteful.
The same idea is important in performance-sensitive code. If rotation is only a logical view, changing an offset variable is often cheaper than shuffling the whole array in memory.
That is why many circular-buffer implementations do not rotate storage at all. They rotate the interpretation of the storage.
The data stays still; the starting index moves.
Choose the Right Approach
The best method depends on the requirement:
- use slicing when clarity matters most
- use the reversal method when you must rotate in place
- use modular indexing when you only need a rotated view
All three ideas are valid. The important part is matching the method to the actual constraint.
Common Pitfalls
- Forgetting to reduce
kwith modulo before rotating. - Mishandling the empty-array case and dividing by zero in
k % n. - Writing an in-place algorithm when a simple slice would have been clearer.
- Confusing left rotation with right rotation and moving elements in the wrong direction.
- Rebuilding the whole array repeatedly inside a loop when one normalized rotation would do.
Summary
- A left rotation by
kmoves the firstkelements to the end of the array. - Normalize the rotation count with
k % len(array). - Slicing is the simplest high-level implementation.
- The reversal algorithm gives an in-place
O(n)solution withO(1)extra space. - Sometimes modular indexing is enough and no physical rotation is needed.

