0
0
DSA Goprogramming~20 mins

Search in Rotated Sorted Array in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rotated Array 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 Go code that searches for a target in a rotated sorted array?
DSA Go
package main
import "fmt"

func search(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := (left + right) / 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
}

func main() {
    nums := []int{4,5,6,7,0,1,2}
    target := 0
    fmt.Println(search(nums, target))
}
A4
B0
C-1
D6
Attempts:
2 left
💡 Hint
Remember the array is rotated; the target 0 is not at the start.
Predict Output
intermediate
2:00remaining
Output when target is not in rotated array
What is the output of this Go code searching for a target not present in the rotated sorted array?
DSA Go
package main
import "fmt"

func search(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := (left + right) / 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
}

func main() {
    nums := []int{4,5,6,7,0,1,2}
    target := 3
    fmt.Println(search(nums, target))
}
A6
B3
C0
D-1
Attempts:
2 left
💡 Hint
Target 3 is not in the array.
🔧 Debug
advanced
2:00remaining
Identify the error in rotated array search code
What error will this Go code produce when searching in a rotated sorted array?
DSA Go
package main
import "fmt"

func search(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := (left + right) / 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
            }
        }
    }
    if nums[left] == target {
        return left
    }
    return -1
}

func main() {
    nums := []int{4,5,6,7,0,1,2}
    target := 0
    fmt.Println(search(nums, target))
}
AReturns -1 incorrectly
BInfinite loop occurs
CReturns correct index 4
DCompilation error due to missing colon
Attempts:
2 left
💡 Hint
Check the loop condition and boundary checks carefully.
🧠 Conceptual
advanced
1:30remaining
Why binary search works on rotated sorted arrays
Why can binary search be adapted to find an element in a rotated sorted array?
ABecause the array elements are all unique, so linear search is faster
BBecause the array is fully sorted, so normal binary search applies directly
CBecause one half of the array is always sorted, allowing elimination of half the search space
DBecause the rotation does not change the array length
Attempts:
2 left
💡 Hint
Think about how the array is split after rotation.
🚀 Application
expert
2:30remaining
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
left, right := 0, len(nums)-1
for left &lt;= right {
    mid := (left + right) / 2
    if nums[mid] &lt; nums[left] {
        right = mid - 1
    } else {
        left = mid + 1
    }
}
return right
B
left, right := 0, len(nums)-1
for left &lt; right {
    mid := (left + right) / 2
    if nums[mid] &gt; nums[right] {
        left = mid + 1
    } else {
        right = mid
    }
}
return left
C
left, right := 0, len(nums)-1
for left &lt; right {
    mid := (left + right) / 2
    if nums[mid] &lt; nums[left] {
        right = mid
    } else {
        left = mid + 1
    }
}
return left
D
left, right := 0, len(nums)-1
for left &lt;= right {
    mid := (left + right) / 2
    if nums[mid] &gt; nums[left] {
        left = mid + 1
    } else {
        right = mid - 1
    }
}
return left
Attempts:
2 left
💡 Hint
The minimum element is the only element smaller than its previous element in the rotated array.