- 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
Admin
A programmer's job is to understand a problem, and then explain it to a machine in a language the machine understands. Why do so many nitwits think it is unreasonable for an interviewer to try to gauge how an applicant goes about the first (and most important) part of his job, trying to understand a problem? If the interviewee did not realize the problem was about prime numbers, then he is most definitely worth less than someone who did. I say that not because I assume the programmer will be using prime numbers in his job, but because I know an educated person is better at understanding problems than an uneducated person. He has seen more types of problems, and he has seen more nuanced problems. And, if you are innumerate, you are just as uneducated as the illiterate.
Admin
TRWTF is asking for 'ten years experience' in a language that's only been around for thirteen years and has changed a lot.
Sadly, the reason for this has been mentioned - HR write the ads, and HR don't know about what the company does. It's like the '5 years experience in windows 95' ad posted in 1998.
Admin
Well, anybody who can't think of a better solution is not somebody I'd want to work with either.
Just imagine having to maintain that person's code shudders
Captcha: dolor
Admin
From the original sidebar (that people haven't been reading, it seems)
So, yes, the interviewer DID prod him for a more efficient answer. Yes, the job involved a LOT of math, so basic high school math principles should be known. One of the first algorithms we're taught in college. It's the definitive "Recursion" algorithm, that really doesn't require much math skill as just a basic understanding of algorithm design.Admin
Thanks. Should have spotted the step 20. Though I would submit that reducing the solution set ahead of time is fair game. With a small enough solution set, the loop is almost irrelevant. (not fair. Quoting only part of your post. But wanted to thank you for the step - I missed that in my search for a solution).
Admin
YOU WIN!
Admin
Admin
YOU WIN!
Admin
The Real WTF is that you guys think this is a solution he would use in a real program. A programming question in an interview is to see if you come up with a solution to a problem (engineering). The solution doesn't have to be the most optimal. It just has to work! Also remember in an interview the only resource you have is your brain, and his solution works so he passed. Only a retarded Noob would think otherwise.
Admin
Actually, the right side of MY screen points southwest.
Admin
Admin
I'm intrigued by the concept that Fibonacci is regarded as the definitive recursion algorithm though, given that it's actually as efficiently solved using a static loop. Factorial is the definitive recursion algorithm, surely...
Admin
No LCM or GCD and runs under 1 ms. :-)
Admin
Admin
Hah. I've actually been teaching myself Haskell by going through Project Euler while at work. As a PHP developer.
Admin
Simple according to who? You only think it's simple because you ALREADY KNOW how to solve it. Of course the solution is simple; finding the solution is the hard part. And without adequate domain knowledge, a person can't be expected to know how to solve the problem without a reference.
So unless the interview was for a position that required knowledge and experience solving math problems, then it WAS a distraction.
A "basic grasp" of mathematics is fairly ambiguous. A ten year old has a basic grasp of arithmetic, but does that mean he/she should be able to solve this problem? What's the magic math skill level needed to develop software? And who determines what that is?
Creativity can't be called on demand. If your developers are under pressure to perform, then they're not producing their very best work. Similarly, if you want to fully realize a candidate's skills and the benefit they can provide your company, then you'll make them comfortable during the interview.
As interviewer you have to ask yourself one question: am I interested in learning what the candidate can offer my company OR can I stuff this warm body into the open position?
If the interviewer in the article chose the former answer, then I would argue that he failed to capture what he was looking for in the candidate.
Admin
The real WTF is that they expected him to write a PROGRAM to compute the solution!
It took me less than 2 minutes with a 4-function calculator to figure out that the solution is 232792560...
A candidate that understands the solution would be left without any code to write, other than multiplying a few constants if they don't have a 4-function calculator available.
A candidate that doesn't understand the solution wouldn't be able to write a good algorithm for this problem anyway.
Seriously folks...think these things through before putting them on an interview...
Admin
Same basic solution...
Admin
Just read the original sidebar - and comments. Pleased to see that it devolved into just as much of a flamewar over there as it did over here ;^)
Admin
I love math, but I haven't seen math like this in years. In ["community"/"technical"] college, we just had basic math that was mostly computer oriented (binary and hexadecimal numbering systems, etc.), and I don't think there was anything like this in grade 12 calculus so perhaps grade 11... Which was like 5 years ago. I would have had to ask the interviewer what "evenly divisible" meant and hopefully could have come up with a solution like the following (which is really just a dynamic adaption of the original solution):
Tested with ranges 1 to 10 and 1 to 20. Compiled using MinGW and tested on Vista. Not guaranteed to work. :P I couldn't even think of an acceptably short/descriptive function name at first... I think I originally picked smallest_number_evenly_divisible_by_range. :P So I agree that either this question shouldn't be asked unless this kind of math is required at the job OR the interviewer should be willing to answer any questions the interviewee has about the question. Which is something a lot of people don't seem to realize about interviews... It's OK to ask about stuff like this (unless you're expected to know it for a job that requires it). Especially when the real purpose of the question is to see how you convert the solution to code (or possibly how you go about solving a problem)... You should at least be able to ask the right questions to find out how to solve problems.
Admin
So you think that dividing a number into another number and seeing if it has a remainder isn't simple? What is simple? 1+1=2?
I was solving problems like this (for a bit of fun) when I was 8. And that isn't the point, the guy KNEW how to do it he just couldn't translate the mental model he had of the solution into a decent piece of code. That's what a developer does, that's ALL a developer does. He KNEW what he had to do, he just did it in a very bad way.
This isn't about creativity, it's about knowing how to write good code. Good code is a lot more than just "being functional".
Look at some of the answers given in the comments, they are all roughly similar, and ALL are more elegant, more maintainable and more flexible than the WTF answer. There's no creativity involved, just an ability to LOOK at some code and think "something's not right here". If you can't spot the unnecessary repetition of the original code then there's really no hope for you and you should stick to Excel and/or Etch-a-sketch.
I don't want people to be too comfortable in an interview, I have deadlines, people demanding things, a degree of pressure to meet deadlines, etc - I don't have the bandwidth to babysit new developers, I don't have the patience to pair-program with someone who is prepared to submit that for inspection/code-review. So a degree of time pressure in a technical interview is essential.
As far as what I'm trying to find out, there's only one thing; Can they write good code? I leave the warm body thing to HR, and what they can offer the company to my boss, I want to know if they can code, everything else is a secondary consideration and why there is usually MORE than just a technical interview.
Admin
You have to know when to go nuts on a complex solution. I see people spending 10 minutes writing a GCD function. Who cares, its a silly interview question. 5 line java program
Ran on my machine in 7.2 seconds. Wrote in 1 minute. Why spend 15 minutes writing a run 1 time program that may save 6 seconds on runtime?
Admin
Opps I used & instead of &&
Switching to && brought run time down to 1.5 seconds.
Seriously.. 1.5 seconds to solve a simple problem, works for me.
Admin
Now solve it for 1 to 700.
Admin
I don't interview people for the position of pocket calculator. I interview people who claim to be programmers. I expect them to demonstrate their best programming.
Avoiding premature optimisation really entails considering the cost versus a-priori benefit of improving an algorithm. If you consider the problem before writing the brute force algorithm it's a clear win, which is why in an interview with 1-on-1 handholding, I would expect candidates to arrive at the GCD method.
Put another way, I don't want to have to rewrite my employees' sloppy algorithms just because they couldn't be bothered to think a problem through (or look it up), and the interview is my opportunity to ensure I won't.
Admin
Just out of curiosity, if they arrived at the prime solution, would you expect them to create an IsPrime() function on the spot?
Admin
This is an interview question, not a app I am shipping to a customer.
Admin
Admin
I'm not surprised by that. You project your own skills and interests on others, and then wonder why they fall short of your expectations. It's great that you love math, but not everyone does. And thinking that every decent developer should have a basic understanding of "simple" math is unrealistic.
You're right. Several solutions posted in the comments show that it wasn't the most elegant solution. I'm not defending the code so much as pointing out the absurdity of the question.
Software developers don't "write code", they solve problems using computers. It's really easy to judge someone for not being able to solve a "simple" math problem because the solution is already defined. As software developers, it is our job to solve problems that don't necessarily have solutions yet. And THAT requires creativity.
You're reminding me of the old saying "penny-wise, dollar-foolish". You look at it as babysitting new developers when you should be thinking of the return on your investment. I'm not suggesting that you hire any idiot that applies. But don't be surprised if you can't find a unicorn in your sock drawer using a metal detector.
TRWTF is that you want a code monkey and not a software developer.
Admin
Thank you, you have just simply made the point for all of us who understand why this can be a useful interview question.
Admin
I don't project, I was just stating that if it's possible for an under 10 to grasp the concept it shouldn't be beyond the wit of a 25+ year old programmer. My point HAS already been proved because the original code uses % which means that he has grasped the concept, he just didn't implement it in a very good way. And the implementation is what we are after, not the maths.
And, no, it's not unrealistic at all. I've been hiring and firing developers for over 10 years and each and every one I have hired has been able to understand the concept of division.
No, I want both, and I can get both, and I have got both before so I demand both again. Probably why I will look at an average of 100 CVs, interview about 10 people, and hire just one. I want a lot (and pay a lot) so I demand a lot. I also want people that I can learn from, who share my enthusiasm for solving problems, talking about design patterns, arguing/discussing best solutions, etc... I don't want a "yes" man, neither do I want a plank.
I want creativity too, but within the bounds of good coding and design practice.
If you're happy to hire a developer/coder who churns out rubbish like that then go ahead, fewer CVs for me to reject and interviews to terminate early...
I think it's "penny wise, pound foolish", "cent wise, dollar foolish" just sounds wrong.
:)
Admin
Sure, it's a suitable programming test.
Of course it's O(huge), but it works.
Admin
Yes, but in a normal situation, you'd have the context of the problem, and be able to make that call. In this situation, is it a constant you just need to whip up, or is it a function that will be used multiple times with multiple parameters? Without context, it's impossible to know. If it's the former, then the no-thinking-brute-force approach is the appropriate one, in my opinion.
Admin
Come on guys, there are new articles to flame each other about now.
Admin
There you go, punks. The factoring could be made more efficient, but it runs much better than the brute force crap. Runs for n=1000 in under a second. (result has 433 digits btw)
You're welcome.
Admin
Code monkey? This example is an extremely badly written piece of code that does exactly what the customer (interviewer) asked for. That is what a code monkey does.
A software developer can decipher requirements (“hey, this is a maths based firm… maybe they would like it to work for numbers other than 20”), sk questions about things that are still unclear (“Is this a once off program? If it is, don’t you think it would be better to just use a calculator”), and then write an easily maintainable, readable, program that doesn’t take forever to execute.
Admin
Wanna get horrible? Cut out the fat.
Admin
True,
But as many, many people have pointed out. You're not testing coding ability here. Your testing mathematical ability. And interviews are artifically stressful environments. So dont be upset if the the guy produces the dumbest thing that works... worry if he produces and overcomplex thing that doesnt work.
I really really wish more employers (mine included) would do some research on interview situations. I've seen the wrong people hired and the wrong people sent home often because the interviewer doesnt understand how to interview someone to find out what they know.
An interview can be as frightening to a interviewee as dangling them off a cliff, upside down and setting fire to the rope. Would you seriously expect to hand someone in that situation a pen and paper and say.. Now solve this prime number test for smart people we found off the web. Oh and by the way if you dont we're not gonna blow the fire out.
Stephen
Admin
Admin
Then you call the db admin and complain about why the db is so slow.
Admin
continue? that is sort of like using goto:
Why not just use if ( num != 1 ) instead? it does the check each loop anyway...
Admin
Just a quick question:
If someone told you to somehow dig a 1m wide and 0.5m deep hole, would you hire this machine to do the digging because the requirement might change to a 1km wide and 500m deep? Or just a man with a shovel?
Admin
The post gives the really naive solution, but applying lcm iteratively is still sortof naive. You can get a better asymptotic complexity, and a simpler algorithm, by generating then using a list of primes up to 20.
Admin
Obviously wrong. It'll end up with too many factors of 2. If your loop ran 2 to max, then it'd work.
Admin
Ding ding ding!! Yes!!
For example, I can't tell you how much I dislike candidates whose first response, for almost any question, is "I would do a google search, look through MSDN KnowledgeBase, etc. and see what other people have done to solve this problem, evaluate their solutions, yadda yadda yadda..." If someone has to resort to a literature search for every frickin' coding problem that rises up and smacks 'em in the face then they're not going to be the most productive of developers, are they?
But most of all, that type of answer makes it completely impossible to judge how good a developer a candidate really is (or might be). I guess I need to be armed with a laptop and turn it over to the candidate and say, "go for it, you have 30 minutes to give me a working solution in C# code!"
(captcha: validus -- how apropos)
Admin
A brute force solution may well be best if the problem size can be bounded, and is more likely to work correctly in an off-the-cuff answer.
That said, I like math tricks too.
Admin
Awesome. It's like 9 women for 1 baby in 1 month. I salute you, sir.
Admin
Personally i think this is a great question. Who cares if they guy can write PHP, Java, C or any other language, typing out the code should be by far the simplest part of any project. I can get some kid straight out of high school to take a mathematical function and reproduce it in code (i bet i could train a monkey with enough time) I wanna know if my candidates can think about problems in an intelligent manor, breaking them down into pieces then reassembling them into a solution. If they can, chances are i can tell them to deliver it in any of a dozen languages even if they don't know them and they'll go figure out the syntax. Or, more importantly, they'll be able to determine the proper tool (language) for the job and really deliver a proper solution.
These are exactly the types of questions you ask when what you want is a conscious being as an employee, not a code monkey.
-x
Admin
Nine women for one baby in one month is doable and has a simple implementation.
Send each woman out to a public park with orders to kidnap a baby. Worst case, all nine get caught, but I'll still have 7 more months to bring the project in on time. Best case, I end up with nine babies in that month and can pick the best possible one to satisfy my client's needs.