0
0
DSA Javascriptprogramming~20 mins

Find Minimum in Rotated Sorted Array in DSA Javascript - 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 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]));
A4
B7
C1
D0
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element in the rotated array.
Predict Output
intermediate
2: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]));
A7
B1
C4
D3
Attempts:
2 left
💡 Hint
If the array is not rotated, the smallest element is the first element.
🔧 Debug
advanced
2: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]));
AReturns wrong minimum value 3
BIndexError
CTypeError
DReturns correct minimum value 1
Attempts:
2 left
💡 Hint
Check the loop condition and how right pointer is updated.
Predict Output
advanced
2: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]));
A0
B1
C2
DError
Attempts:
2 left
💡 Hint
Duplicates require careful handling to avoid missing the minimum.
🧠 Conceptual
expert
2: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?
ABecause linear search is too slow and binary search guesses the minimum randomly
BBecause the array is fully sorted and binary search always works on sorted arrays
CBecause the array is partially sorted and we can compare middle and right elements to decide search direction
DBecause the minimum element is always at the middle index after rotation
Attempts:
2 left
💡 Hint
Think about how the rotated array keeps some sorted order properties.