0
0
DSA Javascriptprogramming~20 mins

Binary Search vs Linear Search Real Cost Difference in DSA Javascript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Linear Search on an Unsorted Array
What is the output of the following JavaScript code that performs a linear search for the number 7 in an unsorted array?
DSA Javascript
const arr = [3, 1, 4, 7, 5];
function linearSearch(array, target) {
  for (let i = 0; i < array.length; i++) {
    if (array[i] === target) {
      return i;
    }
  }
  return -1;
}
console.log(linearSearch(arr, 7));
A4
B-1
C3
D0
Attempts:
2 left
💡 Hint
Remember linear search checks each element until it finds the target.
Predict Output
intermediate
2:00remaining
Output of Binary Search on a Sorted Array
What is the output of the following JavaScript code that performs a binary search for the number 7 in a sorted array?
DSA Javascript
const arr = [1, 3, 4, 5, 7, 9];
function binarySearch(array, target) {
  let left = 0;
  let right = array.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (array[mid] === target) {
      return mid;
    } else if (array[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
console.log(binarySearch(arr, 7));
A4
B-1
C3
D5
Attempts:
2 left
💡 Hint
Binary search finds the middle element and narrows down the search.
🧠 Conceptual
advanced
2:00remaining
Why Binary Search Requires a Sorted Array
Why does binary search require the array to be sorted to work correctly?
ABecause it compares the target with the middle element to decide which half to search next, which only works if the array is sorted.
BBecause binary search checks every element one by one, so sorting helps speed it up.
CBecause binary search uses hashing which requires sorted data.
DBecause binary search only works on arrays with unique elements.
Attempts:
2 left
💡 Hint
Think about how binary search decides where to look next.
Predict Output
advanced
2:00remaining
Output of Binary Search on an Unsorted Array
What is the output of the following JavaScript code that performs a binary search for the number 7 in an unsorted array?
DSA Javascript
const arr = [7, 3, 1, 4, 5];
function binarySearch(array, target) {
  let left = 0;
  let right = array.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (array[mid] === target) {
      return mid;
    } else if (array[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
console.log(binarySearch(arr, 7));
A2
B-1
C0
D3
Attempts:
2 left
💡 Hint
Binary search assumes the array is sorted, but here it is not.
🧠 Conceptual
expert
3:00remaining
Real Cost Difference Between Linear and Binary Search
Which statement best describes the real cost difference between linear search and binary search when searching for an element in a large sorted array?
ABinary search is always faster because it reduces the search space by half each step, while linear search checks elements one by one.
BLinear search is faster for large arrays because it does not require sorting, unlike binary search.
CLinear search and binary search have the same cost if the target is near the start of the array.
DBinary search has a higher setup cost due to sorting, but once sorted, it is much faster than linear search for repeated searches.
Attempts:
2 left
💡 Hint
Consider the cost of sorting and repeated searches.