VC++6 compiler was a lot faster (to fail on wrong code) - but still took 30 minutes on 1GHz machine. Result is that there are indeed only 3 solutions.
Code:
- at 24:
int i, n = 20;
for (i=0;-i<n; i--)
{
cout << "x" << endl;
}
+ at 26:
int i, n = 20;
for (i=0; i+n; i--)
{
cout << "x" << endl;
}
n at 30:
int i, n = 20;
for (i=0; i<n; n--)
{
cout << "x" << endl;
}
Last edited by RoboTact; October 18th, 2006 at 06:53 PM.
"Programs must be written for people to read, and only incidentally for machines to execute."
I got all three of them in a few minutes, because I thought about the loop terminating with an answer that is not Boolean.
I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done. I highly recommend his article:
Finally, avoid brain teaser questions like the one where you have to arrange 6 equal length matches to make exactly 4 identical perfect triangles. If it's an "aha!" question, you don't get any information about "smart/get things done" by figuring out if they happen to make the mental leap or not.
A great read. The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...
I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done.
The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...
I finally got all three! I'll admit that it helped when the details of the problem were explained... as it hinted at where I should look for the third solution (which was -i<n for me).
Last edited by Matthew Doucette; October 20th, 2006 at 07:54 PM.
Took about 5 minutes. I never would have thought of n+1. More than 3 solutions, I guess. : )
If n is -20, i=0 will never be less than n.
Interestingly, since I didn’t want to read past the first few posts, I did not realize that I played this game five years ago, and accidentally found (again) all three solutions. Again without timing; about 10 minutes, I guess.
Do I get a double bonus?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
Bookmarks