share
Stack OverflowFor statement in C
[+39] [7] Blaya
[2011-02-07 22:10:05]
[ c interview-questions for-loop ]
[ http://stackoverflow.com/questions/4927230] [DELETED]

Possible Duplicate: Help with C puzzle [1]

This morning i had a job interview and they gave me this problem:

You should change one character to print "*" 42 times. You can replace, add or remove ONLY one character.

Still i cant figured it, i've tried over and over again.

#include <stdio.h>
main(){
    int i,n = 42;

   for(i = 0; i < n; i--){
       printf("*");
   }
}
(3) This strikes me as an interview question that tests knowledge completely unrelated to what you'd be doing on the job. Where was this? - templatetypedef
(29) I hope for your sake they don't offer you the position!! - David Heffernan
(2) As this could apply to many languages, this is for testing your programming skills. - kohlehydrat
(25) Might be a decent question for a puzzle or some recreative activity. For a working interview, it's a horrible question. - leonbloy
(5) I don't think it's a horrible question, but it might not stress the kinds of skills that are useful at the job... - James
I haven't used C in years, and still realized that the answer probably involves adding a - flipping a < or adding a !... That said I still couldn't work it out. Seems like a fair test of my (in)ability to quickly gauge what tools are available and how to apply them within narrow constraints. - J. Winchester
(4) I'm all for tricky questions on analytical skills, but this one seems a bit too lunatic. - luis.espinal
(2) Go to xxxx David Heffernan - Blaya
(2) The correct answer is "change two characters (i-- => i++) so you have code that is a little bit more readable". If that doesn't get you the points, you don't want the job anyway. - JeremyP
(5) This really is a bad question. If anyone actually wrote code as obfuscated as this in a code review I'd beat them within an inch of their life. - Josh Kodroff
Voted to close, not a real problem/question. - sixlettervariables
I would never have suspected that there would be three possible solutions (not counting @pmg's awesome smartass answer :) for such a simple-looking problem. - sarnold
[+138] [2011-02-07 22:13:55] templatetypedef

One option would be to change the < to a +:

#include <stdio.h>
main(){
    int i,n = 42;

   for(i = 0; i + n; i--){
       printf("*");
   }
}

The reason this works is that C treats nonzero values as "true" and zero values as "false." The value of i + n starts at 42 and then counts down 41, 40, 39, ..., 1, 0. When it hits zero, it must mean that i == -n, at which point you're done.


(25) +1 for being even weirder than -i or n-- - Henk Holterman
(4) Super cool solution. ++. - Justin Morgan
(1) really a n1! ++ - fakemustache
That's brilliant. - Maxpm
1
[+71] [2011-02-07 22:12:58] Lucas [ACCEPTED]
#include <stdio.h>
main(){
    int i,n = 42;

   for(i = 0; -i < n; i--){    //added a minus here
       printf("*");
   }
}

But to be honest, I think that this is a silly interview question.


Maybe I'm misunderstanding, but aren't you making 0 negative there? How does that help? - user535617
(7) @user### but it's also making -1 and -42 positive - Henk Holterman
(2) @user535617 You can think of it this way: for( i = 0; i < n; i++ ) would work, so it should still work if we negate i everywhere -- but we have to negate both sides of the assignment rather than assign to -i. So, i = 0 -> -i = 0 -> i = -0 -> i = 0, i++ -> i = i + 1 -> -i = -i + 1 -> i = i - 1 -> i--, and i < n -> -i < n. - Jim Balter
I think the reasoning behind my "so it should still work if we negate i everywhere" is unclear. What I actually mean is to replace i with the equivalent -minusi everywhere, and then rename minusi to i. The result is computationally equivalent to the original; it's a mathematical refactoring. - Jim Balter
2
[+68] [2011-02-07 22:13:07] James
for(i = 0; i < n; n--){        
    printf("*");    
}

Looks like you change the i-- to n--.

Seems like a strange interview question. What was it for, if you don't mind me asking?


(1) Just tested it, and it printed 42 times. - James
@james: yep. misread. - BlackBear
@BlackBear: No problem. Just had me second guessing my answer for a second. :) - James
(3) really don't know, since the spot was for PHP... - Blaya
@Blaya - if that's the case, they probably just ask it to take you out of your comfort zone. I wouldn't worry about not being able to answer it at the time. - James
(2) yeah it was an "exam" of 10 pages to wanswer in 30 minutes...different languages and stupid questions like add 4 features to a wheel and explain why... - Blaya
(3) This is definitely the way i would do it... what is with everyone else choosing to negate part of the test rather than decrement what you mean to be decrementing?? :P - tobyodavies
What you mean to be doing is iterate n times. You can do that by modifying either the counter i -- which is by far more common -- or decrementing the limit n. Note that doing the latter would result in a bug if n were used again later on and was expected to still be 42. - Jim Balter
3
[+10] [2011-02-08 00:35:12] pmg

What about changing the - to a +? :)

