Questions: Linear Search

4 questions to test your understanding

Score: 0 / 4
Question 1 Multiple Choice

What is wrong with this Python function? def find(items, target): for i in range(len(items)): if items[i] == target: return i else: return -1

ANothing; it is a correct linear search
BIt checks only the first element, because both branches return during the first pass of the loop
CIt skips the first element
DIt returns the last match instead of the first
Question 2 Multiple Choice

A linear search runs on an unsorted array of 1,000 distinct values. What is the largest number of element comparisons it can make?

A10
B500
C1,000
D1,000,000
Question 3 True / False

Because binary search is O(log n) and linear search is O(n), it is always faster to sort an unsorted list and then binary search it than to run one linear search.

TTrue
FFalse
Question 4 Short Answer

A list contains the target value at indexes 3, 7 and 12. A standard linear search that returns as soon as it finds a match is run on it. Which index does it return, and why?

Think about your answer, then reveal below.