0
0
DSA Javascriptprogramming~10 mins

Find Minimum in Rotated Sorted Array in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the binary search with the correct left pointer.

DSA Javascript
function findMin(nums) {
  let left = [1];
  let 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];
}
Drag options to blanks, or click blank then click option'
A0
B1
Cnums.length
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or -1 causes missing the first element.
Starting left at nums.length is out of bounds.
2fill in blank
medium

Complete the code to calculate the middle index correctly.

DSA Javascript
function findMin(nums) {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    let mid = [1];
    if (nums[mid] > nums[right]) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return nums[left];
}
Drag options to blanks, or click blank then click option'
A(left + right) / 2
BMath.floor((left + right) / 2)
CMath.ceil((left + right) / 2)
Dleft + right
Attempts:
3 left
💡 Hint
Common Mistakes
Using (left + right) / 2 without floor gives a float index.
Using Math.ceil can cause infinite loops in some cases.
3fill in blank
hard

Fix the error in the condition to decide which side to search next.

DSA Javascript
function findMin(nums) {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] [1] nums[right]) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return nums[left];
}
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or <= reverses the logic and causes wrong results.
Using >= can cause infinite loops.
4fill in blank
hard

Fill both blanks to correctly update pointers based on comparison.

DSA Javascript
function findMin(nums) {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] [1] nums[right]) {
      left = [2];
    } else {
      right = mid;
    }
  }
  return nums[left];
}
Drag options to blanks, or click blank then click option'
A>
B<
Cmid + 1
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > reverses the search direction.
Updating left to mid - 1 can skip the minimum element.
5fill in blank
hard

Fill all three blanks to create a dictionary of indices to values for elements less than or equal to 3.

DSA Javascript
function filterValues(nums) {
  return nums.reduce((acc, num, index) => {
    if (num [1] 3) {
      acc[[2]] = [3];
    }
    return acc;
  }, {});
}
Drag options to blanks, or click blank then click option'
A<=
Bindex
Cnum
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= causes wrong filtering.
Using num as key or index as value swaps keys and values.