A search algorithm that checks each element in a list one by one, starting from the first, until the target is found or the entire list has been examined. It is the simplest search algorithm and works on both sorted and unsorted data.
Looking for your keys by checking every pocket and drawer in order.
Showing a random 20 of 50 problems.
Example 1
easy
On unsorted data, can you use binary search directly?
Example 2
medium
Linear search for 6 in [6,2,9,4]. Which index and how many comparisons?Target 6 is at index 0: best case โ found in 1 comparison
Example 3
medium
A web-scraper reads pages from a never-ending RSS feed and stops at the first article matching a keyword. Why is linear search the natural fit?
Example 4
challenge
You must check whether ANY of 5 targets appears in a list of n items. Best total cost using linear search?
Example 5
medium
Linear search on [2,4,6,8,10] for 7. How many comparisons before returning not-found?Target 7 not in list: all 5 elements compared, not-found returned after 5 comparisons
Example 6
hard
Design a single linear pass over [3,1,4,1,5,9,2,6] that returns the index of the FIRST element strictly greater than 5. Give the result.Predicate search for first element > 5: indices 0-4 all fail, 9 at index 5 is the first match
Example 7
medium
Trace linear search of [3,8,8,1] for 8, first-match. Output the index and comparisons.First-match search for 8: 3 checked first, then 8 found at index 1 after 2 comparisons
Example 8
easy
What is the time complexity of linear search?
Example 9
easy
Why might linear search outperform binary search on a list of size 4?
Example 10
medium
If you must perform k=100 searches on the SAME unsorted list of n=10,000 items, total work is approximately?
Example 11
easy
Linear search on [5,5,5,5] for 5 (first match). How many comparisons?All elements are 5; first-match search hits index 0 immediately โ 1 comparison
Example 12
hard
On a list of n unique elements where the target is GUARANTEED present, what is the worst-case number of comparisons for linear search?
Example 13
easy
Linear search on [10,20,30] for 50. What is the result?Target 50 not in list: every element checked, returns not-found (โ1)
Example 14
easy
Worst case, how many elements does linear search check in a list of 50?
Example 15
medium
Linear search returns the index of the first match but your code uses the result without checking for -1. What bug class is this?
Example 16
medium
Linear search a list of n=220. Approximate worst-case comparisons vs binary search on the SAME size sorted list.
Example 17
medium
Linear search returns the index of the FIRST match. Modify the algorithm so it returns the index of the LAST match. What is the smallest change?
Example 18
easy
In the best case, how many comparisons does linear search make?
Example 19
easy
Linear search on [4,8,15,16] for 15 finds it at which 0-indexed position?Linear search for 15: scanned 4 and 8 before finding 15 at index 2
Example 20
hard
You sort first (O(nlogn)) then binary-search k times (O(klogn)). For what k does this beat k linear searches (O(kn))?