Skip to main content
All Questions

Find the number of rotations in a circularly sorted array.

Medium
Unlock detailed company stats for this questionUpgrade

Find the number of rotations in a circularly sorted array. A circularly sorted array is an array that has been sorted in ascending order but then rotated, meaning that the elements at the end of the array have been moved to the beginning.

Write a function that determines the number of rotations the original sorted array has undergone.

Constraints

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

Examples

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

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.