FizzBuzz Still Works
globalnerdy.com
globalnerdy.com
Be careful with this kind of test evaluation:
1. The problem isn't new to you - you have already had the problem in your head for days or even years. Whether you admit it or not, you already have a good idea of how to approach the problem.
2. You are not under stress, as the candidate would be.
if !multiple_of_3(i) && !multiple_of_5(i)
output = i
edit: In order to avoid overwriting output, the solution should read... if !multiple_of_3(i) && !multiple_of_5(i)
output << iIf you get "close enough" on paper and pencil under the pressure of an interview, that's great. You can walk through debugging it if you want to see more, but I wouldn't hold even basic logic errors against someone.
I certainly admit that when coding on my own I usually get all kinds of stuff wrong that I fix as I write tests and iterate.
The results were quite frankly a disaster, no better than what Joey found (though I didn't do enough interviews for a statistical sample).
Unfortunately my limited observations have lead me to believe that too many developers (particularly in the enterprise environments) are used to working on legacy codebases that they hack and change until the bug they are fixing goes away. Many rarely implement features much, if at all, and the ones they do are heavily broken.
To my surprise, the candidates' solutions were either bang-on or way off. The guy who asked "Do you think I should use a loop?" had to be cut off at the ten-minute mark, and his paper had only the start of a C-syntax for loop and a lot fo scratched-out lines.
If the number is not multiple of 3 or 5, you must output the number. That's what is he doing.
He prints output at the end of each iteration, setting it to empty string at the beginning.
In each loop, he initializes the output variable to an empty string. If the current integer is not a multiple, he replaces the empty string with the integer. If it is a multiple, he doesn't know which one it is, or if it's a multiple of both, so he appends the text to the existing string variable. At the end of the loop, the string is output.
Since the `output = i` line is the only assignment that occurs when that condition (not a multiple of either) is met, it makes sense to just reassign the variable rather than append to it. It might be less flexible, sure, but flexibility isn't required here.
"" << 0x41 # => "A"Admittedly, having a variable that contains either an integer or a string is atrocious programming style, but some people do actually like languages that allow that, so it is par for the course in the given situation.
No. Though output << i.to_s will produce the same result, his code is correct. And yours isn't if you do output << i
1. If not a multiple of 3 or 5 or 15, output = i.
2. If a multiple of 3, output << fizz.
3. If a multiple of 5, output << buzz
4. If a multiple of 15, "fizzbuzz". Already covered in 2 and 3 owing to <<
Apparently people mess up fizzbuzz even when they know it:)
Unless they "cheat" this should give you a good idea of what they're capable of. The cheating should also show up if they cannot explain it or why they wrote it the way they did.
Additionally, they could spend some time with the team they'd be working in, pairing (or whatever) with other members and really demonstrating their abilities.
It's not really that difficult to get away from the whole "technical question at the interview" problem, and I'd wager that in the long-term it'd be better for all concerned.
I'm sympathetic to complaints about overly technical interviews (I've bombed a few in my day because of stress and/or ignorance), but as others have pointed out, this is FizzBuzz. I'm generally very bad at interview/algorithm puzzles, but I could even figure this one out pretty quickly.
The big difference between the two metaphors is that mine has direct impact on your personal life and negative consequences are gonna be seen as your fault, while yours is a company threat - fixing it will get you seen as a hero, not a disgraced villain.
Even Google lets you try again in a year.
I have to admit that I myself rarely need to use it. When I first read about FizzBuzz I tried to solve it by division and then comparing the floating point result to a rounded int result (i.e. circular way to get the remainder).
I'd be interested to see how many people pass if you simply remove the remainder operator part of it entirely. My guess is that near 100%.
FizzBuzz is still less "unfair" than a lot of the trick programming questions, riddles, or random CompSci trivia that many companies (Google, Microsoft, etc) do.
This is one of the reasons I would never even apply at a Google. I don't perform well programming wise under that level of pressure and my "random trivia" reserves are low.
But if you take a second and think about it, how the hell could these same people who fail fizzbuzz write production code in previous companies? Are you really saying that you believe that fizzbuzz is more complicated/difficult than their previous work?
As someone who's run lab experiments and who's seen a bunch of surprising results, trust me when I say that shocking results usually mean that you're running a test that you don't understand.
In this case, you're probably testing for a host of things that have nothing to do with day-to-day engineer performance.
Maybe they had a lot of supervision, or were someone that could talk a good game to management but didn't deliver, or maybe they just fiddled around with minor stuff in other folks' code.
While experience is obviously a really good thing and a useful thing, if they can't write fizzbuzz you'd have to re-evaluate that experience in the light of new knowledge.
What I'm pointing out is a fundamental problem with coding problems.
FizzBuzz is a relatively simple problem to solve, so if fizzbuzz fails as a test for coding ability, then it's fairly likely that all coding problems fail.
There's are probably better metrics (faster, more uniform, etc) that you could use to figure out how good a coder is.
For example, one thing I've noticed is that I'm fairly aware of the skill levels of everyone I've ever worked with. I think it might be interesting to see how peers rate each other (A players can tell A players, and so on)- maybe that would be the metric that's most useful. All I know is that FizzBuzz probably isn't it.
One last thing I need to add: the people who are dramatically more productive coders are not always dramatically more productive because they code faster or better. It's often because they enact/enable processes that amplify the other developers. (e.g. the developer who pushes the company to have a hackathon, or the dev who creates machine images preconfigured for deploying test and dev builds).
No no, not the average engineer, the average applicant. Huge difference.
Of the authors three ideas as to the reason for the inability of candidates to pass the test, I can perhaps understand the first two. My younger days involved copying/pasting Actionscript2 code and relying on superstition to get it working. All the while having no idea of how the fundamental constructs (like loops) even worked. What I don't understand is how someone has the confidence to apply to a job/hold down a job when their coding style is cut and paste.
His final point has me scratching my head a bit. Specialist or not, aren't loops and conditional logic kind of the fundamental tools in all languages. What would the discipline be that has you coding in a specialist niche, but not using loops or conditionals?
I dunno. I just think it's strange that they even get to the interview stage.
Another question i feel works really well in interviews is asking them to show you something they've created, personally or professionally. Its a nice open ended question, gets them talking about something they know and lets you ask many follow up questions about specific parts of the thing they've done which gives you a good feel of what they're capable of.
The point of FizzBuzz is that it's not something you "know" or need to study. It's such a trivial problem that any developer should be able to write it off the cuff.
FizzBuzz is famous, but alas it is famous amongst people who could code it without advance knowledge of it.
If that's true, it's still a good screening test. You either have heard of it but could code it even if you hadn't, so you pass, or you haven't heard of it and couldn't code it even if you had, so you fail.
For it to become common knowledge it would have to be part of a significant percentage of interview screens across the industry. Evidently that hasn't happened, and I wonder if it ever will.
The worst was one perky young woman who wrote her answer in three minutes flat, handed it back and asked, "Did I get it right?" I answered, "No...." and from that point on it became the most awkward interview ever.
Since when is it good programming practice to have functions for every little thing ? (I'm picturing myself scrolling or jumping around in an editor to figure out what exactly the call does). This is like doing hello world with a function. Or having a function to add two numbers. Yes, sometimes it's necessary for syntactical clarity but in this case it just seems like overkill.
Also why are some people on HN talking about edge cases to FizzBuzz ? There are no edge cases that I know of unless you talk about the number zero - which isn't part of the question.
EDIT: Why the downvotes? I don't see why trying to discuss what's clearly a programming related problem on HN brings you downvotes ? Interesting.
def multiple_of_3(number):
number % 3 == 0
end
to def multiple_of_3(number):
number % 7 == 0
end
Now perhaps he sees the advantage: no refactoring necessary.(* I feel I should say that I am being sarcastic. Anyone who thinks it's ok to have a function called "multiple_of_3" that checks if it is a multiple of 7 should probably be fired immediately.)
1. The function definition (def multiple_of_3)
2. The function contents (number % 3 == 0)
3. The function call(s).
If I didn't try to use functions on this, I'd just have to change a single line (or two at the most) in my program and get on with it.
Your use case is exactly what I meant when I said that breaking such a simple problem into functions actually introduces complexity.
def fizz?(i)
i % 3 == 0
end
def buzz?(i)
i % 5 == 0
end
So that he'd just need to change them once.Looking at a function call for "multiple_of_3" I would actually expect it to do more than i%3==0 since that could've been done just right there.
I'm just trying to understand if there are benefits of doing it this way and not directly. So far I've just encountered snarky remarks and no real arguments either way.
> 3.frozen?
=> false
> 3.freeze
=> 3
> 3.frozen?
=> true
Just in case, I'd better add: :)1. To make it clear to the interviewer that I "get" the problem. 2. To prevent myself from making any dumb mistakes. I'm only a half-decent coder, so I've made it a habit of putting such "Mollyguards" (http://en.wiktionary.org/wiki/molly-guard) in my code as a precaution. 3. To show a little Ruby coding style. The convention in Ruby is to favour lots of concise little routines over big-ass ones. See "Eloquent Ruby" for the guideline and Shopify's ActiveMerchant library for an example of such Ruby in actual production code.
Writing small, concise components(classes, methods, functions, modules...) is a generally a good practice in any language. However, moving i % 3 in a separate method is not a good practice. As another commenter pointed out, your names are hardly apt. It would better serve to name them fizz?(multiple_of_3) and buzz?(multiple_of_5). But even then, if all fizz? is doing is i % 3, let it be inline. True that in the future you might need additional processing for fizz?(not for this particular eg; I am talking real world), but re-factor it when you need it. It's not like if you have i%3 inline, it makes it harder to re-factor it into a fizz? method later.
Also, since you are talking ruby conventions, shouldn't your predicates be named multiple_of_3? instead of multiple_of_3?
Ah, Currying... (and how Firefox spellcheck recognizes that as a word, but not spellcheck)
* On another note, I've seen other bizarre blind spots in experienced programmers. One time, when I was learning Ruby, I went to a Ruby "hack night" in SF where a bunch of people there explained that they had been hackers for a while. The group was given a challenge to implement C-style integers (with overflow/loop-around) using Ruby classes.
The session had "mentors" who were professional Ruby programmers (mainly with Rails).
So as I was writing my class, and implementing addition, I figured I would need to input another integer object and add its value. So I needed to know how to expose a member variable, and couldn't find the doc that said how, leading me to ask a mentor.
He was stumped. I was thinking, "WTF? You program Ruby all day but don't know how to write [what I later found was called] an accessor?" I wasn't asking for anything fancy, just the plain ol'
def foo
@foo
endI don't know about you, but I sure see it as an edge case.
# This is incorrect due to edge case.
# If divisible by 3, fizz
# If divisible by 5, buzz
# If divisible by 15, fizz buzz
# Else number
1.upto(20) do |i|
if i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 15 == 0
puts "FizzBuzz"
else
puts i
end
endBut the main point of Fizzbuzz is that some people who call themselves programmers are incapable of producing a loop, some conditionals, and some print statements. Your solution had that, which is why you passed it. The point is to find the people who can't even get that part right.
Date.leap?(2000) # => true
So you might inadvertently end up testing a candidate's knowledge of their language's standard libraries, unless you forbid them from using these.I suspect that's why more artificial tasks like FizzBuzz are still popular.
Surely the latter won't be a surprise; if I answered like that I'd do it with a grin and a "Well, I could do it like this [write out the above], but I suspect you want something more like this [then write an implementation of the method]."
What I'm curious about is why so many seem so beholden to FizzBuzz in the first place to the point they assume everyone already knows what it is. The fact that none of your candidates had heard of FizzBuzz was to your advantage. What's the point of administering a test if everyone had already heard of it and knew the answer?
So, did you hire the one candidate that completed FizzBuzz and did you hire this person because they could?
I've interviewed many a "web designer" that were obviously not qualified on the level they claim. Just asking how to make text flow around an image is a simple gotcha that someone who claims to hand code HTML should know. If you're just a designer then that's fine, but don't say you can hand code HTML and not know what a float is or even have creative solution that doesn't involve a float. I've met many who couldn't come up with a reasonable answer. But it's not an immediate "sorry, you should go now" moment for me when they cannot do so. I also look into how they answer and how they handle being told they were wrong.
Something like FizzBuzz is a useful tool but I see too many give it too much weight without considering other variables involved in giving the test. Each interviewer has their own style but having a heavy hand on one or two aspects of the interview means you may be passing up a good hire.
If I had followed the "rules" that I've seen many advocate then one of the best hires I had ever seen wouldn't have walked through the door and even then he would have been told to walk right back out. Turned out he was an excellent hire that learned quickly and was excited about his work. That young man just recently started his own development company, granted its just him for now but he's got the work and chops to support it. Makes me wonder if I were a FizzBuzz or nothing kind of interviewer how things might have been different. To be honest I have no idea if he could have coded FizzBuzz, I didn't ask.
But I also understand if you are going through dozens or hundreds of resumes then you have to do something. Just don't be the "I don't hire unlucky people" guy that throws away half the resumes when starting. Too many FizzBuzz advocates get dangerously close to that kind of attitude.
Being snob about the interview question is not going to help things.
Isn't "because you need the job" snob? The interviewer could have very well said "We start with simple problems, and then we expand on it. So let's start with this fizzbuzz problem."
That would be fine with me. If it does get to the point that _everyone_ knows it, then we have to find a new question.
To be honest, I seriously doubt the knowledge of FizzBuzz will be a common thing. I was seriously coding for a year or so before I had ever heard of it. When I first saw the question I had what I felt was a responsible block of code that worked. Since then I've tweaked it to be more efficient as my skill level increased. But I've never been asked in an interview to code FizzBuzz or anything like it.
1. There was a great deal of primping and preening prior to the coding phase of the interview, and it was made clear that the phase of the interview was designed to demonstrate basic coding skills, that it's a fairly standard industry practice, and that these exercises are fairly meaningless and all the rest. There was plenty of warm up.
2. I wasn't running the interview; I lack the authority to do that. I am simply a member of the group the interviewee was interviewing for. I blurted it out because I was shocked by his response, which I felt demonstrated that he felt he was above menial coding exercises. I've never met a good engineer who would scoff at a problem because it was too simple, especially in a context where demonstrating prowess was desired.
3. The candidate, who is a "senior engineer" in his current position, also showed us some of his code after failing this part of the interview, which was a truly disgusting, thousands of lines of copy-paste crap pile. Imagine writing a large interactive site with jQuery, then remove the ability to use selectors and do all that by hand. Then imagine what you're doing also happens to be done for you by your backend if you enable a bit of configuration. That's the level of shockingly incompetent we're talking about.
4. We still offered the candidate the job, because we're fairly desperate, we were worried about losing the position if we didn't hire someone, and he seemed personable--and like he probably wouldn't screw things up worse than they already were on this particular project, which came back from being outsourced in really sorry shape.
5. The candidate refused to take the job, not because of the group, the low pay, the interview process, but because we couldn't get HR to change the job title from "Software Engineer" to "Senior Software Engineer." He actually made this clear to my bosses on the phone, that if they could just add "Senior" to the job title he would take the job, even with no other changes, and refused--angrily--when they couldn't do that.
I can understand why, with one remark taken out of context, you might relate more to the interviewee than the interviewer. But you really should cut people a little slack around here. Most applicants to programming jobs--especially outside technology hubs--are just not very good. This guy wasn't very good and seemed to know it, even though he hadn't seen Fizz Buzz before, and it hasn't stopped him from having a career in programming for decades.
In a good programming job, when given a task, it's an excellent response to say, "Why on earth would I want to do that?" (Though maybe not worded quite like that.) That way you get context, refine the requirements, and eliminate unnecessary work. If that behavior is required by the job, then you should expect it in the interview.
It's an important point. If the job involves writing FizzBuzz type code, give them that in the interview. If the job is writing kernels and databases and distributed whatevers, go ahead and hand out some hardcore computer science questions. If the job is fiddling with CSS and filling out TPS reports, be honest with yourself about that, and go ahead and hire some mediocre programmers, because anyone else is just going to get bored anyway.
It's kind of a sore point because when I interviewed for my current job, they grilled me on robust algorithms for keeping data stores synchronized. Cool, I like this stuff. I got hired. Guess what? We don't even use database transactions here. I'm serious, people consider them too complicated. I spend my time in meetings, in the bug tracker, doing code reviews, occasionally coding something less complicated than what I cut my programming teeth on 20 years ago. That's the actual job. FizzBuzz misses the point for jobs like this.
Now amazing as it is that there is anyone around who has not already heard of FizzBuzz as a job screen, I actually heard an even more amazing story in August 2012, which I shared here on Hacker News a month ago.
http://news.ycombinator.com/item?id=4603414
The story, which no one here seemed to believe the first time I related it, but which I fear is very likely true, is that the hiring supervisor who told me this story is the lead programmer for a company that uses Web back-end programming to serve up online, adaptive lessons. He has a position open for a new programmer as the company's business grows. He has advertised in the expected places like Stack Overflow and Joel on Software. During the year 2012, the company has received many resumes. Applicants who have computer science degrees, and especially the many applicants who have master's degrees and who otherwise look all right, are invited to do a phone screen in preparation for an in-person interview. During the phone screen, the applicants log into an application that allows them to type on their own PC to write code that can be seen by the hiring supervisor. The test the hiring supervisor uses is "Write a program using a for loop to print the integers from 1 to 100." Applicants are permitted to use any language they please, and the supervisor said he would even consider correct pseudocode a successful answer. He has not yet had ANY applicant, not even one, submit a flawless working program to do that. Not one. He was eager to interview an applicant who got the test MOSTLY correct, after seeing many applicants fail to do it at all.
I know the company that that hiring supervisor works for. I do not know how much its location or the fact that the software roles in that company are support roles rather than producing the company's main product may influence the desirability of the company. But somehow I think he is not lying when he says that a straightforward implementation, no tricks, of "Write a program using a for loop to print the integers from 1 to 100" is too hard for many CS graduates. That's why everyone hiring today should build a work-sample test into the hiring process.
http://news.ycombinator.com/item?id=4613543
It's not clear that higher education credentials guarantee ANY minimal level of coding skill, however elementary.
"That's great for FizzBuzz to 100, but what if we went to a million? What would you do to make it perform in... I don't know, a quarter of the current execution time?"
Even if they wrote a solution that avoided % or / it is always worth a few minute exploration into how one might optimise code that works today but one day is found to be slower than desired.
<html>
<body>
<script type="text/javascript">
function fastfizzbuzz() {
var fizz = 1, buzz = 1, fizzbuzz = 1;
for (var i = 1; i <= 100000000; i++) {
if (fizzbuzz == 15) {
//document.writeln('fizzbuzz<br />');
fizz = 0;
buzz = 0;
fizzbuzz = 0;
} else if(fizz == 3) {
//document.writeln('fizz<br />');
fizz = 0;
} else if(buzz == 5) {
//document.writeln('buzz<br />');
buzz = 0;
} else {
//document.writeln(i + '<br />');
}
fizz++;
buzz++;
fizzbuzz++;
}
}
function slowfizzbuzz() {
for (var i = 1; i <= 100000000; i++) {
if (i % 15 == 0) {
//document.writeln('FizzBuzz<br />');
} else if (i % 3 == 0) {
//document.writeln('Fizz<br />');
} else if (i % 5 == 0) {
//document.writeln('Buzz<br />');
} else {
//document.writeln(i + '<br />');
}
}
}
var startTime=new Date();
fastfizzbuzz();
var endTime=new Date();
document.writeln('Fast: ' + (endTime - startTime) + 'ms<br />');
startTime=new Date();
slowfizzbuzz();
endTime=new Date();
document.writeln('Slow: ' + (endTime - startTime) + 'ms<br />');
</script>
</body>
</html> val translation = List(3 -> "Fizz", 5 -> "Buzz")
val numbers = {
def start(from: Int): Stream[Int] =
from #:: start(from+1)
start(1)
}
val fizzbuzz = numbers.map(nr =>
translation.collect {
case (k,value) if nr % k == 0 =>
value
}
.reduceLeftOption(_+_)
.getOrElse(nr.toString))
println( fizzbuzz.take(100).mkString("\n") )The controversial bit, is we ask for Big O of their solution. There are a lot of candidates who didn't get the CS degree and struggle with this, surprisingly no one has ever said "I don't know Big O".
Generally, working code is enough to at least get you in the door for a face-to-face. Yet this alone filters out more than 50% of the resumes.
def top_four_ints_from(input_list)
working_set = []
input_list.each do |n|
working_set.push(n)
working_set.sort!.shift while working_set.count > 4
end
working_set
end
I'm pretty sure that's O(n). Constant time to insert any one value to the working set, Roughly n sorts performed but since each individual sort covers at most five elements we're still at (5 lg 5) * n -> O(n).Followup-questions may go into the direction of what if some of the assumptions are loosened, or what are the performance implications of several possible solutions, or how to do it when iterating in whatever is the most optimal manner performance-wise considering the storage implementation of the matrix (avoiding cache misses or disk seeking etc.).
I've only had a single person ever pass this test, out of 10 or so interviewees.
Another one was where a colleague of mine drew a car on a piece of paper and asked the candidate to explain how he would design a class hierarchy if one were to model a car in OOP. His answer was to have a class 'rectangle' and two classes 'circle' (referring to the box and the two wheels my colleague drew on the paper). Seeing the incredulous looks on our faces, he then proceeded to an unintelligible story of 'has-a' vs 'is-a' as it related to the point in the middle of the circles, and how a 'point' somehow 'was-a' circle except that it had room in the middle. True story.
This sounded fun, so I thought about it for a couple minutes and came up with the following in JavaScript:
var pyramatrix = function(a) {
a += 1 - (a & 1); // increment a by one if even
var b = [], // pyramid y-axis
c = a-(a>>1), // get the "center" of the pyramid (eg. 7 -> 1234321 -> 4 is the center)
d = function(x) { return Math.abs((x+1)-c) }, // calculate offset from center
e, f, // values for storing current index offset
g = ''; // initialize string for pretty output
for(var i = 0; i < a; i++) {
b[i] = []; // pyramid "x-axis" (current row)
for (var j = 0; j < a; j++) {
e = d(i); // get y offset
f = d(j); // get x offset
b[i][j] = c - (e > f ? e : f); // set current index to center minus larger offset
g += '['+b[i][j]+']'; // add value to pretty output
}
g += '\n'; // add a line break after each row
}
console.log(g); // print pretty output
}
Example usage and output: pyramatrix(7)
[1][1][1][1][1][1][1]
[1][2][2][2][2][2][1]
[1][2][3][3][3][2][1]
[1][2][3][4][3][2][1]
[1][2][3][3][3][2][1]
[1][2][2][2][2][2][1]
[1][1][1][1][1][1][1]
I was pretty satisfied when it worked exactly as intended on the first try!EDIT: Now that I look at it, you could obviously move e = d(i); outside the second for loop, but for the sake of posterity I'm not going to change the solution from what I first came up with.
Personally I consider the 'naive' version (as in: the first, easy to manually verify version one would bang out as a prototype) to be one where a matrix is pre-allocated and each ring is filled in from the outside inwards (so first assign all 1's, then all 2's, and so on), but funnily enough nobody ever went that route, not even my colleagues who did it.
I'd consider the 'naive' version (and the first solution that popped to my mind pretty much instantly) to be where you first fill the grid with 1's, then then loop over the next level and add 1 and repeat until you're on the top. So like this for example:
function(a) {
a = a | 1; // add 1 to even numbers
var b = new Array(a),
i, y, x,
s = 0;
// initialize the array
for(y = 0; y < a; y++) {
b[y] = new Array(a);
for(x = 0; x < a; x++) {
b[y][x] = 0;
}
}
// turn it into a pyramid heightmap
for(i = a; i > 0; i--) {
for(y = s; y < i; y++) {
for(x = s; x < i; x++) {
b[y][x]++;
}
}
s++;
}
return b;
}
Though in JavaScript it's a bit more complex than it might otherwise be since you can't just declare a multi-dimensional int array in a single line. Anyway, I discarded this solution about as fast as I came up with it because I knew there'd be more clever ways to go about it, and came up with the offset calculation method a couple minutes after that. And amusingly enough I had to actually test and iterate this 'naive' version a bit before I got it running right, whereas my 'complex' solution worked on the first try. Funny how that goes.I did google "2d array python" when the array initialization I used at first didn't work. (I'm new to python and had not done a 2d array in it yet.)
Works fine, dumps the result in formatted output, and accepts any positive number as an argument to pyramid size, odd or even.
Did that person get the job? Lots of companies seem to administer those brain-bending tests and then does not use it as the deciding factor when hiring someone.
I've personally been exposed to several quizzies like checking if a number is a prime, walking a directory tree and printing .txt files, implementing a variant of the soundex algorithm, a web server to interact with the twitter api... The worst (or best) was one in which the interviewer asked me to solve a Roman Square problem as a homework. Except instead of a 3x3 square it was a hexagon containing 64 numbers. Aced all the tests, even the Roman Square and the interviewer even told me I was the only one they had interviewed that solved the problem. It was freakishly hard and involved creating a pretty complicated constraint solver.
I still didn't get the job. In fact, the common theme in my professional life, after dozens of interviews, is that if there is a programming test involved then I will not be offered the job, no matter how well I score.
My conclusion is that companies do not use tests to determine whether to hire applicants or not. Interviewers make up their decisions regardless of test results and then use them purely to make their decision seem more objective they in reality are: "Bill scored low on or test, he is not a good candidate..." or "Bill did score well, BUT $blablabla" where $blablabla is some irrational stuff.
1. Turn it into bisect: return the index at which the searched value should be inserted to maintain the sorted list.
2. Allow any type of array member and require a comparison function provided that returns the standard -1,0,1.
3. Point out that binary search can be used for membership testing with O(log(n)) comparisons where n is array length. Then ask for a data-structure that can perform membership testing in constant time (answer: hash table.)
EDIT: the actual programming was in a shared buffer (something like etherpad). I was mock-interviewing a friend and I had him do it on paper and that worked alright too.
EDIT2: The hardest part about discussing FizzBuzz is avoiding derailing the discussion into FizzBuzz code golf.
#!/usr/bin/env python
for i in range(1,101):
print [i,"Fizz","Buzz","FizzBuzz"][(0==i%3)+2*(0==i%5)] #!/usr/bin/env python
rules = {3:'fizz, 5:'buzz'}
for n in xrange(1, 101):
print ''.join(n%x is 0 and rules[x] or '' for x in rules) or nWell, since we're going there... have some JavaScript again!
for(var i=0;i++<100;console.log(((i%3?'':'Fizz')+(i%5?'':'Buzz'))||i));
In the spirit of http://140byt.es I also condensed my pyramatrix function (though without the visualization) to 135 characters: function(a){a=a|1;var b=[],c=a>>1,d=Math.abs,e=0,f;for(;e<a;e++)for(b[e]=[],f=0;f<a;f++)b[e][f]=1+c-d(d(c-e)>d(c-f)?c-e:c-f);;return b}
And to get the same visualization as in the original version, you can use this helper function: function(a){var b=a.length,c='',d=0,e;for(;d<b;d++){for(e=0;e<b;)c+='['+a[d][e++]+']';c+='\n'}console.log(c)}
Assign those to say, pyramatrix and visualize and you can call visualize(pyramatrix(n)) and get the same pretty output!void main(){for(int i=1;i<=100;i++)printf(i%3&&i%5?"%d\n":"%szz\n",i%3?(i%5?i:"Bu"):i%5?"Fi":"FizzBu");}
;)
I do not object to the broad points being made here but I do object to this interpretation. It's beyond awkward for many good valuable programmers, stifling really, to do this kind of unrealistic audition programming for a live audience. You are not just under stress -- you are far, far, far outside of any demonstration of your true value to the employer.
Perhaps the hiring pool really is that bad, you have a giant pile of unqualified applicants, and you really must use this kind of blunt instrument rather than taking the time to talk with candidates and their references and really get to know their work and their reputations. I've never had to do hiring and I don't envy you if you do. But I've also never in my life had to do this kind of test, and I think I'd use it as a filter myself to avoid working for someone who uses it.
[EDIT: I assumed it would be clear that I'm not talking about the coding challenge of FizzBuzz. I'm talking about the experience of audition-style programming. You end up filtering on less important characteristics of applicants who might otherwise be your best candidates, but think you're filtering on FizzBuzz.]
It's a for-loop, some conditionals, and printing. If you can't produce this in a few minutes, you can not program.
The point isn't to make a perfect, working fizzbuzz in the first try, the point is to find out if your applicant can program at all in the first place.
Ridiculously enough, there are people that can't program that would pass a normal interview with flying colours, but who utterly fail at fizzbuzz, which makes it a very valuable tool.
And it sounds like the OP did it in an ok way, and didn't scare away any great programmers, which makes his conclusion pretty valid, that 60% of his applicants simply weren't capable.
If folks can't get past that quickly and easily, do you really want them around?
The biggest issue I see with this approach is the “On Paper” (white board applies as well) code. This is an unrealistic expectation. Unless you’re writing all your code on paper and this is a basic skill for your process, you've already added a factor that may push a normally fine developer out.
If language is unimportant, then process should be as well. Let the developer solve the problem in the best way they can, even if they do ‘Google’ it. You can use the amount of ‘googleing’ as another factor to evaluate.
I don't want to hire somebody that can only "ship" with google.
'FizzBuzz without Google' doesn't mean the candidate is expected to have memorized everything. Just a very minimal baseline.
You can always test Google-skills separately, if you think it is in doubt.
If you allow people to google it, you allow copy-paste-coders to pass the test, and you don't want that.
Solving it requires a loop, some conditionals, and some print statements. If you can't write that from scratch, then you can't program. At all.
Also, writing code or pseudo-code on a whiteboard is something we do pretty often at work, I don't think it's an unrealistic demand that your coworkers should be able to join in on such a discussion.
If you were being handed a high fastball over the plate, would you let the interviewer know it was a high fastball over the plate?
int x=5 % 3
and asking "What is the value of x?" Because a lot of jobs are CRUD and that form of programming rarely involves the modulus operator.
x is an unused variable in the function "int" you're defining. This function returns the ratio (as a ratio, not a float) 5 / 3 for any argument, and is equivalent to "int = const (5 % 3)", which you would be more likely to see (probably inlined).
Immediately end business relationships with that recruiting firm and start doing phone interviews yourself. Someone failing FizzBuzz during an in-person interview is also a red-flag on your hiring process.
This person was definitely not acting in their client's best interests.