Beginner Topic 15 of 51 Algorithms
Brute Force
Trying every candidate solution exhaustively.
How it works
Brute force solves a problem by trying every possible candidate and checking each one, so correctness is easy to argue but the running time often grows exponentially or polynomially with a high power. It is the simplest strategy and gives a baseline for judging cleverer algorithms. Pruning obviously invalid candidates turns it into backtracking.
Step-by-step walkthrough
- List every possible candidate answer.
- Test candidate 1 against the requirements.
- Test candidate 2.
- Continue until one works or all are tried.
- It is simple and always correct, but cost grows quickly, for example 2 to the n subsets.
Where it is used
Small input sizes, password-strength checks, verifying a faster algorithm against a reference in tests, solving tiny puzzles and exploring search spaces where no better approach is known use brute force.
Common pitfall
Time can explode: trying all subsets of 40 items is a trillion cases. Do the arithmetic on the search-space size before coding. Look for structure to prune candidates, sort first, use hashing or switch to dynamic programming. Keep a brute-force version for validating optimized solutions.
Where it fits in the course
Brute Force is topic 15 of 51 in Algorithms, rated Beginner. It follows Radix Sort. The next topic is Two Pointers.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Brute Force?
Where do people use Brute Force?
What is the most common mistake with Brute Force?
What should I learn before this topic?
How can I practice it?
Is Brute Force hard to learn?
How long does it take to learn Brute Force?
Which topics are related to Brute Force?
What should I study after Brute Force?
Related topics
- Radix Sort (Algorithms)
- Two Pointers (Algorithms)
- Counting Sort (Algorithms)
- Sliding Window (Algorithms)