How do you get better at solving programming problems?

The key idea

Practise turning an ambiguous question into a precise contract, then build and test a method that satisfies it. Learning another syntax feature is not always the missing step. Sometimes the problem is that two people interpret the required output differently.

Use a small example where the distinction matters: “find the second largest number.” Does a repeated largest value count again, or must the second value be different?

Define the task precisely

For this exercise, return the second largest distinct integer in a finite collection. If fewer than two distinct values exist, report that there is no result. The input may contain negative values and duplicates.

InputExpected resultReason
9, 3, 9, 77Repeated 9 does not count as a different value
4, 4No resultOnly one distinct value
-8, -2, -5-5Negative values follow the same ordering
6No resultA second distinct value does not exist

These examples are part of the specification, not an afterthought.

Begin with a clear method

A straightforward approach is to sort a copy of the collection and scan from the largest end until you find a different value. Keep the original unchanged if that is part of the contract.

This approach is easy to explain, but it has sorting and copying costs. Once it works, you can investigate a one-pass method that keeps the largest and second-largest distinct values seen so far.

Avoid inventing a sentinel value

Initialising both candidates to zero fails when all input values are negative. Initialising to the smallest integer also requires care if that value is a legitimate input.

Represent whether a candidate exists separately from its value. As you scan, ignore a value equal to the current largest, update both candidates when a new largest appears, and update the second candidate when an appropriate value lies below the largest.

Write those cases down before turning them into conditions. A shorter expression is not automatically a clearer or more correct solution.

Use counterexamples actively

If you think the method works, try to break the claim with a tiny input. Two equal values, a new largest arriving last and a negative-only collection each challenge a different assumption.

When a case fails, identify the first step at which the maintained state becomes wrong. Repair the rule responsible instead of adding an unrelated special case for that one input.

Build a repeatable practice routine

For each problem, keep the contract, a simple solution, a few counterexamples and one improvement with its cost. Explain the improvement in words before coding it.

Gammal Tech's sequence of C, C++, Data Structures and Algorithms gives you subjects that support this work. Progress is the ability to select and justify a method for a new question, not only to reproduce a familiar solution.

Choose your next step with Gammal Tech

Choose one skill you want to demonstrate, then review the relevant course and its prerequisites. Ask Gammal Tech about the content, study arrangements and current fees before enrolling.

References and course details

Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.

Articles & Guides