Time/Space
Tradeoffs
Every algorithm makes a choice: use more memory to run faster, or use less memory and accept slower execution. Master this fundamental tension in C++.
What Is a Tradeoff?
In computing, a tradeoff is a situation where improving one quality of a program comes at the cost of another. The most common tradeoff is between time (how fast code runs) and space (how much memory it uses).
The Balancing Act
Why Does This Matter?
In real-world C++ programs, you face finite resources. A game running on a phone has limited RAM. A web server must respond in milliseconds. Understanding tradeoffs lets you choose the right tool for each constraint.
The Core Principle
Quick Big-O Refresher
Big-O notation describes how an algorithm's resource usage grows as the input size \( n \) increases. We apply it to both time and space.
| Notation | Growth | Example |
|---|---|---|
| \( O(1) \) | Constant | Array index access |
| \( O(\log n) \) | Logarithmic | Binary search |
| \( O(n) \) | Linear | Single loop |
| \( O(n^2) \) | Quadratic | Nested loops |
Caching vs Recomputing
The most common time/space tradeoff pattern: cache results you'll need again, or recompute them each time.
Example: Fibonacci
Computing \( F(n) \), the \( n \)-th Fibonacci number, is the classic illustration of this tradeoff.
Fibonacci in C++ Code
How to Apply the Tradeoff
Example: Lookup Tables
Problem: Given a character, determine if it is a vowel. You'll check this millions of times in a text-processing loop.
Example: Two Sum Problem
Given an array of \( n \) integers and a target, find two numbers that add up to the target.
| Approach | Time | Space |
|---|---|---|
| Brute Force (nested loops) | \( O(n^2) \) | \( O(1) \) |
| Hash Map lookup | \( O(n) \) | \( O(n) \) |
Example: Finding Duplicates
Given \( n \) numbers, determine if any value appears more than once. Three approaches show the tradeoff spectrum:
The Tradeoff Spectrum
Common Mistakes
Summary Table
| Pattern | Time | Space | When to Use |
|---|---|---|---|
| Memoization | Faster | \( +O(n) \) | Overlapping subproblems |
| Lookup Table | \( O(1) \) access | Table size | Repeated queries |
| Hash Map | \( O(1) \) avg | \( O(n) \) | Fast search/insert |
| In-place Sort | \( O(n \log n) \) | \( O(\log n) \) | Memory-constrained (avoids \(O(n)\) space) |
| Recomputation | Slower | Minimal | Tiny memory budget |
Question 1 of 5
The naive recursive Fibonacci function has time complexity \( O(2^n) \). By adding an array to store previously computed values, the time complexity drops to:
Question 2 of 5
A programmer replaces a nested loop (\( O(n^2) \) time, \( O(1) \) space) with a hash map approach (\( O(n) \) time, \( O(n) \) space). What tradeoff was made?
Question 3 of 5
You are writing software for an embedded device with only 2 KB of RAM. Which approach to finding duplicates in a list of 400 integers is most appropriate?
Question 4 of 5
A lookup table of size 256 is created to instantly check character properties. The table uses 256 bytes of memory. What is the time complexity of each lookup?
Question 5 of 5
Which statement best describes the time-space tradeoff principle?