FREE SAMPLE · NO ACCOUNT REQUIRED

Understanding the problem

Before you start coding, understand the problem, break it into parts, and turn your solution into clear steps.

Introduction

One of the main skills in programming is turning an unclear requirement into precise steps that can be executed and checked. Code expresses that thinking in a programming language.

By the end of this topic, you will be able to:

  • Identify what a problem asks for and what data it involves.
  • Break a large problem into smaller subproblems.
  • Express your solution using pseudocode and a flowchart.
  • Run an algorithm step by step on paper.
  • Find edge cases, compare alternatives, and explain why your solution is correct.

We will work through every stage using one example:

Find the second-largest distinct integer in a given list of integers.

This seemingly simple task shows why understanding a problem precisely matters.

Define the problem precisely: what are we solving?

Before writing code, answer four questions:

ConceptMain questionIn our example
ProblemWhat result do we want?Find the second-largest distinct integer
InputWhat data are we given?A list of integers
OutputIn what form should we return the answer?The number found, or an indication that no result exists
ConstraintsWhat limits and conditions apply?List size, range of numbers, and time and memory limits

The first important point is what “second-largest” means.

text● ● ●
Input: [12, 12, 9, 4]

If repeated values count as separate positions, the answer could be 12. If the second-largest distinct value is required, the answer is 9.

This difference changes the solution's logic. Resolve ambiguity in the requirement before writing code.

The contract we will use for this task:

  • The input is a list of integers.
  • Values may repeat.
  • Negative numbers are allowed.
  • The list may be empty.
  • If there are fewer than two distinct values, the absence of a result is indicated explicitly.
  • The original list's order is preserved.
text● ● ●
[7, 3, 9, 9, 5] → 7
[-8, -2, -5]    → -5
[4, 4, 4]       → No result
[]              → No result

Here, “No result” is a separate outcome. Replacing it with 0 or -1 would be incorrect: either number could itself be a valid answer.

In Java, this outcome can be represented with OptionalInt or a separate result type.

Why do constraints matter?

A solution that works for a list of 10 elements may be unsuitable for 10 million elements. If the data arrives as a stream, keeping every element in memory may also be impossible.

Constraints are therefore more than rules to check; they are information that affects the choice of algorithm.

Ready for the next step?

Explore the curriculum and create an account to continue in the learning workspace.

Create an account and open the course