Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
FOUNDATIONS

Searching

Find a target - linearly on any data, or in O(log n) when it’s sorted.

Beginner7 problems4 interactive subtopicsO(log n) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (4)Open in full playground →
STEP 1/4Search the whole array
target = 23
012345678
2
5
8
12
16
23
38
56
72
lo
hi
  • inside the window
  • ruled out
  • active this step
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONnums = [2, 5, 8, 12, 16, 23, 38, 56, 72], target = 23
1def binary_search(nums, target):
▶2 lo, hi = 0, len(nums) - 1
3 while lo <= hi:
4 mid = (lo + hi) // 2
5 if nums[mid] == target:
6 return mid
7 elif nums[mid] < target:
8 lo = mid + 1
9 else:
10 hi = mid - 1
11 return -1
STEP 1 OF 4Setup

Search the whole array

lo = 0 and hi = 8 cover all 9 values.

VARIABLES

lo
0
hi
8
mid
-
nums[mid]
-
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Linear search checks every element. Binary search halves a sorted range at each step by comparing the middle element with the target.

Why it matters

Halving turns a scan of a million elements into about 20 comparisons.

Think of it like this

Guessing a number from 1 to 100 by always guessing the middle: you never need more than 7 guesses.

Use it when

  • The data is sorted, or a condition is monotonic
  • You need the first or last position meeting a condition
  • Repeated lookups on data that doesn’t change

Avoid it when

  • Unsorted data you search only once - a linear scan is simpler
  • Frequent inserts - keeping the array sorted costs O(n) each

Complexity cheatsheet

OperationTimeSpace
Linear searchO(n)O(1)
Binary searchO(log n)O(1)
Hash set lookupO(1) averageO(n)

Often confused with: two pointers - also uses sorted order, but moves one step at a time, so it’s O(n).

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

sortedO(log n)find the positionfirst or last occurrenceinsert position
  • A required O(log n) runtime almost always means binary search
  • An answer range up to 10⁹ hints at searching the answer space
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Writing while lo < hi with hi = mid − 1 - the last candidate is never checked
  • Setting lo = mid instead of mid + 1 - an infinite loop
  • Forgetting that the array must be sorted

Edge cases to test

  • Empty array - return -1 right away
  • Target smaller or larger than every value
  • Duplicates - decide whether you want the first or last match
Interview tip: Decide whether lo and hi are inclusive or exclusive before coding, and keep it consistent.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

About how many comparisons does binary search need for 1,000,000 sorted items in the worst case?

06

Practice

Apply what you just watched on real interview problems.

Generate Binary Search problem in AI DSA Practice →
  • BeginnerBinary SearchLeetCode 704 ↗ (opens in a new tab)
  • IntermediateFind First and Last Position of Element in Sorted ArrayLeetCode 34 ↗ (opens in a new tab)
  • AdvancedMedian of Two Sorted ArraysLeetCode 4 ↗ (opens in a new tab)
SortingLinked Lists