I changed the source a tiny little bit.


$ cat 4927230.c
#include <stdio.h>
int main(void) {
  int i, n = 42;
  for (i = 0; i < n; i--) {
    printf("*");
  }
  puts("");
  return 0;
}
$ tr \- \+ < 4927230.c | gcc -xc - && ./a.out
******************************************
$ 

hehe


(12) Hah. +1 for a potential solution that doesn't change any of the source at all, -1 for cheating, and +1 for the case where the interviewer doesn't know what tr does, scratches his/her head, and says "umm... you pass". Total score = +1 - Mike
4
[+10] [2011-02-07 22:14:07] Collin Hockey

How about:

for(i = 0; -i < n; i--) {
           ^

5
[+9] [2011-02-07 22:15:33] kohlehydrat

That's easy:

#include <stdio.h>
main(){
    int i,n = 42;

   for(i = 0; i < n; n--){
       printf("*");
   }
}

6
[+6] [2011-02-07 23:05:08] Jim Balter

While this is a "puzzle" rather than a programming task, I think it is actually a rather good interview question, as it tests a number of skills valuable in a programmer. If you can't solve this, then you may have a lot of trouble reasoning about complex code.

The first thing I noted is that i is initialized twice, so its initialization to 42 is irrelevant unless the second initialization is eliminated, e.g. by changing it to i + 0. The next thing I noted is that we're stuck with -- as it cannot be usefully modified by a single character. So we're decrementing a counter but using < for the test, so we need to do an inversion of some sort. -i < n does that handily, as does n-- by decrementing the limit rather than the counter. The i + n solution, which is equivalent to -i != n, is harder to come up with.


(9) I think you must be confusing some other language with C here. The line "int i,n = 42" does not initialize i, just n. At that point, i is simply declared but its value is indeterminate. - Bob Murphy
(1) Yes, you're right. I wrote C code for over 30 years but have been using Scala of late and some of the C neurons seem to have died off. - Jim Balter
Someone downrated this? Odd. - Jim Balter
And now another downrate. Some people apparently ignore the popup that urges them to provide a comment offering an improvement. - Jim Balter
And another downrate long after the post. Some people are just bad. - Jim Balter
(1) Your comments give me the sudden urge to downvote, I can't explain why... maybe it's the innate wickedness in human nature? I managed to resist the temptation this time sigh - Annan
Don't blame your own wicked nature on humanity. - Jim Balter
Another drive-by downvote by a sociopath. - Jim Balter
A counterbalancing upvote ... thanks, somebody. - Jim Balter
And more downvotes. These people are not only sociopaths, but pathetic cowards. - Jim Balter
(1) This monolog deserves all the downvotes. - atamanroman
@atamanroman Downvotes of answers is not appropriate just because of someone's opinion of the answerer's "monolog" -- your statement is completely counter to the purposes of SO. And, as I have noted, none of the downvoters, before you, bothered to offer a reason for their downvotes -- again, counter to the purposes of SO, and to the FAQ. - Jim Balter
... and there goes another one. - Jim Balter
7