Journal Saige's Journal: Brain Teasers and Algorithms - Straight from my Interview 21
As I had a request to give information on the types of brain teasers and algorithms I was asked at my interview, I thought I'd share everything I could remember with those interested. However - there are no answers here, as getting the correct answer wasn't the point here, but going through the problem solving process.
Brain Teasers:
- You have a list of n numbers, in random order, from 1 to n+1 - except there is one missing. Find the missing number.
- There are four cities, located at what would be the corner points of a square with an edge length of 1 mile. You need to build roads to connect all the cities, though they don't all have to be directly connected. What is the smallest amount of road you need to build?
And a bonus one, not one I was asked, but one I saw while preparing, and couldn't get out of my head until I had solved it:
- There are 4 women on one side of a bridge. It is night out, and thus dark. While they have a single flashlight, the bridge is still dangerous, so they can only cross one or two at a time, and only while one of them is carring the flashlight - and they have to walk the flashlight across, they can't toss it. Each woman walks at a different speed - woman A crosses in 1 minute, woman B crosses in 2 minutes, C in 5 minutes, and D in 10 minutes. When two women are walking together, they walk at the speed of the slowest woman.
How do they cross in 17 minutes? And what is the other way?
Algorithms/Functions:
- Given a string containing a full URL, return the string with the directory structure simplified. By simplified, I mean that if "http://www.website.com/this/website/really/sucks/../../is/great/index.html" is passed in, you'd return "http://www.website.com/this/website/is/great/index.html"
- You are given two arrays that contain sorted lists of integers. The arrays are not full - empty space remains in both, and at least one array has room for all the elements in both lists. Merge them together into an array with room, in sorted order.
- Give an algorithm to find all the words in a string of arbitrary length, and the number of times each word appears.
- Given two strings of less than ten characters, how do you determine if the two are anagrams? Now, how do you do it for strings of thousands of characters?
- Give an algorithm for solving the n queens problem. If you are not familiar with it, the problem is - how do you fit n queens on a n by n chessboard so that none of them could capture any other?
- You are given a list of n numbers, from 1 to n, in random order. Using only a call to the function "flip(list, size_of_list, flip_position)", sort the list. When you pass the list into flip(), all the items from 1 to flip_position are reversed (ie 3 1 5 6 7 flipped at 4 becomes 6 5 1 3 7). Find the most efficient solution.
Wow (Score:2)
The bridge thing is an oldie but a goodie, commonly called the "U2 Bridge problem."
Re:Wow (Score:1)
I think they did. The point isn't that you answer the questions correctly it is how you go about answering them, that is what direclty reflects your abilities at a job.
Would you rather:
Person (A) who gets 80% of the questions right without thinking about them but gives up on the others.
OR
Person (B) who during your 2 hour interview only got 50% of them right but was on the right track for the other 50% and, like our little
Re:Wow (Score:2)
There are some good well-thought-out rants against the "MS style" interview on the net (sorry no link at the moment). Most of them seem to advocate asking canidates to solve job related problems and to disscu
Re:Wow (Score:1)
5+ hours worth of interviews is a lot of time, and a lot of opportunities to ask the algorithm/brain teaser questions, along with asking questions to better get a feel for the personality, along with details of past jobs and solving more job
Re:Wow (Score:2)
Re:Wow (Score:2)
No test, quiz, hypothetical situation, or 1001 Interview Questions (from the Big Book Of Interview Questions) is better at judging how well a person does at the job than actually making them do the job. We also get to see how they do the job (widget-construction style, neatness, attention to detail, etc.).
---
Question for candidate 1:
Here is a flat-file. H
Re:Wow (Score:1)
As far as the right candidate I'd have to ponder if this is just a contractor like thing where I need to get this one job done as best as possible or if I'm looking to hire someone who in a few years will move up the food chain with me =)
There was a supervisor at IBM I worked with who said he'd never have anyone work under him who could ever replace him. Didn't seem to be thinking about ever moving up the foodchain to me,
Re:Wow (Score:1)
Re:Wow (Score:1)
Buffer's brain is currently on shore leave.
self answering? (Score:1)
"You have a list of n numbers, in random order, from 1 to n+1 - except there is one missing. Find the missing number."
Sure implies to me that you are merely given 2 to n+1 and the O(1) solution is that the number one is absent. It could just be how the problem is stated.
The rest are pretty darn straighforward things I'd expect my students to be able to do... hmm, perhaps there are some good test questions in the lot.
Re:self answering? (Score:1)
There is also an almost identical problem where there are 1 to n numbers, and a list of size n+1, with one of the elements duplicated, and you need to find which number is present twice. The answer is practically identical.
Re:self answering? (Score:1)
Hmm... (Score:1)
Re:Hmm... (Score:1)
The bonus - well, I'm assuming you mean you think it can only be done in 19 minutes - A with B (2), A back (3), A with C (8), A back (9), A with D (19). That's the best I could see for a while, also. But the problem is that you are optimizing eac
Re:Hmm... (Score:1)
Yup, I tend to be myopic with my answers. It causes me as many problems as it solves(I'm gradually learning to grow through my mental myopia, but it's a hard process). I read the answer, and now I don't understand how I
Re:Hmm... (Score:2)
TMTOWTDI. It's easy, because you know only one numbers is missing. There are a couple of O(n) calculations you can do that will reveal the missing number immediately.
Re:Hmm... (Score:2)
this is easy if you realize you need C&D to cross together. There are two ways to make this happen, and they both yield 17 minutes.
Hmmmmm - if little, widely known problems like this makes MS think they are getting really clever people, it explains a lot.
Moo (Score:2)
n+1(n+2)/2 - (all the numbers added together).
Typo (Score:2)
You left out parens on the n+1.
Re:Typo (Score:2)
The reason is....... the answer originally was n(n+1)/2--the normal formula--until i realized that he already described n as being one less than the number. So, i added one to each n, completely forgetting about the parens. Thanx!
Oh my... (Score:1)
For my last job I got asked 5 or 6 of them, and since I ask them myself, I did pretty well
(I did admit to them that I'd heard almost all of them before)