AlgoViz

Theory with examples

Easy

How to approach any problem

Problem

Before coding, restate the problem, note the input/output and constraints, pick a data structure, and trace one small example by hand. This habit turns a vague prompt into a concrete plan.

In simple words

Understand the question and try a tiny example before writing code.

The idea

Before writing a line of code, restate the problem in your own words, write down the exact input and output shapes, read the constraints, and hand-trace one small example. The constraints are the strongest hint available: they tell you which complexity will pass and therefore which technique to reach for.

The trick

  • n ≤ 10⁵ rules out O(n²); n ≤ 20 practically invites exponential search.
  • Hand-trace one example fully before coding — most wrong solutions are wrong in the first example.
  • Say the brute force out loud first. It gives you a correctness baseline and usually reveals the redundant work to remove.

This one walks through the worked example rather than tracing the algorithm frame by frame — a full walkthrough is still to be drawn. The code and the idea below are the real solution.

3
9
2
0
1
2

Step 1 of 2. Here's the example — Find the largest number in [3, 9, 2] Values: 3, 9, 2.

1/2
Optimal
timespace
11. Read the problem + constraints22. Write down input -> expected output33. Try a brute force, then optimise44. Dry-run a tiny example before coding

Input

array
[3, 9, 2]

Output

answer

Check yourself

1 quick question about this walkthrough. A wrong answer costs nothing.

Examples

Example 1

Input:
Find the largest number in [3, 9, 2]
Output:
9
Explanation:
Before writing code, walk the tiny case by hand: best = 3, then 9 beats it, then 2 does not. Now the loop writes itself.

Example 2

Input:
Find the largest number in [-4, -1, -7]
Output:
-1
Explanation:
The same walk. Starting best at 0 instead of the first element would wrongly answer 0 — which is exactly the bug a hand-worked example catches.

Finished the walkthrough? Add it to your streak.