Now and then I work on small programming puzzles. I do this mostly because I enjoy it, but it’s also a great way to try out new languages, tools, and techniques, or to to maintain familiarity with those you haven’t used in a while.
Below are three of my favorite sources of programming exercises.
The venerable Project Euler has accumulated over five hundred problems in its fourteen years. The site is named after a mathematician, and many of the problems are very math-heavy.
Typically a brute-force approach will take way too much time and/or memory, but the problems are designed such that, “an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute.” (source)
Upon solving each problem, you’re given access to a forum thread to compare and discuss solutions with other users.
Some thoughts and advice:
- Some problems can be reasonably solved with brute force, but are later repeated with larger inputs that cannot, requiring you to write an efficient solution. For example, if you solve #18 naively, your solver will get stuck on #67.
- Because you provide only the answer, your selection of tools is not restricted at all. Want to use Matlab? TI-BASIC? Abacus? No problem.
- For many problems, it’s been handy to have access to big numbers and data structure serialization (e.g. gmplib and boost serialization for C++).
- It can be really difficult to build a correct solution with long iteration times and literally one bit of feedback. Build additional, smaller testcases whenever possible!
- My favorite problem so far is #393 – “Migrating ants.”
CodeEval is a both a challenge site for programmers and a recruiting tool for companies. In my view, the distinguishing characteristic is that it runs your code, enabling some cool features:
- Submissions are scored based on memory usage and runtime:
Test inputs may be varied to prevent you from merely printing a known, precomputed answer.
Notably, CodeEval provides more than one bit of feedback when your submission fails:
- When you fail an easy level challenge, you are given both the input that was used to test your submission and your program’s output.
- When you fail a moderate or hard challenge, you are not given the input, but you are given your program’s output. I’m not sure they thought this through.
The paste-your-code-and-they-run-it-for-you approach has some downsides, though:
- There’s a specific set of supported languages, and you don’t get to choose the compiler/interpreter, language dialect, or any configuration options.
- Your solution needs to be contained entirely in one file.
Finally, I must sadly note that CodeEval’s Clojure support is broken, and has been for some time.
Exercism.io is dramatically different: rather than focusing on concrete outcomes like the right answer (Euler) or a fast and efficient solution (CodeEval), Exercism seems to focus on the craft of building software:
- TDD – each exercise actually comes in the form of a set of failing unit tests!
- Technical Communication – after you build a passing implementation, instead of submitting it to a robot for grading, you submit it to humans for critique. This is crowdsourced: feedback comes from other users, and you can critique other users’ submissions.
- Refactoring – Each round of discussion will likely lead to changes in your solution code. There are also refactoring exercises, in which you are given code that works but is ugly, and your job is to clean it up.
I’ve only done a handful of challenges, but so far I really like it. It’s fun to build an implementation against an existing set of unit tests, and the community seems to be knowledgable and respectful.
Exercism offers a good mix of supported languages and a slick command line client for fetching exercises and submitting solutions. Give it a try!
Programming exercises like these provide a fun way to sharpen your skills without a large time commitment. Do you work problems like these? What are your favorite sources?