Daily iOS
CS fundamentals and algorithms

Choosing a search algorithm

You're building name search over a list of 10,000 contacts. How do linear search and sort-then-binary-search compare, and what would you actually use in a shipping app?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Linear is O(n), binary search is O(log n), but you pay O(n log n) to sort first. Exact match wants a Dictionary, prefix match wants lower bound on a sorted array, and substring match wants a local DB index or a precomputed token inverted index.

Share a question