Tech-Interview Riddles
An anonymous submitter writes "A computer engineering student at UC Berkeley has made a comprehensive archive of riddles from technical interviews. Very challenging and loads of fun. Also useful for interview preparation."
What's up with using this type of question for interviews, anyway? Sure, they can be fun, but they're perfectly useless as far as telling whether someone can actually write solid code. 9 times out of 10, all they tell you is whether the interviewee has heard that one before.
To interviewers: Do you really think that the answers to these questions don't spread through the entire department within 15 minutes after your first interview? I realize that "knowing the answer" makes you feel smarter than the prospective employee in some sense, but how about actually doing your job for a change?
Doesn't matter, the variable will be renamed along with the others.
;)
Now I get a gold star right?
Is the answer .. nothing?
You shouldn't have to special case that since it's just another variable name.
This is a great site for more information on interviewing at Microsoft. It has some sample questions, study materials and testimonials etc.
I.O.U One Sig.
This was a nifty riddle, I never saw it before, and I do think it's appropriate for the programming mind set.
... you do nothing! You turn around and walk out! Look at it this way. All you joes are never going to turn the lamp off. Ever. That's Number's job. And each of you joes are going to turn on a dark lamp and make it bright exactly ONCE in your life. Just ONCE. No more, no less. If you can count up to 1, you can do this. Numbers has to count to 100, and I know he can handle that. And we'll all be out of here eventually."
Essentially the prisoners have to come up with some programs for themselves. They become little finite state machines with an unlimited number of internal states, one input bit, and one output bit. Then the jailer picks one prisoner at a time, randomly, and lets the prisoner run his state machine.
First I noticed that there aren't enough different truth tables for 100 prisoners, which led me to think about the state machines.
Then I looked around for some kind of protocol where prisoner #1 could signal that he had rendez-voused with the lightbulb, and then hand off a notional "token" to prisoner #2, and so on for 100 prisoners, eventually to make a token ring. That wasn't working out very well. I got up to 4 prisoners that way, and I wasn't coming up with a general mechanism.
Then I thought: okay, try client-server, make one prisoner the "boss" with one program, and 99 other prisoners are "drones" with a second program.
That worked out pretty well!
The next problem is communicating the strategy to the prisoners:
"Okay, all you mugs, listen to me, because I was a smart guy on the outside!"
"You, KILLER, you are the BOSS. It's your job to keep the count. Whenever you go in that room -- if the lamp is on, you turn it off, and you make another tally mark on your roster. If the lamp is off, you leave it off. When you get to 99 tally marks, you tell the warden that everyone's been in there, and we can all go".
"All the rest of you joes, listen up. If you go in the room, and the lamp is off, you turn it on -- but ONLY ONCE. JUST ONCE. After you do that, you never touch the lamp again. Ever. I don't care how many times you go in the room."
"All you joes -- if the lamp is already on, don't touch it. Leave it on. That just means that someone else turned it on and Killer here hasn't seen it yet. If the lamp is off, and you touched it before, leave it the hell off! Cause if you turn it on again, Killer's count is gonna get messed up, and we're all going to die."
"Killer, you got pencil and paper? Good. Maybe you want to tell Numbers here to do the counting instead? It doesn't matter who does it, as long as we all agree RIGHT NOW who's going to keep the count. Because if you blow the count -- either we are going to be waiting here forever, or you are going to pipe to the warden too soon and we'll all fry."
"Any questions?"
(One of my buddies has to be pre-arranged to ask this): "Yeah! What if I go in the room and the light's already on? What do I do?"
"Answer: you do nothing! You turn around and walk out! If you haven't turned on the light for yourself yet -- this doesn't count."
Q: What if I turned on the light and the jailer calls me back and it's still on?
A: "Same as above
Then we train the guys using a couple of decks of playing cards, and a lamp, so they can see how it works.
.
* SPOILER *
.
* SPOILER *
.
* SPOILER *
The rule is: Turn on the light if it's off, unless you've already done this once, in which case, do nothing.
The day all 100 of you meet, designate one person to turn off the light. Have them count each light they turn off. When they reach 100, they will know everyone else has been out already, and can safely demand their freedom.
(Of course, assuming the warden really does pick someone at random, he could pick the same person every day, forever. Or not pick one person, every day, forever. Either way, there's no guarantee you're ever getting out.)
He who refuses to do arithmetic is doomed to talk nonsense.
It doesn't matter if there is a tail in the first 20, if there is then it becomes a head, so you end up with 19 tails in the first 20 and 19 tails in the next infinity.
sig's not here
Am I the only one who thinks this interviewing technique is retarded?
Because Microsoft does something most definitely isn't a reason to emulate it. Microsoft isn't exactly known for producing well designed software, nor software that reuses proven patterns or algorithms that solved known problems 20 years ago. Better to hire a bunch of 21 year old college grads who can solve word problems from 8th grade algebra, and pretend that Microsoft invented computers! Whee.
When I hire developers I want them to be good developers, not promising young interns. My interview questions typically involve technology questions, process questions, some theoretical PROGRAMMING questions, and some social / communication questions. I'm not saying that hiring smart people is a bad idea, but ignoring skills and only looking at generic problem solving ability is a recipe for unbelievably bad code. It's like hiring musicians based on measured hearing sensitivity and reflexes. OK, maybe that matters if you want to figure out which 5 year old is going to be a prodigy, but hand them an instrument and the noise that comes out is going to sound like ASS.
Examples of things that "smart" developers I've worked with before have totally missed:
- the existence of more efficient data structures than arrays
- generalizing code into reusable chunks (functions, objects, whatever)
- regular expressions
- the difference between "client" and "server"
- the reason for using descriptive variable names
- collection libraries with built in sorting ("whatcha workin' on?" / "coding up a quicksort algorithm" / "in a J2EE app!?!?")
You can't just get this from reading a book, either, although that definitely helps. You have to have some degree of EXPERIENCE too: at least a few projects, and some awareness of things like performance tuning, security, coding for maintainability, etc.
I would use these "tech interview questions" only for hiring interns or recent college grads where the expectation is zero experience, zero clue, zero skill, and a correspondingly low salary. After all you're investing in someone. But for someone that commands a market rate developer salary in the high five figures, screw the brain teasers - just spend a couple of hours grilling them on skills, experience, discipline, etc. They will respect you big time in return because they know when you extend an offer that they won't be working with a bunch of dumb-asses who can get the explorers across the river without being eaten by the headhunters but who can't code their way out of a soggy paper bag.
I've caught some really bright people with it.
So, what's the point again? Proving people aren't as bright as they think? Making people sit there and squirm because they really need a job, are nervous anyway, and have some "three cups and a white marble" puzzle standing between them and feeding their kids?
I don't get it. My first question would be "why don't we just hire the bright people and get back to work?"
Yes, these questions look exactly what Microsoft optimizes for: employees who are really "smart" in a Mensa-sort-of-way. Too bad that programming isn't about being "smart", it's about craftsmanship, taste, engineering tradeoffs, tradition, experience, and long-term dedication. And, not surprisingly, those are areas where Microsoft is sadly lacking.
The guy freaked. He started complaining that it was unfair and things like that. The funny part was I wasn't judging based on what answer he gave, but how he answered the question. He could have done well, by just rambling about the tradeoffs between different answers. Hell, he could have picked any answer and still got the job, but to lose it over a single question. That was unacceptable.
Where I work, things are often unfair. You can't freak out about it, or you're lost. He was the only person we interviewed for the job. We didn't hire anyone.
'SBEMAIL!' is better than a goat!!
Anyway, during my Microsoft interview when I was an undergrad, they asked me the following question, which I got "wrong"
You have a 7kg bar of gold (assumed rectangular). Your employee gets paid 1kg of gold a day for seven days (because apparently Microsoft people don't get the weekend off). What is the minimal number of cuts to make such that you can pay him 1kg every day?
I came up with some creative solutions, such as:
- Cutting in 3/4 section, stacking the sections, and recutting, so one cut breaks two pieces.
- Cutting a cosine wave into the bar which just brushed the edges with period 7.
- A whole bunch of other ideas, all of which were "wrong".
Anyway, after much back and forth, he basically hinted away that the answer he wanted was to cut the bar into sections of 1kg, 2kg, and 4kg. Then you give him 1kg the first day, then on the second day, give him the 2kg and ask for the 1kg back, etc etc. (ie binary arithmetic basically)Personally, this seemed like the stupidest answer ever to me, in that you were making the assumption that your employee would a) not spend any of the gold you gave him and b) bring it back to work with him the next day.
Long story short, I didn't get the job, but I think that it shows that people are too fixated on what they think is the "right" answer to something like this, when in reality, there are other solutions.
I could also add some good natured Microsoft bashing about how they make stupid assumption like this in code, but then you wouldn't have anything to reply with
Man, that's great. But isn't this backwards:
"whichever computer gets a blue screen of death first will win the fortune for its owner."
To work, shouldn't it be "whichever computer gets a BSOD first will lose the fortune for it's owner." ??
try { do() || do_not(); } catch (JediException err) { yoda(err); }
I've seen a windows system, fresh off of install (nothing added in, all drivers detected by windows), with no hardware problems, crash in under 20 minutes.
the same system ran Linux for a month without rebooting.
I didn't think anything of it at the time. I should have video taped it..........
I would call it "the Bingo solution"...
- You are asuming that everyone has paper and pencil (or similar instruments).
- They have to be careful (not to loose the count of the days so that incorrect info is transmited)
- If the prisoners were taken at a regular pattern, they would never go out, but the text says "Everyday, the warden picks a prisoner at random" so there in no regular pattern.
The first days:
In average, in the first 100 days one should be picked in his own day, so himself and the next one who visits the room know that he has been there. Both can tell about it if they are chosen in that day.
The last dyas:
Probably, the last days before freedom many of them have their "bingo cards" almost complete and they are waiting for the last to be chosen to visit the room in his own day.
--
ACid