0
0
DSA Javascriptprogramming~20 mins

Search in Rotated Sorted Array in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rotated Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of search in rotated sorted array
What is the output of the following JavaScript code that searches for a target in a rotated sorted array?
DSA Javascript
function search(nums, target) {
  let left = 0, right = nums.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] === target) return mid;
    if (nums[left] <= nums[mid]) {
      if (nums[left] <= target && target < nums[mid]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    } else {
      if (nums[mid] < target && target <= nums[right]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }
  return -1;
}

console.log(search([4,5,6,7,0,1,2], 0));
A0
B6
C-1
D4
Attempts:
2 left
💡 Hint
Think about how the rotated sorted array is split and how binary search adapts.
Predict Output
intermediate
2:00remaining
Output when target is not in rotated sorted array
What is the output of this code when the target is not present in the rotated sorted array?
DSA Javascript
function search(nums, target) {
  let left = 0, right = nums.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] === target) return mid;
    if (nums[left] <= nums[mid]) {
      if (nums[left] <= target && target < nums[mid]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    } else {
      if (nums[mid] < target && target <= nums[right]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }
  return -1;
}

console.log(search([4,5,6,7,0,1,2], 3));
A-1
B2
C0
D6
Attempts:
2 left
💡 Hint
If the target is not found, what does the function return?
🧠 Conceptual
advanced
2:00remaining
Why binary search works on rotated sorted arrays
Why can binary search be adapted to work on a rotated sorted array?
ABecause the array elements are all unique, so we can search linearly.
BBecause the array is fully sorted, so normal binary search applies without changes.
CBecause at least one half of the array is always sorted, allowing us to decide which half to search next.
DBecause the rotation does not affect the order of elements, so we can ignore it.
Attempts:
2 left
💡 Hint
Think about how the array is split by the rotation point.
🔧 Debug
advanced
2:00remaining
Identify the error in this rotated array search code
What error will this code produce when searching in a rotated sorted array?
DSA Javascript
function search(nums, target) {
  let left = 0, right = nums.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] === target) return mid;
    if (nums[left] <= nums[mid]) {
      if (nums[left] <= target && target < nums[mid]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    } else {
      if (nums[mid] < target && target <= nums[right]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }
  return -1;
}

console.log(search([4,5,6,7,0,1,2], 0));
AReturns -1 even if target is present
BSyntaxError due to missing parentheses
CInfinite loop because left and right pointers never meet
DReturns correct index 4
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search range.
🚀 Application
expert
3:00remaining
Find minimum element in rotated sorted array
Given a rotated sorted array with no duplicates, which code correctly finds the minimum element?
A
function findMin(nums) {
  let min = nums[0];
  for (let i = 1; i &lt; nums.length; i++) {
    if (nums[i] &lt; min) min = nums[i];
  }
  return min;
}
B
function findMin(nums) {
  let left = 0, right = nums.length - 1;
  while (left &lt; right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] &gt; nums[right]) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return nums[left];
}
C
function findMin(nums) {
  return Math.min(...nums);
}
D
function findMin(nums) {
  let left = 0, right = nums.length - 1;
  while (left &lt;= right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] &lt; nums[left]) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return nums[left];
}
Attempts:
2 left
💡 Hint
Use binary search logic to find the point where order breaks.