How to build your own programming puzzle

STAY CONNECTED, FOLLOW CODINGAME NOW
After having solved many programming puzzles, have you ever thought about creating your own puzzle? You’re not sure where to start? Or do you think you’re not up to the task? Here are 5 easy steps that should help you create your own coding puzzle.

1/ Pick a theme

You create a puzzle for other developers to solve it. They solve puzzles for several reasons, one of them being entertainment. Your first job is to entertain the solver. The theme won’t make it all but without a theme you surely won’t have a great puzzle.
Preferably choose a theme you like. It will be easier to create a top-notch puzzle.
Now that you have a theme, you can find an idea of puzzle more easily, or adapt the idea you already had to the theme.
You should also think about the originality of your puzzle/theme. Try to avoid classic topics of programming puzzles (like Ascii Art).

Ex: I’m fond of tennis, the goal of my puzzle will be to compute a tennis score from the chronological list of points.

2/ Write tests

You’ve got the idea of the puzzle: there should be a goal and some rules to achieve it. Make sure to define clear rules.
How will you check that a code has solved your problem? If it passes the tests you’ve created for it.
Simply write all the conditions a solution should respect to be valid and make a test for each one of them*.
If it’s a scaling problem, you’ll have to write tests of simple scenarios and at least one test for a larger scenario, so a brute force solution would fail this test (ie timeout).

Ex: My tests should verify that a solution correctly computes the score of a game and set, handles deuce and tie break...

3/ Write the solution of your puzzle

This part helps you assess the difficulty of your problem. A too hard puzzle might deter some developers from solving it. On the other hand a too easy puzzle won’t be challenging for advanced developers. You can tweak the rules (and obviously modify the corresponding tests and validators) to modify the difficulty.
Even if you know the solution to your puzzle**, it might be a good idea to check on the internet for similar problems. Maybe you’ve missed a trivial solution or forgotten a specific test case.

Ex: I found a StackExchange topic about my problem which makes me realize my puzzle may be too simple. To make it more tricky, I could decide to add incomplete inputs…

4/ Present your puzzle

This could be a boring part but this is highly important. The problem/puzzle might be familiar to you, but developers will just discover it.
A complete statement usually contains: a small story, the description of the problem (goal and rules), the description of the inputs and expected output along with an example. All of it must be very clear and simple to understand, else developers might not even try to solve it. The story shouldn’t be too long. It is used to create a fun atmosphere around the programming puzzle.
Title is also important, make it sexy! However it should not misleading.

Ex: Instead of "tennis scoring system", I could call the puzzle "Game, Set & Match". To be sure the description is clear, I would choose wordings from the related wikipedia article.

5/ Ask friends for review

You could think your job is finished at this step. It is not. You need to have someone review your puzzle. Don’t explain anything about your puzzle, just give to your reviewers the puzzle and ask them to solve it. There will be remarks for sure. Or proposals for better wordings. Maybe you made a typo. Or even a mistake in the test cases.
In any case, the quality of your contribution can just increase after this process.

The tennis puzzle is obviously incomplete but you can still give your opinion on it. If the global feedback is positive, I might even take the time to really create the puzzle :)


If you’ve followed these 5 steps thoroughly, you’re ready to share your puzzle with the world and become famous. This is what happened to SamSi whose puzzle “Heart of the City” got featured as Puzzle of the Week on CodinGame. 


Here is his story:

Careful, if you have not solved his puzzle yet, there are spoilers below

“My college is in a military area, so we have sort of pine trees all over. My friend and I were just roaming around. I was wondering if there existed any sort of pattern as to how many trees are visible if we are surrounded by them and they have equal spacing.

At first I thought that there was a pattern to it, I derived an equation from it and checked it against my bruteforce solution. It failed.

Later on, I realized that this problem is nothing but counting the number of co-prime pairs so I tried to find some related algorithms and I discovered Euler totient theorem.

So I decided to create a puzzle. I did several mistakes but my friend and the CodinGame community (MadKnight, Kyran…) were very helpful and told me what could be improved.

My piece of advice for creating a nice puzzle would be the following: 

  • it should be easy to understand
  • it should be challenging
  • it’s better if it’s related to nature or things people are familiar with”
Thank you SamSi and congratulations for your puzzle!

* On CodinGame, the tests will be visible, so you’ll have to write another set of tests (validators) to prevent hard-coded solutions. These validators need to validate a code the same way tests do.
** On CodinGame, your own solution is a proof that your puzzle is valid, it is now mandatory.

2 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great article! Thank you, guys! I'm thinking about combining my puzzle with a cosmology theme, because I love everything about Universe and it will keep me motivated for my programming assignment. Though I'm not sure this - it’s better if it’s related to nature or things people are familiar with - advice will be followed if I choose this theme.

    ReplyDelete