Bird
Raised Fist0
DSA C++programming~20 mins

Search in Rotated Sorted Array in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rotated Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of search in rotated sorted array
What is the output of the following C++ code that searches for a target in a rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) return mid;
        if (nums[left] <= nums[mid]) {
            if (nums[left] <= target && target < nums[mid]) right = mid - 1;
            else left = mid + 1;
        } else {
            if (nums[mid] < target && target <= nums[right]) left = mid + 1;
            else right = mid - 1;
        }
    }
    return -1;
}

int main() {
    vector<int> arr = {4,5,6,7,0,1,2};
    int target = 0;
    cout << search(arr, target);
    return 0;
}
A-1
B0
C4
D6
Attempts:
2 left
💡 Hint
Think about where the target 0 is located in the rotated array and how the binary search narrows down the search space.
❓ Predict Output
intermediate
2:00remaining
Output of search for missing element
What is the output of the following code when searching for a target not present in the rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) return mid;
        if (nums[left] <= nums[mid]) {
            if (nums[left] <= target && target < nums[mid]) right = mid - 1;
            else left = mid + 1;
        } else {
            if (nums[mid] < target && target <= nums[right]) left = mid + 1;
            else right = mid - 1;
        }
    }
    return -1;
}

int main() {
    vector<int> arr = {4,5,6,7,0,1,2};
    int target = 3;
    cout << search(arr, target);
    return 0;
}
A6
B-1
C0
D3
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🧠 Conceptual
advanced
1:30remaining
Why binary search works on rotated sorted arrays
Why can binary search be applied to a rotated sorted array to find a target efficiently?
ABecause at least one half of the array is always sorted, allowing us to decide which half to search next.
BBecause the array is fully sorted, so normal binary search applies without changes.
CBecause the array elements are all unique, so we can use hashing instead.
DBecause the array is rotated, we must search both halves every time.
Attempts:
2 left
💡 Hint
Think about how the array is split and what property remains after rotation.
🔧 Debug
advanced
2:00remaining
Identify the bug in rotated array search code
What error will the following code produce when searching in a rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) return mid;
        if (nums[left] <= nums[mid]) {
            if (nums[left] <= target && target < nums[mid]) right = mid - 1;
            else left = mid + 1;
        } else {
            if (nums[mid] < target && target <= nums[right]) left = mid + 1;
            else right = mid - 1;
        }
    }
    return -1;
}
AInfinite loop because 'left' and 'right' never change.
BSyntax error due to missing semicolon.
CReturns wrong index because mid calculation is incorrect.
DMay miss the target if it is at index 'right' due to loop condition 'left < right'.
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when left equals right.
🚀 Application
expert
3:00remaining
Find minimum element index in rotated sorted array
Given a rotated sorted array with unique elements, which code snippet correctly finds the index of the minimum element?
A
int findMinIndex(const vector&lt;int&gt;&amp; nums) {
    int left = 0, right = nums.size() - 1;
    while (left &lt; right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] &gt; nums[right]) left = mid + 1;
        else right = mid;
    }
    return left;
}
B
int findMinIndex(const vector&lt;int&gt;&amp; nums) {
    int left = 0, right = nums.size() - 1;
    while (left &lt;= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] &lt; nums[left]) right = mid - 1;
        else left = mid + 1;
    }
    return right;
}
C
int findMinIndex(const vector&lt;int&gt;&amp; nums) {
    int minIndex = 0;
    for (int i = 1; i &lt; nums.size(); i++) {
        if (nums[i] &lt; nums[minIndex]) minIndex = i;
    }
    return minIndex;
}
D
int findMinIndex(const vector&lt;int&gt;&amp; nums) {
    int left = 0, right = nums.size() - 1;
    while (left &lt; right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] &lt; nums[left]) right = mid;
        else left = mid + 1;
    }
    return right;
}
Attempts:
2 left
💡 Hint
The minimum element is the only element where the previous element is greater, or it is the smallest in the rotated array.

Practice

(1/5)
1. What is the main challenge when searching for an element in a rotated sorted array compared to a normal sorted array?
easy
A. The array is not fully sorted, so normal binary search cannot be directly applied.
B. The array contains duplicate elements which confuse the search.
C. The array size is unknown, so we cannot set search boundaries.
D. The array elements are all negative numbers.

Solution

  1. Step 1: Understand normal binary search assumption

    Binary search works efficiently on fully sorted arrays by comparing mid element to target.
  2. Step 2: Identify the problem with rotated arrays

    Rotated sorted arrays are sorted but shifted, so the order is broken at the rotation point, making direct binary search invalid.
  3. Final Answer:

    The array is not fully sorted, so normal binary search cannot be directly applied. -> Option A
  4. Quick Check:

    Rotated array breaks sorted order = need modified search [OK]
