Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main goal when finding the minimum in a rotated sorted array?
easy
A. To find the largest element in the array
B. To count the number of rotations
C. To find the smallest element after the rotation point
D. To sort the array again

Solution

  1. Step 1: Understand the rotated sorted array

    A rotated sorted array is a sorted array shifted at some pivot. The smallest element is the rotation point.
  2. Step 2: Identify the goal

    The goal is to find the smallest element, which is the minimum after rotation.
  3. Final Answer:

    To find the smallest element after the rotation point -> Option C
  4. Quick Check:

    Minimum element = smallest after rotation [OK]
Hint: Look for the smallest element where order breaks [OK]
Common Mistakes:
  • Confusing minimum with maximum
  • Trying to count rotations instead of finding minimum
  • Assuming array is fully sorted without rotation
2. Which of the following is the correct syntax to start a binary search loop in JavaScript for finding minimum in rotated sorted array nums?
easy
A. while(left < right) { ... }
B. for(let left = 0, right = nums.length - 1; left <= right; )
C. for(let i = 0; i < nums.length; i++)
D. if(nums.length === 0) return -1

Solution

  1. Step 1: Identify the loop type for binary search

    Binary search typically uses a while loop with condition left < right to narrow down the search space.
  2. Step 2: Check options

    while(left < right) { ... } uses while(left < right), which is correct for binary search in this problem.
  3. Final Answer:

    while(left < right) { ... } -> Option A
  4. Quick Check:

    Binary search loop uses while(left < right) [OK]
Hint: Use while loop with left < right for binary search [OK]
Common Mistakes:
  • Using for loop incorrectly for binary search
  • Using <= instead of < causing infinite loops
  • Not initializing left and right properly
3. What will be the output of the following code?
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]));
medium
A. 1
B. 4
C. 7
D. 0

Solution

  1. Step 1: Understand the binary search logic

    The code compares middle element with right element to decide which half contains the minimum.
  2. Step 2: Trace the code with input [4,5,6,7,0,1,2]

    Initially left=0, right=6. Mid=3 (value 7). Since 7 > 2, left = mid+1 = 4. Now left=4, right=6. Mid=5 (value 1). 1 <= 2, so right=mid=5. Next left=4, right=5. Mid=4 (value 0). 0 <= 1, right=mid=4. Now left=4, right=4, loop ends. Return nums[4] = 0.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    Minimum element found = 0 [OK]
Hint: Compare mid with right to decide search half [OK]
Common Mistakes:
  • Returning nums[mid] instead of nums[left]
  • Confusing comparison direction
  • Not updating pointers correctly
4. Identify the bug in this code snippet for finding minimum in rotated sorted array:
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];
}
medium
A. The loop condition should be left < right, not left <= right
B. The mid calculation is incorrect
C. The return statement should be nums[right]
D. The comparison should be nums[mid] < nums[right]

Solution

  1. Step 1: Analyze loop condition

    The loop uses left <= right, which can cause infinite loop or skip the correct minimum index.
  2. Step 2: Correct loop condition

    Binary search for minimum in rotated array requires left < right to narrow search correctly without missing elements.
  3. Final Answer:

    The loop condition should be left < right, not left <= right -> Option A
  4. Quick Check:

    Use while(left < right) for correct binary search [OK]
Hint: Use strict less than in binary search loops [OK]
Common Mistakes:
  • Using <= causing infinite loops
  • Changing mid calculation unnecessarily
  • Returning wrong index after loop
5. Given a rotated sorted array nums that may contain duplicates, which modification is needed in the binary search approach to correctly find the minimum?
hard
A. Return nums[mid] immediately when nums[mid] == nums[right]
B. When nums[mid] == nums[right], reduce right by 1 to skip duplicate
C. Always move left to mid + 1 when nums[mid] >= nums[right]
D. Sort the array first before applying binary search

Solution

  1. Step 1: Understand duplicates effect

    Duplicates can cause ambiguity when nums[mid] equals nums[right], making it unclear which side to search.
  2. Step 2: Modify binary search to handle duplicates

    When nums[mid] == nums[right], safely reduce right by 1 to skip duplicate and continue search.
  3. Final Answer:

    When nums[mid] == nums[right], reduce right by 1 to skip duplicate -> Option B
  4. Quick Check:

    Handle duplicates by shrinking right pointer [OK]
Hint: Skip duplicates by decrementing right when equal [OK]
Common Mistakes:
  • Ignoring duplicates causing wrong minimum
  • Moving left pointer incorrectly on equality
  • Sorting array unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard