A better way to think.
Learn how to turn an unclear task into a model, an algorithm, and evidence.
01 Model the idea
Data structures organise information; algorithms describe how to work with it. Neither is a contest to use the most elaborate technique. Start by writing the required input and output, choose operations that matter, and test a small example by hand. This course explains the ideas independently of a language and uses readable Python 3.11+ implementations you can run with only the standard library. You do not need to finish the separate Python track first, but basic variables, loops, functions, and lists will help.
The candidate is the smallest value in the inspected prefix.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
SET candidate to the first value
FOR each remaining value
replace candidate if the value is smaller
RETURN candidate| Value examined | Smallest so far |
|---|---|
| 12 | 12 |
| 4 | 4 |
| 19 | 4 |
| 7 | 4 |
03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
values = [12, 4, 19, 7]
smallest = values[0]
for value in values[1:]:
if value < smallest:
smallest = value
print(smallest)4
- 1
The problem asks for a value, not a sorted list. Sorting the entire input would do unnecessary work just to obtain its minimum.
- 2
Trace the current candidate after each comparison. The candidate represents a claim about the prefix already inspected, which is the beginning of an invariant.
- 3
Run examples from the downloadable DSA workbench or copy one into a .py file. Use the visualiser to predict the next operation, then compare your prediction with the step explanation.
04 Count the cost
O(n)
O(n) temporary slice in this exact example
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Write a minimum function that explicitly rejects an empty sequence.
Reveal one possible solution +
def minimum(values):
if not values: raise ValueError("At least one value required")
best = values[0]
for value in values[1:]:
if value < best: best = value
return best
print(minimum([12, 4, 19, 7]))06 Check your reasoning
What should you define before choosing an algorithm?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.