- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Actually it will always return 1 because if (n=0) always returns true (successful assignment)
Admin
You know, some people might regard replying to a post that says
with - especially when they would have had to explicitly delete the aforementioned snippet - as callow and intellectually dishonest...Admin
Just yesterday I had someone in for a c# developer position. I gave them a 9 question aptitude test. Question 9 was:
Write a c# function that returns true if the input is zero or greater than 5.
They answered:
int a; if(a=0 or >5) { return(true); } else { return(false); }Admin
Ooh captcha == burned, how appropriate
Admin
Nice infinite loop.
Should be: else return n * factorial(n-1)
Admin
It is about -1.081230492.
Admin
Lemme guess, an H1B candidate here because he is "more qualified" (where "qualified" is a function inversely proportional to salary requirements) than any American.
Seriously, I see that all the time - more from H1Bs, but I also taught CS at a state university and it is not uncommon for students of certain ethnic backgrounds to work as a team when doing assignments. The net result is that 1 student who worked by himself and followed the rules is worth n foreign students working in a team - which is why you have to pay him n times as much. If you want to duplicate his knowledge, you have to hire all n foreign students because they all only know 1/nth as much as the American.
On a related note - be sure to contact you congressman to oppose the new immigration bill that doubles the number of available H1B visas.
Admin
The statment "n=0" is just like any other assignment statement, and evaluates to true. (assuming this IS c++)
It will never get to the else, or the part about the universe having lost all sanity.
CAPTCHA: kungfu!!
Admin
This is a poor solution that falls under a local minimum, -1.081230492 corresponds to the global minimum.
Admin
h=0 returns false.
int factorial(unsigned int n) { if (n<2) return 1;
return (n *factorial(n-1)); } //took me less than 20 seconds to write. craaazy
Admin
Wow! Just, oh wow! Why do people post condescending remarks, when they themselves don't have a clue? Assignments in C and C++ return the result of the assignment, not true. Try it and blush:
int main(int argc, char* argv[]) { int n; if (n = 0) printf("n=0\n"); if (n = 1) printf("n=1\n"); return 0; }
Admin
When I read this I laughed. We must be interviewing the same people. My most recent favorite interview of a "Senior Java Programmer" went like this:
Me: What is the base class of all other classes in java? Interviewee: I don't know. Me: It's called "Object" Interviewee: Oh, I've never seen that.
The rest of the interview wasn't much better.
Admin
Let me guess, you decided not to hire him as a senior programmer, but two weeks later he was introduced to you as the new Director of IT.
Admin
in scheme r5rs
Admin
Don't know what C++ you're using, but in my compiler assigment evaluates to the value of the thing being assigned. This is frequently abused as such:
int a, b, c; a = b = c = 42;
Admin
No, it won't. The program will use the value of n as input for the if, and it happens to be 0, and will evaluate to false.
Admin
Well, if they replaced "object" with "thingy", they might be dev material, but with communication skill issues. If they replaced "object" with "instance", that's a little better.
Admin
Actually, that code was totally wrong. This function actually works...
int factorial(int n) { if(n == 0) { return 1; } return n * factorial(n-1) }
Admin
Admin
Unfortunately for n = 0, while(--n) will result in an infinite loop.
Admin
The python version:
def factorial(n) : return (1 if n <= 1 else reduce (lambda x, y : x * y, range(1, n + 1)))
Admin
Will always return 0. The 'if' sets n to 0 and the compiler can then optimize the code and remove the recursive call completely and just return 0.
CAPTCHA: sanitarium <-- perfect
Admin
My operating systems course started with 60 people. Like 20 made it to the end of the year and 12 actually passed. All to learn a bunch of IBM370 we'll never used again.
Pretty much every CS course starts with twice as many people than it ended with.
Admin
le sigh
It matters not what the value of n was when it was passed in, it is assigned the value of 0.
In C... x = y = 6; What is the value of x? It is 6 as they are all assignments and the assignment returns the value assigned rather then a flag indicating success.
In VB x = y = 6 Only the first = is an assignment the second is a equality operation. So if y was 6 it returns true, otherwise false.
Following this: if (n=0) return 1; N is assigned 0 and returns the results of that assignment which is 0. 0 is false so it hits the else and recurses forever.
Admin
I love it when a high school student blows the stack. WTF
Admin
The education I did receive (only stayed a year for a diploma, no degree) we started with 35-40 students, only 7 graduated. So that is 20% or less that actually finished. Most of the drop outs were withing the first three courses.
Admin
Usually happens right after stacking the blow. Man I hate it when that happens.
Admin
should it be:
public int factorial(int n) { int a = 1;
}
or maybe
public int factorial(int n) { if(n>1) return n * factorial(n-1); }
In guess the graduate student wasn't THAT far off... i mean, he had the function name, variable AND a for loop :P
Admin
That's only in C. In C++, there's no need to test for assignment because it will throw an exception if it fails.
(snicker)
Admin
That's only in C -- C++ will throw an exception if assignment fails.
(drat, that would have been a whole lot funnier if I'd hit quote instead of reply)
Admin
C++
int factorial(int n){(n==0?return 1:return n*factorial(n-1))};
Admin
I hate code like this much more than the readable-but-wrong kind...
Admin
pfff, that's not C++. THIS is C++:
Admin
I would also like to use this question to weed out people who can't communicate well, but I will need help evaluating their answers. Could you please give an example of an explanation of this concept in simpler terms.
Thx in adv.
-Harrow.
Admin
Admin
Templates gah! My mind esplode!
Crap, I misplaced my semicolon. Oh well. Thank God I already have a job.
Admin
I guess to me this goes to prove how easy it is to screw up, if you dont test your own code.
oh buttons, I susspect you meant i<=n
Admin
Sonofa...!
Let me try again: int factorial(int n){n=abs(n);(n==0?return 1:return n*factorial(n-1));}
Not sure how to handle negatives.
Admin
But he did communicate simply within the context of the participants. If you can't understand a tech explaining this to a tech, why are you running interviews? If you want him to explain it in "simple terms" ask him how he would explain it to his mother.
Trying to evaluate to many factors in one question is a failure of the interviewer, not the interviewee.
Admin
Yeah me too, but its fun to write it.
Admin
heh, geek.
Admin
Umm... I think that ought to be a else return n * factorial n-1
AM
Admin
Admin
Actually, that code was totally wrong. This function actually works...
int factorial(int n) { if(n == 0) { return 1; } return n * factorial(n-1) }
Admin
Try again. That won't compile.
int factorial(int n) { return n == 0 ? 1 : n * factorial(n - 1); }
Ignoring negative input issues.
Admin
Oh god. I'm not even a programmer and that's horrible!
Admin
There are not an infinite number of negative integers, therefore n will eventually wrap around and become positive, which will eventually count down to 0.
Admin
I can't believe none of you can do factorial in O(1). Before you argue pedantry, none of the other implementations in C/C++ could exceed the values of their defined return types, either.
Admin
I cannot explain it in terms you understand, because no one understands it in terms you understand.
Admin
Any employer who asks me this would regret it: