๐Ÿ”

Searching Fundamentals

Understand linear search, binary search intuition, and when to apply each.

0 Problems45 min

Overview

Searching means finding a target value in a collection. Linear search checks every element in O(n). Binary search works on sorted data and eliminates half the search space each step, achieving O(log n). Knowing when your data is sorted (or can be sorted) is the first step to applying binary search.

Complexity Summary

Time Complexity

Linear O(n) ยท Binary O(log n)

Space Complexity

O(1) for both iterative approaches

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Linear search

Concept

scan from left to right, works on any unsorted data, O(n)

Practice questions for this pattern
2

Binary search

Concept

requires sorted input, halves search space each step, O(log n)

Practice questions for this pattern
3

Precondition

Concept

binary search only works when elements are sorted or monotone

Practice questions for this pattern
4

Template

Concept

left=0, right=n-1, mid=(left+right)/2, compare and narrow

Practice questions for this pattern
5

Off-by-one

Concept

understand whether to use left <= right or left < right based on problem

Practice questions for this pattern
6

Search on answer

Concept

binary search on the answer value range, not on array index

Practice questions for this pattern
7

Sorting trade-off

Concept

sorting costs O(n log n) but enables O(log n) repeated searches

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview