0
0
DSA Goprogramming~20 mins

Find Minimum 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
Find minimum in rotated sorted array - simple case
What is the output of the following Go code that finds the minimum element in a rotated sorted array?
DSA Go
package main
import "fmt"

func findMin(nums []int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := left + (right-left)/2
        if nums[mid] > nums[right] {
            left = mid + 1
        } else {
            right = mid
        }
    }
    return nums[left]
}

func main() {
    arr := []int{4,5,6,7,0,1,2}
    fmt.Println(findMin(arr))
}
A0
B1
C4
D7
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element in a rotated array.
Predict Output
intermediate
2:00remaining
Find minimum in rotated sorted array - no rotation
What is the output of the following Go code when the array is not rotated?
DSA Go
package main
import "fmt"

func findMin(nums []int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := left + (right-left)/2
        if nums[mid] > nums[right] {
            left = mid + 1
        } else {
            right = mid
        }
    }
    return nums[left]
}

func main() {
    arr := []int{1,2,3,4,5,6,7}
    fmt.Println(findMin(arr))
}
A4
B7
C1
D0
Attempts:
2 left
💡 Hint
If the array is sorted and not rotated, the first element is the smallest.
🔧 Debug
advanced
2:00remaining
Identify the error in minimum finder for rotated array
What error does the following Go code produce when trying to find the minimum in a rotated sorted array?
DSA Go
package main
import "fmt"

func findMin(nums []int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right-left)/2
        if nums[mid] > nums[right] {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return nums[left]
}

func main() {
    arr := []int{4,5,6,7,0,1,2}
    fmt.Println(findMin(arr))
}
Ainfinite loop
B0
C7
Druntime error: index out of range
Attempts:
2 left
💡 Hint
Check how the pointers move and if they can go out of bounds.
🧠 Conceptual
advanced
2:00remaining
Why binary search works for finding minimum in rotated sorted array
Why does binary search work to find the minimum element in a rotated sorted array?
ABecause the minimum element is always at the middle index
BBecause the array is partially sorted and the minimum is the only point where order breaks
CBecause the array is fully sorted and binary search always works on sorted arrays
DBecause the array contains duplicates that help locate the minimum
Attempts:
2 left
💡 Hint
Think about the property of rotated sorted arrays and how order changes.
Predict Output
expert
2:00remaining
Find minimum in rotated sorted array with duplicates
What is the output of the following Go code that finds the minimum element in a rotated sorted array which may contain duplicates?
DSA Go
package main
import "fmt"

func findMin(nums []int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := left + (right-left)/2
        if nums[mid] > nums[right] {
            left = mid + 1
        } else if nums[mid] < nums[right] {
            right = mid
        } else {
            right--
        }
    }
    return nums[left]
}

func main() {
    arr := []int{2,2,2,0,1,2}
    fmt.Println(findMin(arr))
}
A0
B1
C2
Druntime error
Attempts:
2 left
💡 Hint
Duplicates require careful handling to avoid skipping the minimum.