Think Like a Programmer
Data Structures & Algorithms · First Steps · Basics · 12 min
Variables, loops and a 5-step recipe to solve ANY coding problem.
A very literal robot holding one labelled box. It walks along a row of numbers, looks at one at a time, and updates the box. When the row ends, the box holds the answer.
The idea in everyday words
Coding is like giving instructions to a very obedient but very literal robot chef. It does EXACTLY what you say, step by step. If you say 'add salt' without saying how much, it gets confused. Programming is just learning to break a big task (make a pizza) into tiny, clear steps (take dough, roll it, add sauce...).
The 3 building blocks
Every program in the world is made of just three things:
- Variables = labelled boxes that hold a value.
age = 25(A handy first picture. As the last lesson said, in Python the name is really a label tied to a value.) - Decisions (if/else) = choose a path. If it's raining, take an umbrella.
- Loops = repeat something. For each plate in the sink, wash it.
That's it. Big apps like Instagram are millions of these tiny blocks stacked together.
The 5-step problem solving recipe (use it in EVERY interview)
- Understand – Repeat the problem in your own words. What goes in? What comes out?
- Example – Try a tiny example by hand. Input
[3, 1, 2]→ output6. - Brute force – What is the dumbest solution that works? Say it out loud.
- Improve – Can I avoid repeated work? (Usually: a hash map, sorting or two pointers.)
- Code + Test – Write it, then dry-run your tiny example line by line.
The Playbook page uses the same recipe in 6 steps: it gives "find the waste" its own step and splits code and test in two. Same idea, more detail.
Example: add up a shopping bill
Problem: given prices [10, 20, 5], find the total.
- Keep a box called
totalstarting at 0. - Look at each price, add it to the box.
- After the loop, the box holds 35.
This "keep a running answer while looping" trick is the #1 pattern in coding.
A handy shortcut: +=. Writing total = total + p again and again gets long, so Python and JavaScript let you write total += p. It means exactly the same thing: take what is in total, add p, and put the result back in total. So count += 1 means "add one to count". You will see += a lot from now on.
Step by step
- Start:
total = 0. Prices = [10, 20, 5].
- Look at 10:
total = 0 + 10 = 10
- Look at 20:
total = 10 + 20 = 30
- Look at 5:
total = 30 + 5 = 35
- Done:
Loop ends. Return total → 35.
Code example
prices = [10, 20, 5]
total = 0
for p in prices:
total = total + p
print("added", p, "-> total is", total)
print("Final bill:", total)Complexity
- Time: O(n) – we look at each price once
- Space: O(1) – only one box (total)
Interview tips
Always talk while you think. Interviewers grade your thinking, not just the code.
Start with brute force out loud, then improve. Never sit silent.
Test your code with an empty input and a single-item input.
Challenge: Count the Big Spenders
Given a list of purchase amounts and a number limit, return how many purchases are greater than limit.
Example: count_above([5, 50, 20, 100], 19) → 3
Solve it in the browser with tests, hints and an AI coach.
Practice problems
- Running Sum of 1d Array (Easy)
- Richest Customer Wealth (Easy)
- Find Numbers with Even Number of Digits (Easy)
- Kids With the Greatest Number of Candies (Easy)
More in First Steps
- Coding from Zero
- Python Crash Course for DSA
- Functions, Lists & Strings
- Classes & Objects
- Big-O: How Fast Is My Code?
- Recursion
Previous: Coding from Zero · Next: Python Crash Course for DSA