Stop 18 · Data Structures & AlgorithmsTopics · 17/17

Searching

Part of the Full Stack Developer Roadmap

In short

Every API that finds a user, an order or a product is doing a search. The question is how much work it does per lookup, and picking the right method is half the answer.

Algorithms and techniques for finding an element or condition within a data set — from linear search to binary search to more specialized graph search.

The purple blob holds up a key and checks a long row of parked scooters one by one.

Think of it like this

Finding your scooter in a big parking lot

If you didn't note the spot, you check scooters one by one: a linear search. If they were parked by number plate in order, you'd jump to the middle and narrow down. If you'd noted the row, you'd walk straight to it.

Watch it happen

Linear search vs binary search

Linear search

  • Checks items one by one
  • Works on unsorted data
  • O(n): 10 lakh items, 10 lakh checks

Binary search

  • Halves the range every check
  • Needs sorted data
  • O(log n): 10 lakh items, about 20 checks

Small or unsorted list, searched once? Linear is fine. Searched many times? Sort it, index it, or put it in a Set.

The big ideas

What you need to know

Linear search

includes, indexOf and find all check items one by one. Perfectly fine for small arrays.

Hash lookup

For repeated exact-match lookups, build a Set or Map once. Each check is then O(1) on average.

Binary search

Sorted data lets you halve the search space each time. This is how database B-tree indexes find rows quickly.

Graph search

BFS finds the shortest hop path; DFS explores everything. Grids and mazes are graphs too.

See it in code

Walk through the code

ts
1const bannedIds = ["u17", "u42", "u99" /* ...thousands more */];2 3// Linear search: checks one by one, O(n) per lookup4bannedIds.includes("u42");5bannedIds.find((id) => id.startsWith("u4"));6 7// Many lookups? Build a Set once, then each check is O(1) on average8const banned = new Set(bannedIds);9banned.has("u42");10 11// Sorted data? Binary search gives O(log n)12// Graph or grid? Use BFS (shortest hops) or DFS (explore everything)

1/3Array methods scan from the start every time you call them.

If an API checks every request against a ban list, the Set version saves real CPU time under load.

Save yourself the pain

Common mistakes

Oops

Calling array.includes() inside a loop over another big array.

Do this instead

That's O(n × m). Convert one array to a Set first to make it about O(n + m).

Oops

Sorting data just to search it once.

Do this instead

Sorting costs O(n log n), more than a single linear scan. Sort only if you'll search many times.

Your mission

Pick the right search

~30 min

Quick check

Your API checks each request's user ID against a list of 50,000 blocked IDs. What's the best structure?

Go deeper

  1. 1Compare linear search vs binary search performance conceptually on a large dataset.
  2. 2Practice recognizing which search technique fits a given problem's structure.
  3. 3Combine searching with other structures — e.g. binary search on the answer, a common advanced pattern.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge