Programming Exercises to Stretch your Noodle

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.

Project Euler

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.

Leonhard_Euler_by_Handmann The answers are single integers. It provides a pretty spartan experience: you put your answer in the box and it says “yes” or “no.”

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

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.
  • Plagiarism detection.

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

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!

Conclusion

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?

Conversation
  • Romain C. says:

    Thanks for these entries I didn’t knew about.

    I am myself into https://www.codingame.com/
    Excelent platform quite similar to CodeEval, if I only look at your description.

    Several problems, categorized by difficulty, coding is done online with 20+ languages available.
    Small bonus for having “geek oriented” problems using background such as Batman, H2G2, Futurama…

    I also tested http://codecombat.com/ some time ago which is quite different. You mostly program Character IA in a Real-time strategy manner.

    • John Ruble John Ruble says:

      Thanks for the comment, Romain! I’ll definitely check those out.

  • Romain C. says:

    I just tested Code Eval, the site is painfull! Every action requires a full page reload.
    The problem database seems quite huge, but I largely prefer codingame, which is much more dynamic.

  • Comments are closed.