0
0
DSA Typescriptprogramming~20 mins

Binary Search vs Linear Search Real Cost Difference in DSA Typescript - 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 TypeScript code that performs a linear search for the number 7 in an unsorted array?
DSA Typescript
function linearSearch(arr: number[], target: number): number {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }
  return -1;
}

const array = [3, 1, 4, 7, 5];
console.log(linearSearch(array, 7));
A-1
B1
C3
D4
Attempts:
2 left
💡 Hint
Remember linear search checks elements one by one from the start.
Predict Output
intermediate
2:00remaining
Output of Binary Search on a Sorted Array
What is the output of the following TypeScript code that performs a binary search for the number 7 in a sorted array?
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] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const array = [1, 3, 4, 5, 7, 9];
console.log(binarySearch(array, 7));
A4
B-1
C3
D5
Attempts:
2 left
💡 Hint
Binary search divides the array and finds the target index.
🧠 Conceptual
advanced
2:00remaining
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 faster because it reduces the search space by half each step, while linear search checks every element one by one.
BLinear search is faster because it does not require the array to be sorted, unlike binary search.
CBoth searches have the same cost because they both look at elements until they find the target.
DBinary search is slower because it requires more comparisons than linear search.
Attempts:
2 left
💡 Hint
Think about how many elements each search checks in the worst case.
Predict Output
advanced
2:00remaining
Output of Binary Search on an Unsorted Array
What is the output of the following TypeScript code that performs a binary search for the number 7 on an unsorted array?
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] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const array = [8, 9, 10, 1, 7];
console.log(binarySearch(array, 7));
A0
B2
C4
D-1
Attempts:
2 left
💡 Hint
Binary search requires a sorted array to work correctly.
🧠 Conceptual
expert
2:00remaining
When Linear Search Can Be More Efficient Than Binary Search
In which scenario can linear search be more efficient than binary search despite binary search's better average time complexity?
AWhen the array is very large and sorted.
BWhen the array is very small or the target is near the beginning of the array.
CWhen the array is sorted and the target is near the end.
DWhen the array is sorted and contains only unique elements.
Attempts:
2 left
💡 Hint
Think about the overhead of binary search and the position of the target.