- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- How to Approach a DSA Problem
DSA Introduction
How to Approach a DSA Problem
Most people fail these problems by starting to type. The thinking is the work; the code is the easy part once the thinking is done.
The method
- Restate the problem in your own words, out loud. Half of all wrong answers are answers to a different question.
- Write down one example by hand, including an awkward one - empty input, one element, duplicates.
- Solve it the obvious slow way. A working brute force beats an elegant idea you cannot finish.
- Name the bottleneck. Which step repeats work? That is the only part worth improving.
- Improve just that step, usually by trading memory for time.
Worked through
Given an array of numbers and a target, return the two indices that add up to the target.
Step 3, the obvious way - try every pair:
Brute force
javascript
