- 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
Hmm, I would have asked whether he wanted the recursive, iterative, lookup table, or compile-time template meta-programing version.
Admin
I so would've used n.
Admin
int factorial(int i, int a=1) { if (i <= 1) return a; return factorial(i - 1, a * i); }
This one allows the compiler to do some optimization and can be as efficient as a loop.
Admin
I'm amazed at the amount of ways in which you guys have managed to break or mangle the factorial function.
Having said that, I guess I have to prove I'm not one of you:
But since this isn't Python, you could could just as well do it in O(1):Admin
OH GOD NO i hate being wrong... but yeah i feel kinda stupid now... haven't done c++ in years and apparently took a real shortcut in remembering that :(
In other news, where I work now they expect no prior language knowledge, so the entrance test was a language-irrelevant logic test called the WCOAT test. Based on the highly-confidential scores of myself and my coworkers, it is pretty accurate. (and felt very appropriate as i was taking it)
/brb killing self
Admin
When I respond to just one statement, I don't always copy the whole thing, and I don't always read the parts i'm not copying. Not everyone hits "quote" all the time - TRWTF is having a "reply without quoting" option in the first place.
Some people might regard making an accusation in the format "Some people might regard..." instead of having the balls to stand by your own words as a form of cowardice.
Admin
It evaluates to the value that was assigned. Which is 0. Which is false.
Admin
"Certain ethnic backgrounds"? Nice code words, there. And I'm sure no one from other countries will read your rant and think about how arrogant, etc Americans are.
I've taught CS and engineering at a state university also, and if you think that assignment collaboration and teamwork are restricted to "certain ethnic backgrounds", you're fooling yourself.
I'm against expanding H1-B visa limits, but your non sequitur rant makes me embarrassed to be on the same side.
Admin
I agree. I am sure there are just a many stupid Americans as there are stupid (insert favorite ethnicity here). The real problem is we Americans are supposed to be leaders in the free world. Nice example we are setting here isn't it?
Admin
The complexity of the naïve recursive definition of fib is far worse than n^2 -- it is exponential, but it drops to linear with a simple memoization scheme.
Admin
That's actually an excellent way to describe OO programming... Probably quoted from a book, but very easy to understand.. And we're glad we don't get the job. We don't want to work for anyone (i.e. you) who's too stupid to understand three-syllable words..
Admin
I hope I am not alone in wishing you would keep your political opinions to yourself (or at least air them on a suitable political site.)
As regards working as a team, teamwork is one of the most sought after properties of programmers in the business world. There are few places these days where you can sit on your own and code your own stuff - most programmers have to work together with customers/users, DBAs, server techs, testers, project managers, external service providers, not to mention other coders.
On some projects, my responsibilities have been less than 25% actual coding, and the rest of the time I have to make sure other people do their stuff. That's how you get things done.
Admin
There's no easy solution other than Newton's method using series approximations for gamma and digamma. Nor is there a most-complex-domain-converging series-like expression that I can find, like there is for the gamma function itself.
Like, you can solve for the limit of the upper and/or lower incomplete gammas through the inverse of regularized gamma. (Regularized gamma is G(a,z)/G(a) with parameters a and z) But it only solves for the 'z' parameter in G(a,z)/G(a), not the 'a' parameter. But in general you can't solve for 'a' in G(a) (imagine 'G' is the greek gamma symbol).
Admin
You do understand the difference between a heuristic and an algorithm, right? That is a heuristic and the underlying basis is a function of culture. I make no value judgements apart from every semester I have to bust up one or more of these little work groups and fail its members for the course because they all turn in the identical code, often cribbed from some book, and then mangled. Occasionally, the group in question is actually greek (as in frat) but more often, it is asian students.
Thats what this example looks like. A snippet of code that almost calculates ten factorial - modified incorrectly somehow. The student will go to the wall insisting that it calculates a factorial and cry over the great injustice.
In fact, I was once called before the dean and student advocate to defend the failings and the students' "defense" (laughably) was that the code in the programming assignment was copied directly from some text book and so must be correct. Nevermind that it was 1) not copied correctly, 2) did not fulfill the requirements of the assignment completely and 3) was plagarized.
My experience, my opinion. There are, of course, plenty of unqualified American programmers. Most of them specialize in Java.
Admin
Ordinarily, I agree with you. But if you've never written the factorial function yourself and simply had a team member work it out and then turned in his work - then you still don't know how to write a factorial function, do you?
School is not the business world. The goals are different. The point is not to get a factorial function written. The point is to impart the knowledge to each and every student how to write one. Working in teams prevents attainment of the goal.
Sheesh.
Admin
And you not only specialize in it, thats what you teach, am I correct?
ducks and covers
Seriously, if you are going to qualify your statements, don't do so by making another overly general, and just as incorrect one while doing so. It makes your qualification of statements so ineffectual.
We could easily discount anything you say if we truly believed the following: Those who can, do. Those who can't, teach.
Admin
Working in teams could accelerate that goal. If one member has a eureka moment he can share that with the rest thereby each member of that team gets the benefit of an individuals ideas. It all depends on the dynamics of the team and how dedicated they are to actually learning. The team idea isn't at fault here, it is the individuals that make up that team.
Admin
Sure, it's calling factorial(n) rather than factorial(n-1) on recursion. Is this a typo, or a real error? This high school student at least understands what recursion is.
A high school student has time to learn the pitfalls. A CS grad interviewee should know all this!
I'd test recursion with these questions: What is recursion? How do you write a factorial function with recursion? Where is the recursion in factorial? (*) Why is this recursive factorial function a bad way to do it?
Admin
Admin
Nice, this actually promotes a two way dialog. Something missing in many interviews. You interviewing any time soon?
Admin
Those who can do both - teach night school as adjunct faculty.
Admin
I've found a good similar question is "What is an abstract base class and why would I want one?"
Precious few developers can answer that question coherently.
Admin
Let me give that a shot.
An abstract class is one that adheres to a defined interface but does not provide functionality. Used as a base class it lets derived classes know they have to implement certain method and provide the functionality for those methods.
Do I win a cookie?
Admin
http://xkcd.com/c221.html
Admin
im more amazed by the apparent indignation of the posters -- as if their mere prior memorization of these terms and riddle somehow meant they were on par w/ having devised them. ridiculous.
myself, id never heard of factorials or recursives, and ive been doing enterprise coding for 8 years for a variety of fortune 100 clients. granted, i use recurisve patterns, just hadnt known its label. never heard of nor used factorials.. but then, i never took a comsci class, either.
my point? for many/most, perhaps textbook comsci lessons arent so valuable in the Real World after all...
Admin
True... a poor answer. But what a daft question to ask.
Admin
Not quite, an abstract class can provide functionality (an interface does not provide functionality).
I would say this is a good definition: An abstract class is a class which can not be directly instantiated.
This is more information than I asked for, but I would still accept it: An abstract class is a class which can not be directly instantiated. In order to instantiate an abstract class one must create a class which inherits from the abstract class and implements the abstract methods (aka pure virtual methods/functions depending on the language background of the interviewee). After that a person can instantiate a variable of the abstract type by calling the constructor of the derived type.
Admin
So here's my attempt at writing the recursive method:
public int factorial(int n){ if n == 1{ return 1; } else { return n * factorial(n-1); } }
Please be kind about the Java, I'm just learning. Gentle corrections would be most appreciated.
Admin
Not to mention he thinks cubes have 4 corners, not 8.
Admin
You know nothing. The factorial of any given (integer) number is 42. It doesn't matter if it's not positive any more either, neither if it'll fit on an 'int' variable.
Revolution!
Admin
The specification calls for a function that will find the factorial of ANY given number.
A 4-byte signed integer can't hold anything larger than 12 factorial.
Captcha: Case statement, anybody?
Admin
So-called "teamwork" is the use of group dynamics to minimize the influence of any single individual and to reduce the number of management layers. Based on the communist belief system. In the gulags, where they thought they came up with it, it was called a "work brigade", and allowed large numbers of prisoners to work outside the fence with relatively few guards. Nothing to do with working together co-operatively, although management certainly wants the employees to play nice.
Admin
Or, for bonus points:
Screw that; use a smart language:Admin
Admin
Admin
Kattman would be given additional prompting for more details.
bill gets part one right - but skips part two. "Why would I want one?"
What is the key advantage in having an abstract base class? When would you create one? Answering this correctly implies you know more than just how to ape the forms and parrot the propaganda and platitudes and demonstrates that you have some idea of why you're doing what you're doing. Also, believe it or not, the motivations for doing this are somewhat language dependent. An awareness of that is a bonus.
Admin
OK - BTW nobody did any error handling at all.
Admin
No, I don't know anyone who was ever this dumb. Certainly not me.
Admin
In a strongly typed language, this helps enforce "interchangable" polymorphism, since all subclass methods implementing an abstract method must return the same type of object.
Sounds like bondage and discipline to me. I'll stick to Ruby.
Admin
42 ?
Admin
The return value is really 12, but the loop ends because i is 13. The 'a' variable captures the value of i before it's incremented and then tested by the for loop.
Here's my code snippet...Just because I like writing fun little code snippets:
int factorial(int n) { return n ? n * factorial(n-1) : 1; }
Self nit-pick 1: Doesn't check for negative values. Self nit-pick 2: Does one more call than it needs to.
Admin
"What is an abstract base class and why would I want one?"
I'd love to know the "expert" answer to this one. If you want to specify required behavior, use an interface. An abstract base class takes away flexibility and gives you what exactly, other then a bigger object hierarchy?
Admin
How did this get so many hits?? Do you programers find this funny/tragic?
Admin
Huh? It'll infinitely recurse even if that is an assignment operator... it'll set n to 0, which evaluates to false, so it'll return 0 * factorial(0) which'll do the whole thing all over again.
Admin
Spoken like a true 'java programmer'.
There are some universal motivations. An abstract base class often embodies a concept or classification, along with a protocol that is unique to that concept. In this case it is a kind of documentation - clarifying intent and reducing the number of concepts - which provides a certain reduction in complexity for the developer at some level as he can generally ignore the details of the subclasses below. An interface also fills this role (assuming your language has interfaces).
The reason for choosing an abstract base class over an interface is primarily to share common code. If there is code that is likely to be reproduced over and over again, then it is worthwhile to move the code up the hierarchy to an abstract base class where it can be shared more conveniently. There are drawbacks to this like if your language does not support multiple inheritance and you really need to mix a couple of concepts into one class. At that point, you should have the abstract base class implement an interface and write everything in terms of that interface. You'll likely have to duplicate a bunch of code. That's life.
In statically typed langages there is an additional reason. It enables polymorphism and substitutability. (lookup Liskov Substitution Principle). Without an abstract base class, you cannot implement polymorphism. (technically, the base class need not be truly 'abstract' but it is not generally good form to derive from concrete base classes).
Admin
Assignment statements evaluate to the value of the assignment. This is why tricks like
while (c = getc() != EOF)
work. n = 0 evaluates to 0, so it is false.
Admin
Okay. Who's the spy?
I can confirm that this is a true story, because I was the one interviewing the candidate. I recognize the code, even though the story is slightly different (thanks, Alex!) I had to switch to the factorial problem, because when I asked him to convert an integer to a string using any language he wanted -- event pseudo-code! -- he said "I don't know" after writing three lines. I would also like to point out that, contrary to the story, the candidate claimed to have at least three years of professional experience. Doing what, I have no idea.
Admin
When someone asks:
and you reply with:
I'd say you could pretty much get up and walk out the interview right at that point.
Admin
Admin
const unsigned int factorial(const unsigned int n) { return n? n * factorial(n - 1): 1; }
I rather don't get it why all you C++ coders don't use the nice unsigned int types and bitch around arguing about how to handle the negative. </rant>