0
0
DSA Javascriptprogramming~10 mins

Binary Search vs Linear Search Real Cost Difference in DSA Javascript - Interactive Comparison Practice

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

Complete the code to perform a linear search that returns the index of the target or -1 if not found.

DSA Javascript
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === [1]) {
      return i;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
Aindex
Bi
Carr
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop index 'i' instead of the target for comparison.
Comparing arr[i] with arr instead of target.
2fill in blank
medium

Complete the code to perform a binary search on a sorted array to find the target index or return -1 if not found.

DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === [1]) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
Amid
Bright
Ctarget
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using left or right instead of target for comparison.
3fill in blank
hard

Fix the error in the binary search code to correctly update the search boundaries.

DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = [1];
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bmid - 1
Cleft + 1
Dright - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting left to mid - 1 which moves left boundary incorrectly.
Using right - 1 or left + 1 which are not correct updates here.
4fill in blank
hard

Fill both blanks to create a function that compares the number of steps taken by linear and binary search to find a target.

DSA Javascript
function compareSearches(arr, target) {
  let linearSteps = 0;
  for (let i = 0; i < arr.length; i++) {
    linearSteps++;
    if (arr[i] === target) {
      break;
    }
  }

  let left = 0;
  let right = arr.length - 1;
  let binarySteps = 0;
  while (left <= right) {
    binarySteps++;
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      break;
    } else if (arr[mid] < target) {
      left = [1];
    } else {
      right = [2];
    }
  }

  return { linearSteps, binarySteps };
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bmid - 1
Cleft + 1
Dright - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the updates of left and right boundaries.
Using left + 1 or right - 1 incorrectly.
5fill in blank
hard

Fill all three blanks to create a function that returns the search method with fewer steps for a given target.

DSA Javascript
function bestSearchMethod(arr, target) {
  const { linearSteps, binarySteps } = compareSearches(arr, target);
  if (linearSteps [1] binarySteps) {
    return [2];
  } else {
    return [3];
  }
}
Drag options to blanks, or click blank then click option'
A<
B>
C"Linear Search"
D"Binary Search"
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in the if condition.
Returning the wrong search method string.