Practice Linear Search in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

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?

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?

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.

Example 7

medium
Trace linear search of [3,8,8,1] for 8, first-match. Output the index and 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?

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?

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?

Example 20

hard
You sort first (O(nlog⁡n)) then binary-search k times (O(klog⁡n)). For what k does this beat k linear searches (O(kn))?