Foundations
Step 1 in the DSA path · 46 concepts · 0 problems
📘 Learn Foundations from zero
The problem: two algorithms both solve a task — which is "better"? Timing them on your laptop is misleading: faster hardware, a different language, or a lucky input all change the number. We need a measure that depends only on the algorithm and the size of the input, written n.
Analogy: imagine planning a road trip. You don't care that one route is "5 minutes faster on a Tuesday." You care how each route scales: does travel time grow with the number of cities linearly, or does it explode (visit-every-pair)? Asymptotic analysis is exactly this — how does the work grow as the input grows toward infinity? Constants (your car's speed) and small detours (lower-order terms) wash out; the growth rate is what survives.
Worked example: find the max in an array of n numbers. You look at each element once, keeping a running best. For n elements you do roughly n comparisons. If n doubles, the work doubles — that is linear time, O(n). Now count duplicate pairs by comparing every element to every other: for each of n elements you scan n others ≈ n² operations. Double n and the work quadruples — O(n²). We write 3n + 7 as just O(n): the 3 and the +7 are irrelevant once n is large, because the n term dominates.
Key insight: asymptotic notation describes how an algorithm's cost grows, not how long it takes. Big-O bounds it from above; Θ pins the exact growth rate when upper and lower bounds match. Either way you keep only the dominant term and drop constants, so you can compare algorithms independent of machine, language, or input luck.
✨ Added by the guide to build intuition — not from the source course.
🎯 Guided practice
Problem 1 (easy): What is the time complexity?
- Code:
for i in range(n): print(i)then separatelyfor j in range(n): print(j). - Count each loop: the first runs
ntimes, the second runsntimes. Sequential blocks add:n + n = 2n. - Drop the constant factor
2→ O(n). Lesson: sequential code sums; constants vanish. - Contrast: if the second loop were nested inside the first, you'd multiply →
n × n =O(n²). Nesting multiplies, sequencing adds.
Problem 2 (medium): Solve the recurrence for binary search and for merge sort.
- Binary search recurrence: each call discards half the array and recurses on one half, doing O(1) work to pick the middle →
T(n) = T(n/2) + O(1). - Recursion-tree view: each level does constant work, and you halve until size 1, so there are
log₂ nlevels → O(log n). - Merge sort: split in half, recurse on both halves, then merge in linear time →
T(n) = 2T(n/2) + O(n). - Apply the Master Theorem with
a=2, b=2, f(n)=n. Compute the watershedn^(log_b a) = n^(log₂ 2) = n¹ = n. Sincef(n)=n = Θ(n^(log_b a))— they match up to alog^k nfactor withk=0— this is Case 2, givingΘ(n^(log_b a) · log^(k+1) n)→ Θ(n log n). - Pattern (the three cases): when you split into
asubproblems of sizen/bplusf(n)combine work, comparef(n)against the watershedn^(log_b a). If the watershed wins → Θ(watershed) (Case 1); a tie → ×log n (Case 2); iff(n)wins polynomially and satisfies the regularity condition → Θ(f(n)) (Case 3).
✨ Added by the guide — work these before the full problem set.
Lessons in this topic
- ○Understanding Data Structures
- ○Types of Data Structures
- ○An Overview of Big-O
- ○Introduction to Algorithm Analysis
- ○Measuring Efficiency
- ○Functions and Their Growth Rates
- ○Overview of Asymptotic Analysis
- ○Big-O Notation O-notation
- ○Big-Omega Notation Ω-notation
- ○Big-Theta Notation Θ-notation
- ○Little-o and Little-omega Notations
- ○Comparing Asymptotic Notations
- ○Understanding Time Complexity
- ○Analyzing Control Structures
- ○Analyzing Simple Algorithms
- ○Best, Worst, and Average Cases
- ○Constant Time O1
- ○Linear Time On
- ○Quadratic Time On²
- ○Quiz
- ○Understanding Space Complexity
- ○Analyzing Space Complexity of Algorithms
- ○Constant Space O1
- ○Linear Space On
- ○Quadratic Space On
- ○Quiz (2)
- ○Introduction to Recursion
- ○Recursion Tree Method
- ○Recurrence Relation Method
- ○Master Theorem Method
- ○Space Complexity Analysis of Recursive Algorithm
- ○Logarithmic Time and Space Olog n
- ○Linearithmic Time On log n
- ○Exponential Time and Space O2ⁿ
- ○Quiz (3)
- ○Array
- ○Linked List
- ○Hash Table and Set
- ○Stack and Queue
- ○Binary Search Tree
- ○Quiz (4)
- ○Sorting Algorithms
- ○Graph Algorithms
- ○Dynamic Programming Algorithms
- ○Coding Interview Problems
- ○Trade-offs in Algorithm Design