What is the main difference between linear search and binary search in terms of data requirements?
Linear search works on any list, sorted or unsorted. Binary search requires the list to be sorted before searching.
Click to reveal answer
beginner
How does the time complexity of linear search compare to binary search?
Linear search has a time complexity of O(n), meaning it may check every item. Binary search has a time complexity of O(log n), meaning it divides the search space in half each step.
Click to reveal answer
intermediate
Why might binary search not always be faster in real life despite its better time complexity?
Binary search requires the list to be sorted, which can take extra time if the list is unsorted. Also, for very small lists, the overhead of binary search steps might be slower than a simple linear search.
Click to reveal answer
intermediate
What is the real cost difference when searching in a very small list (e.g., 5 elements)?
For very small lists, linear search is often faster because it checks elements directly without extra calculations. Binary search's divide-and-conquer steps add overhead that may not pay off for small sizes.
Click to reveal answer
advanced
What is the impact of sorting on the total cost when using binary search on an unsorted list?
Sorting an unsorted list before binary search adds extra cost, usually O(n log n), which can outweigh the benefits of binary search if only one search is done.
Click to reveal answer
Which search algorithm can be used on an unsorted list without any preparation?
ABinary Search
BLinear Search
CBoth require sorted lists
DNeither can search unsorted lists
✗ Incorrect
Linear search checks each element one by one and does not require the list to be sorted.
What is the time complexity of binary search on a sorted list?
AO(n)
BO(n log n)
CO(log n)
DO(1)
✗ Incorrect
Binary search divides the search space in half each step, resulting in O(log n) time complexity.
Why might linear search be faster than binary search on very small lists?
ABecause binary search has overhead from dividing steps
BBecause binary search requires sorting
CBecause linear search is always faster
DBecause linear search uses less memory
✗ Incorrect
Binary search involves calculations to find midpoints, which adds overhead that may not be worth it for small lists.
What extra step is needed before using binary search on an unsorted list?
ASort the list
BReverse the list
CRemove duplicates
DNo extra step needed
✗ Incorrect
Binary search requires the list to be sorted to work correctly.
If you only need to search once in an unsorted list, which approach is usually better?
AUse a hash table
BSort then binary search
CBinary search without sorting
DLinear search
✗ Incorrect
Sorting takes extra time, so for a single search, linear search is often faster.
Explain the real cost difference between linear search and binary search when searching in small and large lists.
Think about preparation time and search steps.
You got /5 concepts.
Describe why binary search might not always be the best choice despite its better theoretical time complexity.
Consider practical factors beyond big O notation.
You got /4 concepts.
Practice
(1/5)
1. Which search algorithm is generally faster on a sorted list of 1,000,000 elements?
easy
A. Linear Search
B. Both are equally fast
C. Binary Search
D. Neither works on sorted lists
Solution
Step 1: Understand the search algorithms
Binary Search divides the list in half repeatedly, while Linear Search checks each element one by one.
Step 2: Compare efficiency on large sorted lists
Binary Search takes about log2(1,000,000) ≈ 20 steps, Linear Search takes up to 1,000,000 steps.
Final Answer:
Binary Search -> Option C
Quick Check:
Binary Search is faster on sorted lists [OK]
Hint: Binary Search halves search space; Linear checks all [OK]
Common Mistakes:
Thinking Linear Search is faster on sorted lists
Confusing sorted and unsorted list requirements
Ignoring the difference in step counts
2. Which of the following is the correct TypeScript syntax to perform a linear search on an array arr for a value target?
easy
A. arr.findIndex(target);
B. for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; }
C. while (arr.includes(target)) { return arr.indexOf(target); }
D. for (let i in arr) { if (arr[i] == target) return i; }
Solution
Step 1: Check syntax for linear search loop
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } uses a for loop with index and compares each element to target correctly.
Step 2: Analyze other options for correctness
while (arr.includes(target)) { return arr.indexOf(target); } misuses while with includes; arr.findIndex(target); uses findIndex incorrectly; for (let i in arr) { if (arr[i] == target) return i; } uses 'in' which iterates keys but with loose equality.
Final Answer:
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } -> Option B
Quick Check:
Correct for loop syntax for linear search [OK]
Hint: Use for loop with strict equality for linear search [OK]
Common Mistakes:
Using while loop incorrectly
Using loose equality (==) instead of strict (===)
Misusing array methods for search
3. What will be the output of the following TypeScript code snippet?
const arr = [1, 3, 5, 7, 9];
function binarySearch(array: number[], target: number): number {
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));
Hint: Calculate mid index and compare stepwise [OK]
Common Mistakes:
Confusing mid calculation
Returning wrong index
Not updating left/right correctly
4. Identify the bug in this linear search function and select the fix:
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i <= arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
medium
A. Change loop condition to i < arr.length
B. Change === to == in if condition
C. Add a break after return statement
D. Initialize i to 1 instead of 0
Solution
Step 1: Analyze loop condition
The loop runs while i <= arr.length, which causes out-of-bounds access at i = arr.length.
Step 2: Fix loop condition to prevent error
Changing to i < arr.length ensures valid indices only.
Final Answer:
Change loop condition to i < arr.length -> Option A
Quick Check:
Loop must stay within array bounds [OK]
Hint: Use i < arr.length to avoid out-of-bounds [OK]
Common Mistakes:
Using <= instead of < in loop
Changing equality operator unnecessarily
Adding break after return (unreachable code)
5. You have a sorted array of 10,000 elements and want to find a target value. Which approach minimizes the average number of comparisons, and why?
Options: A) Use Linear Search because it checks elements sequentially. B) Use Linear Search but start from the middle. C) Use Binary Search but only check every other element. D) Use Binary Search because it divides the search space each step.
hard
A. Use Linear Search because it checks elements sequentially.
B. Use Linear Search but start from the middle.
C. Use Binary Search but only check every other element.
D. Use Binary Search because it divides the search space each step.
Solution
Step 1: Understand average comparisons for Linear vs Binary Search
Linear Search averages about n/2 = 5,000 comparisons; Binary Search averages about log2(10,000) ≈ 14 comparisons.
Step 2: Evaluate options for efficiency
Binary Search halves search space each step, minimizing comparisons. Starting Linear Search from middle or skipping elements breaks correctness or efficiency.
Final Answer:
Use Binary Search because it divides the search space each step. -> Option D
Quick Check:
Binary Search minimizes comparisons on sorted arrays [OK]