A girl and a robot thinking together

Let’s think it through · Start with a story

The robot rushes to type code, but the girl first asks it to arrange three cards on paper. A few minutes later, they find a case it had missed.

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Try an example on paperWork it out by hand first
  2. Describe the methodWhat happens at each step?
  3. Look for counterexamplesTry boundaries and duplicates
  4. Then write the codeExpress the steps you have worked out

Designing a solution before coding helps separate mistakes in the rules from syntax errors.

Small examples help you see whether the method covers all the required cases. Then the code has a clear plan to follow.

01Why not start coding as soon as you see a problem?

Because writing code is only “implementing a solution”; it is not the same as understanding the problem.

If you have not understood the task yet, you may:

  • Misunderstand the goal.
  • Miss a special case.
  • Choose an algorithm that is too slow.
  • Write a lot of code before realizing you are on the wrong track.

02Start by asking four questions

What is the input?

What data will the program receive?

What is the output?

What must it print at the end?

What is the goal?

What exactly must be calculated or found?

How large are the inputs?

What is the maximum value of n?

03Restate the problem in your own words

If you cannot explain the problem in one or two sentences, your understanding may not be solid yet.

04Work through a small example by hand

Do not rely only on the official sample. Make up a small case with just 1–3 elements and calculate the answer yourself.

This helps you check whether your interpretation of the rules matches what the problem actually asks for.

05When should you start thinking about algorithms?

Once you have clarified:

  • The problem’s goal
  • The input and output
  • The constraints
  • At least one example you have worked through by hand

Then ask: “Which methods do I know that could solve this?”

06When should you start writing code?

Understand the problem→Work out the algorithm→Then code it

Code should express an approach once your idea is reasonably clear; it should not replace the thinking that comes first.

What you have learned

  • The first step in solving a problem is understanding it, not writing code.
  • Clarify the input, output, goal, and size of the data first.
  • Restating the problem in your own words can check your understanding.
  • Small hand-worked examples can help verify your model.
  • Code implements an algorithm; it should not replace the analysis that comes before it.

Next: How Do You Really Understand a Problem?

Your turn

What is the benefit of noticing before coding that your method misses duplicate numbers?

Have an answer? Click to see the explanation

You can fix the method first instead of redesigning it after writing a lot of code.