0
0
DSA Javascriptprogramming~20 mins

Why Binary Search and What Sorted Order Gives You in DSA Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search on Sorted Array
What is the output of the following JavaScript code that performs binary search on a sorted array to find the index of number 7?
DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const sortedArray = [1, 3, 5, 7, 9, 11];
console.log(binarySearch(sortedArray, 7));
A-1
B3
C2
D4
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
🧠 Conceptual
intermediate
1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before performing binary search?
ABecause binary search requires the array to be reversed first.
BBecause binary search only works on arrays with unique elements.
CBecause binary search compares the target with the middle element to decide which half to search next, which only works if the array is sorted.
DBecause binary search can only find the first element in any array.
Attempts:
2 left
💡 Hint
Think about how binary search decides which half of the array to ignore.
Predict Output
advanced
2:00remaining
Binary Search Result on Unsorted Array
What is the output of this JavaScript code that runs binary search on an unsorted array to find number 7?
DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const unsortedArray = [9, 1, 11, 3, 7, 5];
console.log(binarySearch(unsortedArray, 7));
A-1
B3
C2
D0
Attempts:
2 left
💡 Hint
Binary search assumes sorted order; what happens if the array is not sorted?
🧠 Conceptual
advanced
1:30remaining
Effect of Sorted Order on Search Efficiency
How does having a sorted array improve the efficiency of searching compared to an unsorted array?
ASorted arrays allow binary search, which reduces the search steps from linear to logarithmic time.
BSorted arrays allow linear search to run faster than on unsorted arrays.
CSorted arrays require more steps to search because elements are arranged in order.
DSorted arrays do not affect search efficiency; search time is always the same.
Attempts:
2 left
💡 Hint
Think about how many elements you can ignore each step in binary search.
🚀 Application
expert
2:30remaining
Final Position After Multiple Binary Search Steps
Given the sorted array [2, 4, 6, 8, 10, 12, 14] and target 10, after two iterations of the binary search loop, what is the value of the variable 'left'?
DSA Javascript
const arr = [2, 4, 6, 8, 10, 12, 14];
const target = 10;
let left = 0;
let right = arr.length - 1;

// First iteration
let mid = Math.floor((left + right) / 2);
if (arr[mid] < target) {
  left = mid + 1;
} else {
  right = mid - 1;
}

// Second iteration
mid = Math.floor((left + right) / 2);
if (arr[mid] < target) {
  left = mid + 1;
} else {
  right = mid - 1;
}

console.log(left);
A3
B5
C2
D4
Attempts:
2 left
💡 Hint
Trace the values of left, right, and mid after each iteration carefully.