Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the minimum element output
What is the output of this JavaScript code that finds the minimum in a rotated sorted array?
DSA Javascript
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
console.log(findMin([4,5,6,7,0,1,2]));Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element in the rotated array.
✗ Incorrect
The code uses binary search to find the smallest element. Since 0 is the smallest in the rotated array [4,5,6,7,0,1,2], it returns 0.
❓ Predict Output
intermediate2:00remaining
Minimum element in a non-rotated sorted array
What will the function output for a sorted array that is not rotated?
DSA Javascript
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
console.log(findMin([1,2,3,4,5,6,7]));Attempts:
2 left
💡 Hint
If the array is not rotated, the smallest element is the first element.
✗ Incorrect
Since the array is sorted and not rotated, the smallest element is the first element, which is 1.
🔧 Debug
advanced2:00remaining
Identify the error in this minimum finder
What error will this code produce when trying to find the minimum in a rotated sorted array?
DSA Javascript
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums[left];
}
console.log(findMin([3,1,2]));Attempts:
2 left
💡 Hint
Check the loop condition and how right pointer is updated.
✗ Incorrect
The loop condition and right pointer update cause the function to miss the correct minimum and return 3 instead of 1.
❓ Predict Output
advanced2:00remaining
Output of minimum finder with duplicates
What is the output of this code when the rotated sorted array contains duplicates?
DSA Javascript
function findMin(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else if (nums[mid] < nums[right]) {
right = mid;
} else {
right--;
}
}
return nums[left];
}
console.log(findMin([2,2,2,0,1,2]));Attempts:
2 left
💡 Hint
Duplicates require careful handling to avoid missing the minimum.
✗ Incorrect
The code handles duplicates by decreasing right pointer when equal, eventually finding the minimum 0.
🧠 Conceptual
expert2:00remaining
Why binary search works for rotated sorted arrays
Why does binary search still work to find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how the rotated array keeps some sorted order properties.
✗ Incorrect
Binary search works because the rotated array is partially sorted, allowing comparison between middle and right elements to decide which half contains the minimum.