Foundation · Module 1 · Lesson 6
Problem-solving workflow
Examples, constraints, brute force, invariants, and optimization.
Lesson 6 of 123
1d streakDetailed theory
A reliable workflow turns an ambiguous prompt into a correct algorithm: clarify the contract, use constraints to set a budget, establish a brute-force baseline, identify repeated work, and prove the optimized invariant.
By the end, you can
- Translate constraints into a practical complexity target.
- Use brute force as a correctness model.
- Optimize by removing a named source of repeated work.
Vocabulary
Definitions with concrete meaning
Contract
The exact input, output, mutation rules, and behavior for edge cases.
Constraint budget
A rough upper bound on operations and memory implied by input limits.
Brute-force baseline
The simplest clearly correct method used to expose repeated work.
Invariant
A statement that remains true before and after every algorithm step.
Rules to apply
- 1Restate the problem with one small example before choosing a technique.
- 2Treat constraints as design information, not as decoration.
- 3Write brute force and derive its cost before optimizing.
- 4Name the repeated work or exploitable property that enables each optimization.
- 5Prove initialization, maintenance, termination, and edge-case behavior.
- 6Test the smallest inputs, duplicates, no answer, extreme values, and worst-shaped data.
Worked example 1
Two Sum: from pairs to complements
Return two distinct indices whose values sum to target.
seen = map()
for i = 0 to n - 1:
need = target - a[i]
if need in seen: return [seen[need], i]
seen[a[i]] = i- 1
Clarify
Return indices; positions must differ; duplicates are allowed.
- 2
Baseline
Check every i < j pair: Θ(n²) time and Θ(1) auxiliary space.
- 3
Find repeated work
For each value, we repeatedly scan earlier values looking for one complement.
- 4
Store searchable history
A hash map answers “have we seen target − a[i]?” in expected Θ(1) time.
- 5
Prove the invariant
Check before inserting the current value, so any found complement comes from a distinct earlier index.
Expected Θ(n) time and Θ(n) auxiliary space, trading memory for speed.
Worked example 2
Use constraints to reject an approach
n ≤ 200,000 and the time limit suggests roughly tens of millions of simple operations.
- 1
Estimate quadratic work
n² = 40,000,000,000 operations—far beyond the likely budget.
- 2
Estimate n log n
n log₂n is about 3.5 million comparison-scale steps.
- 3
Set a target
Look for Θ(n) or Θ(n log n), then verify constants and memory.
Constraint analysis narrows the search, but it does not prove correctness; the invariant still does.
Comparison table
Rough constraint guide
These are interview-style heuristics, not machine-independent laws. Language, constants, and time limits matter.
| Largest n | Often feasible | Patterns to consider | Be cautious with |
|---|---|---|---|
| ≈ 20 | Θ(2ⁿ) | Backtracking, subset DP | Θ(n!) |
| ≈ 200 | Θ(n³) | Dense DP, Floyd–Warshall | Large constants |
| ≈ 2,000 | Θ(n²) | Pair DP, all pairs | Heavy inner work |
| ≈ 100,000 | Θ(n log n) | Sorting, heaps, divide-and-conquer | Θ(n²) |
| ≈ 1,000,000 | Θ(n) | Scanning, counting, two pointers | Large memory per item |
Check your understanding
1Why write brute force if it will be too slow?
It establishes correctness, exposes repeated work, and provides a test oracle for small random inputs.
2Does a matching complexity budget prove an algorithm is suitable?
No. It must also be correct, fit memory, and account for constants and input shape.
3What are the three parts of a loop-invariant proof?
Initialization before the loop, maintenance after each iteration, and termination implying the result.
Key takeaway
Optimization should have a sentence: “The baseline repeats X; this data structure or property removes X while preserving invariant Y.” That sentence is more reusable than memorizing a solution.
Cost Model
Complexity & Trade-offs
When to use it
- Any interview, contest, or production algorithm task.
- Choosing techniques from constraints instead of keywords alone.
- Explaining why an optimization is correct and what trade-off it introduces.
Common mistakes
- Jumping to a memorized pattern before clarifying the contract.
- Optimizing without naming the repeated work being removed.
- Giving time complexity without auxiliary space, assumptions, or worst-case behavior.
Practice prompts
- Apply the workflow to “longest substring without repeating characters.”
- Derive a budget for n = 10⁵, then compare three candidate approaches.
- Use brute force as an oracle to test an optimized solution on random small inputs.
Mastery check