Check if Array Is Sorted and Rotated
An easy Arrays interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Arrays
- Sheets
- 1
- Core for
- 26 roles
The problem
Check whether the array was originally sorted in ascending order and then rotated some number of positions.
Example 1
- Input
- [4,5,6,1,2,3]
- Output
- true
Example 2
- Input
- [1,2,3,4,5,6]
- Output
- true
Example 3
- Input
- [3,1,2,4,5]
- Output
- false
Constraints
- 1 <= n <= 100
- -10^9 <= arr[i] <= 10^9
How to think about it
Updated 2026-09-29Connecting the array into a circle reveals the underlying sorted order: an originally sorted array rotated any number of times can contain at most one drop where arr[i] > arr[i + 1], including the wrap-around comparison between the last element and the first. Counting how many times order drops across the circular boundary gives the answer immediately.
Approaches, worst first
Try all rotation offsets
time O(n^2) · space O(n)
Test all n possible rotation shifts. For each shift, verify if the resulting array is sorted in ascending order. Exits true if any shift yields a sorted configuration, taking quadratic time across all offset checks.
Single circular drop countWrite this one
time O(n) · space O(1)
Iterate i from 0 to n - 1 and count how many times arr[i] > arr[(i + 1) % n]. If the count of inversions is at most 1, the array is a valid rotated sorted array; otherwise, more than one inversion means it cannot be formed by rotating a sorted array.
Where people lose marks · 3
- Neglecting the wrap-around comparison `arr[n - 1] > arr[0]`, which fails to detect disordered arrays like [2, 1, 3, 4] where the interior drop count is 1.
- Rejecting arrays with count == 0: an unrotated sorted array has 0 drops and is completely valid under 0 rotation steps.
- Using `>=` instead of `>` when checking inversions incorrectly flags arrays containing duplicate valid values as invalid.
The theory behind it
Arrays — the ground this problem stands on. All Arrays problems
What Arrays is
An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.
When to reach for it
Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.
How the pattern works
Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.
More Arrays problems
Target Roles
Core requirement for 26 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .