Skip to content

Lesson 1 of 27Read before you code

Reading the Problem

Read a coding question twice, strip the story, and restate exactly what it asks in one plain sentence.

13 min read0/6 questions

The Algorithms & Patterns course gave you the patterns. This track is about the step before them: working out which pattern a new problem needs, then solving it out loud under a clock. Each lesson expands one step of the method from How to Solve Problems. Most failed interview answers are not wrong algorithms. They are right algorithms for a slightly different question. The candidate skimmed, latched onto a familiar word, and spent twenty minutes solving something nobody asked. Reading carefully is the cheapest skill in this whole track, and it pays off on every single problem.

This lesson gives you a routine: read twice, strip the story, name the inputs and outputs, find the hidden question, and say it back in one sentence. You will use it on every problem from now on, and the Decoder at /practice/decode walks you through the same stages on real problems.

Read it twice, for different things

The first read is for the shape: what goes in, what comes out, roughly what kind of problem it is. Do not plan anything yet. Just get the picture.

The second read is for the details: every word that limits the answer. "Strictly" warmer, not warmer or equal. A later day, not any day. Return the indices, not the values. "In increasing order". These small words decide whether your code passes the hidden tests.

A good habit on the second read: underline (or type into your notes) every phrase that would change your code if it were different. If changing "later" to "any" would change the solution, it is load-bearing.

Quick check
In 'Buy once and then sell once on a later day', what does the word *later* rule out?
Need a nudge? A hint rules out a wrong answer.

Watch what the word does on a real input. The skim-reader's answer and the careful reader's answer differ.

One word changes the answer
3
0
8
1
6
2
1
3

Prices over four days. A skim-reader sees *buy low, sell high* and reaches for max minus min.

1/4

Strip the story

Problem setters wrap the same handful of ideas in stories: oranges rotting in a crate, a robot on a grid, a thief robbing houses, koalas eating bananas. The story helps you picture it, but it also hides the structure. Your job is to translate it back into plain data words: array, string, grid, graph, interval, count, index.

Match the pairs0/5 matched

Match the story phrasing to what it really means.

Choose an item, then its match on the other side.

Notice that none of the plain versions mention oranges, houses, or courses. If your restatement still contains the story nouns, you have not finished translating.

Inputs, outputs, and the hidden question

Write down three things before you think about an algorithm:

  • Inputs: the name, the type, and the size of each argument. "nums: list of ints, up to 10^4 long. target: one int."
  • Output: the exact type and form. A boolean? A count? A list of indices, and in what order? A modified array (and do you return it or change it in place)?
  • The hidden question: the smaller question that, once answered, solves the problem. For Two Sum it is "for this number, have I already seen its complement?". For Daily Temperatures it is "which earlier days are still waiting for a warmer one?".

The hidden question is where the algorithm lives. You usually cannot see it on the first read. You find it by asking "what would I need to know, at each step, to produce the answer?".

Put it in order5 steps

Put the reading routine in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

The one-sentence restatement

If you can say the problem in one plain sentence, you understand it. If you cannot, you do not, and no amount of coding will fix that. In an interview, saying it out loud also lets the interviewer correct you before you waste time.

A good restatement names the input, the output, and the rule, with no story:

  • "Given an array of ints, return the two indices whose values sum to target. Exactly one pair exists."
  • "Given a grid of 0, 1, 2, return the number of BFS rounds until no 1 is left, or -1 if a 1 is unreachable."
  • "Given a string of brackets, return whether every closer matches the most recent unmatched opener."
Quick check
Which is the best restatement of Best Time to Buy and Sell Stock?
Need a nudge? A hint rules out a wrong answer.

Worked examples on three real problems

Here is the full routine on three problems from Practice. Try each one yourself before reading the breakdown.

Two Sum

Given a list of integers nums and a number target, find the two different positions whose values add up to target. Return their indices in increasing order. Exactly one valid pair exists.

  • Limiting words: "two different positions" (you cannot use one index twice, but two equal values at different indices are fine), "indices" (not values), "exactly one" (stop at the first pair).
  • Inputs: nums list of ints, target int.
  • Output: a list of two indices, smaller first.
  • Hidden question: "for the number in front of me, did I already see target - x, and where?"
  • One sentence: "Return the two indices i < j with nums[i] + nums[j] == target; one such pair exists."

Daily Temperatures

For each day, return how many days you have to wait for a strictly warmer day, or 0 if none comes.

  • Limiting words: "strictly" (equal is not warmer), "after" (only look right), "0 if none".
  • Output: a list the same length as the input, of distances, not temperatures.
  • Hidden question: "when a warm day arrives, which earlier days was it the answer for?"
  • One sentence: "For each index, return the distance to the next index with a strictly greater value, or 0."

Rotting Oranges

A grid has 0 for empty, 1 for fresh, 2 for rotten. Each minute, rotten oranges rot their four neighbours. Return the minutes until nothing is fresh, or -1 if impossible.

  • Limiting words: "above, below, left and right" (4 directions, not 8), "-1 if some fresh orange can never be reached", "every minute" (all rotten oranges spread at the same time).
  • Hidden question: "how many rounds of spreading, starting from all rotten oranges at once?"
  • One sentence: "Multi-source BFS from every 2; return the number of levels needed to reach every 1, or -1."
Predict the outputPython

Using the restated rule (largest prices[j] - prices[i] with j > i, else 0), what does this print?

prices = [9, 7, 4, 3]
best = 0
for j in range(len(prices)):
  for i in range(j):
      best = max(best, prices[j] - prices[i])
print(best)
Need a nudge? Hints walk you through it.
Quick check
What is the hidden question in Valid Parentheses?
Need a nudge? A hint rules out a wrong answer.

How this course is laid out

  • Read before you code turns a prompt, its examples and its constraints into a clear question and a list of edge cases.
  • From idea to plan goes from a brute force to the right data structure and pattern, checked by a dry run.
  • Talk it through covers narrating, getting unstuck, debugging, explaining complexity, testing and follow-ups in a full 45-minute round.
  • Decode by category gives you the deciding clue for each family of problems, from arrays to design.
  • Practice and polish covers multi-part practical problems and a practice system that makes all of it stick.

By the end you can take a problem you have never seen and solve it out loud, calmly, inside the clock. The course closes with a wrap-up that ties it all together.

Practice the routine

Before you write any code for this problem, do the routine out loud: limiting words, inputs, output, hidden question, one sentence. Then solve it.

ChallengeEasy

Two Sum

Given a list of integers nums and a number target, find the two different positions whose values add up to target. Return their indices as a list in increasing order.

nums = [2, 7, 11, 15], target = 9    →  [0, 1]   (2 + 7)
nums = [3, 2, 4],      target = 6    →  [1, 2]   (2 + 4)
nums = [3, 3],         target = 6    →  [0, 1]

You may not use the same index twice.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Exactly one valid answer exists.
def two_sum(nums: list[int], target: int) -> list[int]: # For each number, what partner would you need? pass
Key terms
limiting word
A word in the prompt that would change your code if it changed, like *strictly*, *later*, *indices*, or *in place*.
strip the story
Translate the narrative (oranges, houses, robots) into plain data words: array, grid, graph, interval, count.
hidden question
The smaller question you answer at each step that, once answered, solves the whole problem.
restatement
One plain sentence naming the input, the output, and the rule, with no story. Say it out loud before coding.
two reads
The first read is for shape (what goes in and out); the second is for every detail that limits the answer.