Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a linear search algorithm?
Linear search is a method of finding a target value by checking each element in a list one by one from start to end until the target is found or the list ends.
Click to reveal answer
beginner
What condition must be true for binary search to work?
The list must be sorted in order (either ascending or descending) before performing a binary search.
Click to reveal answer
intermediate
How does binary search reduce the search area?
Binary search compares the target with the middle element of the list. If the target is smaller, it searches the left half; if larger, it searches the right half. This halves the search area each time.
Click to reveal answer
beginner
Which search algorithm is faster for large sorted lists: linear or binary search?
Binary search is faster for large sorted lists because it reduces the search area by half each step, while linear search checks elements one by one.
Click to reveal answer
intermediate
Explain the worst-case time complexity of linear and binary search.
Linear search worst-case time is O(n), meaning it may check every element. Binary search worst-case time is O(log n), meaning it divides the search area repeatedly, making it much faster on large lists.
Click to reveal answer
Which search algorithm requires the list to be sorted?
ANeither linear nor binary search
BLinear search
CBinary search
DBoth linear and binary search
✗ Incorrect
Binary search only works correctly if the list is sorted. Linear search works on any list.
In linear search, what happens if the target is not in the list?
AThe algorithm stops immediately
BThe algorithm stops after checking half the elements
CThe algorithm sorts the list first
DThe algorithm stops after checking all elements
✗ Incorrect
Linear search checks each element until the end if the target is not found.
What is the main advantage of binary search over linear search?
AIt works on unsorted lists
BIt is faster on large sorted lists
CIt uses less memory
DIt is easier to implement
✗ Incorrect
Binary search is faster on large sorted lists because it halves the search area each step.
If a list has 16 elements, how many steps does binary search take in the worst case?
A4 steps
B8 steps
C16 steps
D1 step
✗ Incorrect
Binary search takes log2(16) = 4 steps in the worst case.
Which search algorithm checks elements one by one?
ALinear search
BBinary search
CBoth
DNone
✗ Incorrect
Linear search checks each element one by one until it finds the target or reaches the end.
Describe how linear search works using a real-life example.
Think about looking for a book on a messy shelf.
You got /3 concepts.
Explain the steps of binary search and why the list must be sorted.
Imagine searching for a word in a dictionary.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is true about linear search?
easy
A. It checks each item one by one until it finds the target.
B. It requires the list to be sorted before searching.
C. It splits the list into halves to find the target quickly.
D. It only works on numbers, not text.
Solution
Step 1: Understand linear search method
Linear search goes through each item in the list one by one to find the target.
Step 2: Compare with other search methods
Binary search splits the list and requires sorting, but linear search does not.
Final Answer:
It checks each item one by one until it finds the target. -> Option A
Quick Check:
Linear search = check items one by one [OK]
Hint: Linear search checks items one by one [OK]
Common Mistakes:
Thinking linear search needs sorted list
Confusing linear search with binary search
Believing linear search only works on numbers
2. Which of the following is the correct syntax for a linear search loop in Python to find target in arr?
easy
A. for i in arr:
if i == target
return True
B. for i in range(len(arr)):
if arr[i] = target:
return True
C. while i < len(arr):
if arr[i] == target
return True
D. for i in arr:
if i == target:
return True
Solution
Step 1: Check correct loop syntax
for i in arr:
if i == target:
return True uses a for loop to iterate over each element in arr correctly.
Step 2: Verify condition and syntax
for i in arr:
if i == target:
return True uses '==' for comparison and proper indentation, which is correct.
Final Answer:
for i in arr:
if i == target:
return True -> Option D
Quick Check:
Correct for loop and comparison syntax [OK]
Hint: Use '==' for comparison and proper indentation [OK]
Common Mistakes:
Using single '=' instead of '==' for comparison
Missing colon ':' after if statement
Incorrect indentation causing syntax errors
3. What will be the output of the following Python code?