0
0
DSA Typescriptprogramming~10 mins

Binary Search vs Linear Search Real Cost Difference in DSA Typescript - 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 Typescript
function linearSearch(arr: number[], target: number): number {
  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'
Atarget
Bi
Carr
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] with i instead of target
Returning the element instead of the index
Using assignment '=' instead of comparison '==='
2fill in blank
medium

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

DSA Typescript
function binarySearch(arr: number[], target: number): number {
  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] [1] target) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
A===
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong search direction
Using '!=' or '===' in the else if condition
Not updating left or right correctly
3fill in blank
hard

Fix the error in the binary search code to correctly calculate the middle index without overflow.

DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    const mid = [1];
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] > target) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
AMath.floor((left + right) / 2)
BMath.ceil((left + right) / 2)
C(left + right) >> 1
Dleft + Math.floor((right - left) / 2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using (left + right) / 2 directly causing overflow
Using Math.ceil which can cause infinite loops
Using bitwise shift which may be unclear
4fill in blank
hard

Complete the code to create a function that compares the number of steps taken by linear and binary search for a target in a sorted array.

DSA Typescript
function compareSearchSteps(arr: number[], target: number): { linearSteps: number; binarySteps: number } {
  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++;
    const mid = left + Math.floor((right - left) / 2);
    if (arr[mid] === target) {
      break;
    } else if (arr[mid] [1] target) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return { linearSteps, binarySteps,  };
}
Drag options to blanks, or click blank then click option'
A>
B<
C,
D;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in binary search comparison
Using semicolon instead of comma in object return
Not incrementing step counters
5fill in blank
hard

Fill all three blanks to create a function that measures and prints the time taken by linear and binary search to find a target in a sorted array.

DSA Typescript
function measureSearchTime(arr: number[], target: number): void {
  const startLinear = performance.now();
  linearSearch(arr, target);
  const endLinear = performance.now();
  const startBinary = performance.now();
  binarySearch(arr, target);
  const endBinary = performance.now();
  console.log('Linear Search Time: ' + (endLinear [1] startLinear) + ' ms');
  console.log('Binary Search Time: ' + (endBinary [2] startBinary) + ' ms');
  console.log('Binary search is approximately ' + ((endLinear [3] startLinear) / (endBinary [2] startBinary)).toFixed(2) + ' times faster');
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causing incorrect time calculation
Using multiplication or division instead of subtraction
Mixing operators inconsistently