A very common unanswerable question I see on StackOverflow is of the form “my CS homework assignment is to solve problem X and I don’t even know how to get started. How do I get started?” That’s too vague and unfocussed for a site like StackOverflow, which is for specific technical questions that have specific answers.

My recent post on the similarly vague problem of how to debug small programs has gotten a lot of hits and great comments; thanks all for that. In light of that I thought I might do an irregular series each highlighting some basic problem-solving techniques for beginner programmers, CS students and the like.(Of course these apply to expert programmers too, but expert programmers often already know these techniques.) So, how do you get started?



Pólya famously said that the way to solve the problem was: (1) understand the problem, (2) come up with a plan, (3) execute the plan, and finally (4) review your work. It’s that second step that concerns me today; if you don’t understand what the problem is even asking you to do then get some clarification from your instructor.

I see a lot of beginner code that was clearly written with no particular plan in mind. But making a plan is itself a problem; how do you attack that problem?

One of my favourite techniques for doing so is to again take the advice of Pólya:

If you can’t solve a problem then there is an easier problem you can solve. Find it.

What good is that?

The solution to the simpler problem might be adaptable to solve the harder problem. Now you have a plan: solve the easier problem, look for an adaption.

Conversely, the inability to adapt the simpler problem to the harder problem might shed light on precisely what makes the harder problem harder. Come up with a plan to tackle the specific thing that makes the problem harder.

Solving simpler problems that you know you can do is good practice and builds confidence. And you’re more likely to be successful if you start with code that already correctly solves a problem.

If you fail to solve the harder problem you can always submit the solution to the simpler problem for partial credit.

Let me give you an example. Suppose your homework assignment is to build a lookup mechanism mapping one million English words to their definitions. You are required to implement the lookup as a binary search tree optimized such that the most commonly looked-up words are always close to the root of the tree. And suppose you don’t know where to begin. Here’s one path you could go down to simplify the problem:

Solve the problem for a dictionary of ten words. Scale it up from there, or figure out why your solution does not scale up.

Maybe that’s still too hard. Build a binary search tree lookup mechanism that maps ten words to definitions that is not optimized. Once you’ve got that, try to figure out how to optimize it.

Maybe that’s still too hard. Build a binary search tree lookup mechanism that maps ten integers to integers that is not optimized. Then later figure out how to replace the integers with strings.

If that’s still too hard, can you build a binary search tree just of single integers, with no mapping? Add the mapping later.

If that’s still too hard, can you build a binary tree just of single integers even if its not a search tree? Make it a search tree later.

Can you build a linked list of integers?

How about an array of integers?

Can you write “Hello world” in this language?

It might sound a bit silly to go that far, but it’s not. When you’re writing twenty-line programs for assignments every program begins its life as “Hello world”. In industry that’s not the case; the number of times I’ve started new shipping code from a blank page and written static void Main(){ is exactly one, and even that was a rare privilege. Almost all the code you write for assignments will start from “Hello world”, so get good at writing it.

There are lots of ways to make a problem simpler; I already mentioned:

Decrease problem size requirements.

Neglect optimizations.

Simplify data structures.

But there are lots more ways to simplify a coding problem. For example:

Reduce dimensionality. If you have a problem about drawing rectangles can you reduce it to a problem about drawing lines? Or even points?

Cut features. If your problem requires reading a dictionary file from disk, instead hard-code the strings into your program.

Readers, what are your favourite general techniques for reducing the complexity of a problem?