# Code Hunt : Perfect score means not-so-perfect coding

With a recent question I went to Code Hunt and played there a little bit.

At one moment I get this result as question :

n     Expected Result
1    0
2    1
3    5
4    14
5    30
6    55
7    91
8    140
9    204


First solution :

public class Program {
public static int Puzzle(int n) {
for (int counter = 0;counter<n;counter++) {
result += counter*counter;
}
return result;
}
}


Result : Not the best result (2 stars).

Second solution to make it shorter was recursion :

public class Program {
public static int Puzzle(int n) {
if (n<=0)return 0;
return Puzzle(n-1) + (n-1)*(n-1);
}


However I do not use braces for the if to reduce the number of lines I still didn't have 3 stars.

Final solution : a one liner.

    public class Program {
public static int Puzzle(int n) {
return n<=0?0:Puzzle(n-1) + (n-1)*(n-1);
}
}


It works. Got 3 stars for it and I'm not proud of that code.
I'm almost sure that it compiles the same bytecode.
So they do just score the solutions based on the number of lines of code ignoring the quality of the code?

What if a Code Review question asks for advice about how to achieve 3 stars?

Do we help them create bad code just to get them their 3 stars, or stick we to our principles and write good readable and efficient code?

• They called it Code Hunt because Code Golf was already taken! – Mathieu Guindon Jun 25 '14 at 16:55

NOTE: Wearing my 'I am a Java Programmer' Hat (not my mod hat)

Having spent some time recently on Code Hunt, I would actively recommend that people avoid it:

Stay Away! This will teach you the wrong things!

Code Hunt is unlike other 'challenge' sites I am familiar with, in the sense that it is more about identifying the problem, rather than identifying the solution.

It gives you a 'truth table': the provided inputs, and the expected outputs. Your job is to convert the inputs, to the outputs. To do that, you have to build a 'black box' of code that does the transformation. If your black box meets some criteria, it scores highly.

After the first few (tutorial) 'challenges', you need to 'hunt' for what to put in the black box. If your 'guess' is wrong, you get some additional information (a new input/output line on the truth-table). You need to identify what the 'algorithm' is to do the transformation.

It is a 'trial and error' system. It encourages you to try things without basing your attempts on a problem statement. You have to guess the problem before you can guess the code.

In addition to the above, the scoring system appears to favour bad code-practices. Slow and inefficient 1-liners have been shown to score high, whereas fast and efficient solutions (10 times faster) have scored poorly, just because they took 3 lines.

So, you have to:

1. guess the problem
2. guess the solution
3. guess what ugly mess you have to make so that your efficient/readable code can be transformed in to golfed crud, in order to score high...

In all, while I think many code-challenge sites are good, the Code Hack site is a bad concept.

Also, it should be noted that it is likely a recruitment tool for Microsoft Research.... and, it worries me (or is it 'the penny dropped' AHA!) that Microsoft Research values obscure, slow, 1-liner solutions to problems they 'invent' and 'solve' with inefficient code.

Now, wearing my Mod hat, (and the Java hat)

Simon accused me of not answering the question: how we should review Code Hunt related questions?

Can you describe the problem you have already solved? Does the code work?

• for Code hunt, this means you need to have determined what the algorithm is, and probably got at least 1 out of 3 already
• you have code that produces the right results.
• you have described the problem, not just "given the inputs a, b, and c, produce the values x, y and z")

Is the question about: Do I want the code to be good code, (i.e. not code-golfing, obfuscation, or similar)

• I want the code to be good code, not just code that scores 3 out of 3.

If the answer is yes, then sure, the question is a good one for Code Review

• A bit of a Microsoft rant like it! – Marc-Andre Jun 24 '14 at 13:41
• You forgot (or missed) the biggest problem: they only accept C# and Java. – Jerry Coffin Jun 25 '14 at 16:53
• -1. Even though I agree, this answer doesn't say anything about how we should review Code Hunt related questions. – Simon Forsberg Jun 29 '14 at 18:40
• @SimonAndréForsberg - updated answer with mod-version. – rolfl Jun 29 '14 at 19:09
• I think you've answered the wrong question though: "Do we help them create bad code just to get them their 3 stars, or stick we to our principles and write good readable and efficient code? " – Simon Forsberg Jun 29 '14 at 19:18

The problem is more general than just Code Hunt. Given current technology, even sophisticated static code analyzers sometimes misjudge code quality.

For example: '100' is a magic number

No doubt, static analysis can be a valuable tool to warn programmers of bugs and other code quality concerns. However, there are inevitably situations where programmers need to ignore bad advice from them. The tricky decision is, when should that advice be heeded or disregarded?

That's where Code Review comes in. We are the community of human experts (I hope you're not bots!), and we can give custom advice. It would be most instructive, I think, to give both answers: one that is genuinely good, and another that passes the test for the sake of passing the test. Then, discuss the merits of each, possibly pointing out that sometimes automated tools are not to be trusted, and discussing the rationale in detail.

• I hope you're not bots! - Well this is awkward... – syb0rg Jun 22 '14 at 18:45
• @syb0rg: At least he didn't make this answer into a CAPTCHA. :P – Jamal Jun 27 '14 at 3:05

My 2 cents in this is having a piece a code that score 3 stars on Code Hunt doesn't make it good code. Code Hunt is still a game, and not an absolute reference on what is and is not a good piece of code.

From what I understand of Code Hunt is that they favorite LOC over anything else (correct me if I'm wrong), so they already start at -1 for me in term of "best-practices".

So, I would answer the question as every other questions, proposing tips and code that is "best-pratice". I would probably include a 3 stars version of the code and explaining if it would be good in a real context.

What you can do is tell them that they should look at the expected result difference for each step.

• The difference between 0 and 1 is 1.
• The difference between 1 and 5 is 4.
• The difference between 5 and 14 is 9.

And then you should tell them that there's a pattern going on here. The power of two is involved. And as the power of two is involved in the result differences, that means that the formula itself can be written using the power of three.

I don't know if Code Hunt allows Math.pow or not so: (int) x*x*x/3.0 - 0.5*x*x + x/6.0

You can also recommend them to use more spacing in their original code,

return n <= 0 ? 0 : Puzzle(n - 1) + (n - 1) * (n - 1);


And lastly, you go on a rant about why Code Hunt have decided the method should be called Puzzle instead of puzzle...

I don't know if my version will score three stars or not, but I would be surprised if it doesn't.

The point here is that good code and three stars don't have to be opposites. At least not in this case.

• yeah, the method name was first thing what noticed me when I did the puzzling. Let's say that it comes from Microsoft, and we all know them :p – chillworld Jun 22 '14 at 11:49