# Find the number of rotations in a circularly sorted array

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/1139/find-number-of-rotations-circularly-sorted-array
Source observed: 2026-11-05T00:13:41.165Z
Last checked: 2026-11-05T00:13:41.165+00:00
Indexed: 2026-11-05T23:13:20.013+00:00

---

# Find the number of rotations in a circularly sorted array

We provide this **Medium** Data Structures & Algorithms coding question for interview practice. It asks you to determine how many right rotations transformed an ascending sorted array into its current circularly sorted form.

[All Questions](https://www.aced.io/questions)

## Problem

A circularly sorted array was sorted in ascending order and then rotated, moving elements from the end of the array to the beginning. Write a function that determines the number of rotations applied to the original sorted array.

## Constraints

- The array contains distinct integers.
- The array may contain any number of elements, including an empty array.

## Examples

```text
arr = [4, 5, 1, 2, 3]
output: 2
Explanation: the sorted array [1, 2, 3, 4, 5] has been rotated 2 times to become [4, 5, 1, 2, 3]

arr = [1, 2, 3, 4, 5]
output: 0
Explanation: the array is already sorted and has not been rotated, so the output is 0
```

The page also contains two expandable challenge sections labeled **Challenge 1** and **Challenge 2**. The detailed challenge text is not shown in the supplied rendered evidence.

## Question metadata

The question is associated with **4 interviews**, **Data Structures & Algorithms**, and a reported last-asked interval of **8 months ago**. It is associated with the roles and companies below:

- Roles: [Software Engineer](https://www.aced.io/questions?role=swe), [Data Engineer](https://www.aced.io/questions?role=data-engineer)
- Companies: [AMD](https://www.aced.io/questions?company=amd), [Visa](https://www.aced.io/questions?company=visa), [Atlassian](https://www.aced.io/questions?company=atlassian), [Microsoft](https://www.aced.io/questions?company=microsoft)
- Categories: [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms), [Coding](https://www.aced.io/questions?type=coding)

The page provides Question, Solution, Mock Interview, and Community tabs. Detailed company statistics and the Solution and Mock Interview content require an upgrade: [Upgrade](https://www.aced.io/upgrade?src=questions).

## Community solutions

The page includes community submissions in Java, Swift, Python, and Java-like syntax. These are member contributions, not a single official solution.

### Java — Abhinav K.

[Abhinav K.](https://www.aced.io/users/12b0bcae-589f-482d-ba23-d892ee67b85f) — member, 183 karma; September 6, 2026.

```java
import java.io.*;
import java.util.*;
class Solution {
    static int findRotations(int[] arr) {
        // your code goes here
        int n = arr.length;
        if (n==0){ return 0; }
        int start = 0;
        int end = (n-1);
        while(start < end){
            int mid = start + (end - start) / 2;
            if (arr[mid] > arr[end]){
                start = mid + 1;
            } else{
                end = mid;
            }
        }
        return start;
    }
    public static void main(String[] args) {
        // debug your code below
        int[] arr = {3, 4, 5, 1, 2};
        System.out.println(findRotations(arr));
        // Output should be the number of rotations
    }
}
```

### Swift — Reno S.

[Reno S.](https://www.aced.io/users/8e9bd83e-e5d1-4b9e-adb7-b38eee0fe9b9) — 3,314 karma; April 7, 2025.

```swift
func findRotations(_ arr: [Int]) -> Int {
    guard !arr.isEmpty else { return 0 }
    if arr[0] < arr[arr.count - 1] { return 0 }
    var start = 0, end = arr.count - 1
    while start < end {
        let mid = start + (end - start) / 2
        if mid < arr.count - 1 && arr[mid] > arr[mid + 1] { return mid + 1 }
        arr[start] <= arr[mid] ? (start = mid + 1) : (end = mid)
    }
    return 0
}
```

### Java-like syntax — Divya R.

[Divya R.](https://www.aced.io/users/2747b128-4fd6-4813-827b-b847aa60d604) — 807 karma; March 12, 2025.

```java
static int findRotations(int[] arr) {
    if(arr==null || arr.length==0) return 0;
    int curr=1;
    while(curr<arr.length && arr[curr]>=arr[curr-1]) curr++;
    return curr==arr.length ? 0 : curr;
}
```

### Python — Abinash S.

[Abinash S.](https://www.aced.io/users/724db58b-5194-48ac-93c8-22912a7d4d84) — member, 191 karma; January 19, 2026.

```python
from typing import List

def find_rotations(nums: List[int]) -> int:
    left = 0
    right = len(nums) - 1
    while left < right:
        mid = (left + right) // 2
        if nums[mid] > nums[right]:
            left = mid + 1
        elif nums[mid] == nums[right]:
            right -= 1
        else:
            right = mid
    return left

# debug your code below
print(find_rotations([2, 3, 4, 1]))
```

### Python — Benjamin

[Benjamin](https://www.aced.io/users/69219270-1a6d-4e86-ace6-f863a086ef2f) — member, 65 karma; May 3, 2026.

```python
from typing import List

def find_rotations(nums: List[int]) -> int:
    if len(nums) == 0:
        return 0
    left, right = 0, len(nums) - 1
    while left <= right:
        mid = (left + right) // 2
        if mid > 0 and nums[mid - 1] > nums[mid]:
            return mid
        elif nums[mid] < nums[left]:
            right = mid - 1
        else:
            left = mid + 1
    return 0

print(find_rotations([2, 3, 4, 1]))
```

## Interview experiences

We show **2 shared** interview experiences related to this question:

- [AMD Software Engineer interview](https://www.aced.io/experiences/amd-software-engineer-interview-bf13c0) — AMD, 8 months ago. The candidate describes two technical rounds lasting about 5.5 hours, covering problem solving, C++ fundamentals, data structures, memory management, and operating-system concepts. Coding problems began simply and became more constrained; the candidate says explaining thought processes, trade-offs, and initial solutions went well, while deeper low-level-system and optimization follow-ups were more difficult.
- [Atlassian Senior Data Engineer interview](https://www.aced.io/experiences/atlassian-senior-data-engineer-interview-b07d95) — Atlassian, 2 years ago. The candidate describes a smooth process with a slow recruiter, a coding screening before the loop, and an interview centered on past projects and Spark optimization.

## Related questions

- [Find the number of 1's in a sorted binary array.](https://www.aced.io/questions/1141/find-number-of-1s-sorted-binary-array)
- [Find the closest number in a sorted array.](https://www.aced.io/questions/3266/closest-number-sorted-array)
- [Find the first or last occurrence of a given number in a sorted array.](https://www.aced.io/questions/1140/find-first-last-occurrence-given-number-sorted-array)

## Related courses

![Data Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/5XoOCgXlGqN2fa1zaroNWd/9a2e23df48a212bc00d352873feb2d6a/Data_Engineer_Interview_Prep_Hero_Image.jpg?w=800&h=800&fm=webp&q=75)

### [Data Engineering Interview Prep](https://www.aced.io/courses/data-engineering)

Our course covers mock interviews, frameworks, and advice from senior candidates, with practice in data modeling, pipeline design, SQL, coding, behavioral interviews, and more. It is positioned for data-engineering roles at Meta, Google, Amazon, Microsoft, Walmart, DoorDash, and other top companies.

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

Our course covers mock interviews, frameworks, and advice from senior candidates, with practice in data structures, algorithms, system design, people management, behavioral interviews, and more. It is positioned for software-engineering roles at Google, Amazon, Microsoft, Meta, Apple, and other top companies.

[Sign up to run code](https://www.aced.io/signup?src=lesson)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1139%2Ffind-number-of-rotations-circularly-sorted-array&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1139%2Ffind-number-of-rotations-circularly-sorted-array). Opening a form does not execute a tool.