What an algorithm is and how to trace one
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Define an algorithm as a finite, ordered, unambiguous procedure.
- Trace a short algorithm by hand using a state table.
- Explain why the same algorithm can be written in two languages.
Overview
#What an algorithm is
An algorithm is a finite, ordered list of unambiguous steps that turns an input into an output. It is the procedure, not the programming language that expresses it. Two people can write the same algorithm in JavaScript and Python and still follow the same steps.
A useful algorithm has three properties:
- Finite: it stops after a bounded number of steps.
- Definite: every step has exactly one meaning.
- Effective: each step is simple enough to carry out.
Tracing by hand
Tracing means running the algorithm on paper and writing down the value of every variable after each step. Do not just read the code and nod. Keep a table.
Consider finding the largest value in a list:
largest = first element
for each remaining element x:
if x > largest:
largest = x
return largest
Trace with input [3, 9, 4]:
| Step | x | largest |
|---|---|---|
| start | - | 3 |
| compare 9 | 9 | 9 |
| compare 4 | 4 | 9 |
After the loop largest is 9. The table is the evidence that you understood the flow, not just the final answer.
Why tracing matters
Complexity analysis, data-structure choice and debugging all depend on being able to say exactly what the machine does at each step. When a solution fails, the trace shows the first step where your mental model diverged from reality. That first divergent step is the bug.
Practice next
- Count the number of comparisons the largest-value algorithm performs on a list of length n.
- Trace the same algorithm on an empty list and decide what should happen.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Trace the largest-value algorithm on the input [7, 2, 7, 1]. What value is returned?
starter.javascriptfunction largest(values) { let best = values[0]; for (let i = 1; i < values.length; i++) { if (values[i] > best) best = values[i]; } return best; }Reveal the reference answer
reference.javascriptbest starts at 7. 2 is not greater, so best stays 7. 7 is not greater than 7, so best stays 7. 1 is not greater. Return 7.Authored expected output: 7
The strict greater-than comparison means an equal value never replaces best, so the first 7 survives. This is also why the algorithm is stable with respect to duplicates.
Exercise 2Explain
Two developers implement the largest-value search, one in JavaScript and one in Python. Explain why they are using the same algorithm even though the code differs.
Reveal the reference answer
reference.textBoth implementations perform the same ordered steps: initialise a running maximum from the first element, then visit each remaining element once and update the maximum only when the new value is larger. The syntax and the type system differ, but the sequence of decisions and the state that is carried between steps is identical.Authored expected output: Same ordered steps and same running-maximum state, expressed in different syntax.
An algorithm is defined by its steps and state transitions, not by its surface syntax.
Exercise 3Implement
Write the steps, in plain pseudocode, for returning the smallest value in a non-empty list.
starter.textsmallest = ??? for each remaining element x: ...Reveal the reference answer
reference.textsmallest = first element for each remaining element x: if x < smallest: smallest = x return smallestAuthored expected output: Pseudocode that initialises smallest from the first element, compares each remaining element with <, and returns smallest.
Initialising from the first element avoids the common bug of starting at 0, which is wrong for all-positive lists.
Common misconceptions
#- An algorithm is the same thing as a program. A program is one encoding; the algorithm is the steps.
- Tracing means reading the code again. Tracing means writing down state after each step.
- If it works on one input it is correct. Correctness is a claim about every valid input.
Follow-up practice
#- dsa-from-zero-counting-steps-and-big-o
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.