Search in Rotated Sorted Array

Algorithms
Medium
Salesforce
148.1K views

Given a sorted array that has been rotated at some pivot, find the index of the `target` value. The algorithm should have $O(\log n)$ runtime complexity.

Why Interviewers Ask This

Salesforce asks this to evaluate a candidate's ability to adapt binary search logic to non-standard data structures. It tests whether you can handle edge cases, maintain O(log n) efficiency under constraints, and demonstrate rigorous logical reasoning when standard assumptions about sorted arrays no longer apply.

How to Answer This Question

1. Clarify the problem: Confirm input format (array of integers), rotation nature, and that duplicates are not present unless specified. 2. Visualize the structure: Explain that the array consists of two sorted sub-arrays where one is shifted. 3. Propose the Modified Binary Search strategy: Instead of checking if the middle element is the target immediately, determine which half of the array is strictly sorted. 4. Execute the logic: If the left half is sorted, check if the target lies within its range; otherwise, search the right half. Repeat for the right half if it is the sorted portion. 5. Optimize and verify: Ensure boundary conditions (single element, fully rotated) are handled and confirm the time complexity remains logarithmic throughout the process.

Key Points to Cover

  • Explicitly state how you determine which half of the array is sorted before narrowing the search space
  • Demonstrate awareness of edge cases such as single-element arrays or arrays with no rotation
  • Maintain strict O(log n) complexity by avoiding any linear scans or full array traversals
  • Clearly explain the conditional logic used to decide between searching the left or right partition
  • Show confidence in handling the pivot point transition where the order resets

Sample Answer

To solve the 'Search in Rotated Sorted Array' problem efficiently, I would implement a modified binary search algorithm that runs in O(log n) time. First, I initialize pointers at the start and end of the array. In each iteration, I calculate the mid-point. The critical insight here is determining which side of the mid-point is properly sorted. If the left half from low to mid is sorted (nums[low] <= nums[mid]), I check if the target falls within this specific range. If it does, I narrow the search to the left; otherwise, I discard the left half and search the right. Conversely, if the right half is sorted, I perform a similar check against the target. For example, in an array like [4, 5, 6, 7, 0, 1, 2], searching for 0 requires recognizing that while the left half [4, 5, 6, 7] is sorted, 0 is not in it, so we pivot to the right. This approach handles all rotation scenarios without degrading performance to linear time, ensuring we meet Salesforce's expectation for scalable algorithmic solutions even with complex data manipulations.

Common Mistakes to Avoid

  • Attempting a linear scan which results in O(n) complexity, failing the core constraint requirement
  • Failing to account for the pivot point, leading to incorrect comparisons when the target is near the rotation boundary
  • Ignoring edge cases where the array has only one element or is already sorted without rotation
  • Overcomplicating the solution by trying to find the pivot index first before searching, adding unnecessary steps

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 49 Salesforce questions