Hint: Look for the rotation point to adjust binary search [OK]
Common Mistakes:
  • Assuming array is fully sorted and applying normal binary search
  • Ignoring rotation and searching linearly
  • Confusing duplicates with rotation
2. Which of the following is the correct syntax to start a binary search loop for a rotated sorted array in C++?
easy
A. if (arr[mid] == target)
B. for (int i = 0; i < n; i++)
C. while (start <= end)
D. do { ... } while (start < end)

Solution

  1. Step 1: Identify loop type for binary search

    Binary search uses a while loop with condition start <= end to narrow search range.
  2. Step 2: Check options for correct loop syntax

    while (start <= end) uses while (start <= end), which is standard for binary search loops.
  3. Final Answer:

    while (start <= end) -> Option C
  4. Quick Check:

    Binary search loop uses while (start <= end) [OK]
Hint: Binary search loops use while (start <= end) [OK]
Common Mistakes:
  • Using for loop instead of while for binary search
  • Using incorrect loop condition like start < end
  • Confusing if statement with loop
3. What will be the output of the following C++ code snippet?
int arr[] = {4,5,6,7,0,1,2};
int target = 0;
int start = 0, end = 6;
int result = -1;
while (start <= end) {
  int mid = start + (end - start) / 2;
  if (arr[mid] == target) {
    result = mid;
    break;
  }
  if (arr[start] <= arr[mid]) {
    if (target >= arr[start] && target < arr[mid])
      end = mid - 1;
    else
      start = mid + 1;
  } else {
    if (target > arr[mid] && target <= arr[end])
      start = mid + 1;
    else
      end = mid - 1;
  }
}
std::cout << result << std::endl;
medium
A. 3
B. 0
C. -1
D. 4

Solution

  1. Step 1: Trace initial variables and mid calculation

    start=0, end=6, mid=3 (element 7). arr[mid] != target (0).
  2. Step 2: Check which half is sorted and adjust search

    arr[start]=4 <= arr[mid]=7, left half sorted. Target 0 not in [4,7), so search right half: start=mid+1=4.
  3. Step 3: Next iteration mid=5 (element 1)

    arr[mid]=1 != target. arr[start]=0 <= arr[mid]=1, left half sorted. Target 0 in [0,1), so end=mid-1=4.
  4. Step 4: Next iteration mid=4 (element 0)

    arr[mid]=0 == target, result=4, break loop.
  5. Final Answer:

    4 -> Option D
  6. Quick Check:

    Target 0 found at index 4 [OK]
Hint: Check sorted half and adjust start/end accordingly [OK]
Common Mistakes:
  • Not updating start/end correctly based on sorted half
  • Confusing indices and returning wrong index
  • Missing break after finding target
4. Identify the bug in the following code snippet for searching in a rotated sorted array:
int search(int arr[], int n, int target) {
  int start = 0, end = n - 1;
  while (start < end) {
    int mid = start + (end - start) / 2;
    if (arr[mid] == target) return mid;
    if (arr[start] <= arr[mid]) {
      if (target >= arr[start] && target < arr[mid])
        end = mid - 1;
      else
        start = mid + 1;
    } else {
      if (target > arr[mid] && target <= arr[end])
        start = mid + 1;
      else
        end = mid - 1;
    }
  }
  return -1;
}
medium
A. The mid calculation is incorrect and can cause overflow.
B. The loop condition should be while (start <= end) instead of while (start < end).
C. The target comparison should use > instead of >=.
D. The function does not handle empty arrays.

Solution

  1. Step 1: Analyze loop condition impact

    Using while (start < end) misses checking when start == end, possibly skipping last element.
  2. Step 2: Correct loop condition for complete search

    Changing to while (start <= end) ensures all elements are checked, including when start equals end.
  3. Final Answer:

    The loop condition should be while (start <= end) instead of while (start < end). -> Option B
  4. Quick Check:

    Loop must cover start == end to check last element [OK]
Hint: Use while (start <= end) for full binary search coverage [OK]
Common Mistakes:
  • Using while (start < end) and missing last element
  • Ignoring overflow in mid calculation (not in this code though)
  • Incorrect comparison operators
5. Given a rotated sorted array arr = {15, 18, 2, 3, 6, 12}, write a C++ function to find the index of the minimum element. What is the output of your function?
hard
A. 2
B. 0
C. 5
D. 3

Solution

  1. Step 1: Understand the minimum element in rotated array

    The minimum element is the rotation point where order breaks. Here, 2 at index 2 is smallest.
  2. Step 2: Use binary search to find minimum

    Check mid and compare with end to decide which half contains minimum. Eventually, index 2 is found.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Minimum element 2 is at index 2 [OK]
Hint: Compare mid element with end to find rotation point [OK]
Common Mistakes:
  • Returning first element index instead of minimum
  • Confusing maximum with minimum element
  • Not handling rotation correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard