0
0
DSA C++programming~20 mins

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

Choose your learning style9 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.