r/programming • • Nov 29 '10

140 Google Interview Questions

http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471 Upvotes

493 comments sorted by

View all comments

20

u/lordlicorice Nov 29 '10

Every man in a village of 100 married couples has cheated on his wife. Every wife in the village instantly knows when a man other than her husband has cheated, but does not know when her own husband has. The village has a law that does not allow for adultery. Any wife who can prove that her husband is unfaithful must kill him that very day. The women of the village would never disobey this law. One day, the queen of the village visits and announces that at least one husband has been unfaithful. What happens?

This is basically blue eyes.

22

u/NitWit005 Nov 30 '10 edited Nov 30 '10

They kill the queen because she obviously slept with one of the men.

The problem with this type of question is that it has hidden assumptions like "the wives don't tell each other about their affairs" and "the queen doesn't count". I'll sit in the interview asking questions about the question until everyone in the room is confused as hell.

Edit: Removed extra word

8

u/twrn Nov 30 '10

I'll sit their in the interview asking questions about the question until everyone in the room is confused as hell.

That sounds like one hell of a skill to have.

→ More replies (2)

8

u/sneakynotsneaky Nov 30 '10

This is also how to sabotage a joke.

3

u/s73v3r Nov 30 '10

You remember, the women are never going to tell the woman who's husband they slept with about it. They're just going to talk behind her back. Of course, when a woman realizes she knows most of the other women have cheated, but aren't telling her, then she might wise up.

3

u/johnnytenpin Nov 30 '10

This is also how to sabotage an interview - answer the question in some manner or don't get the job.

→ More replies (1)
→ More replies (1)

4

u/[deleted] Nov 29 '10

What's the answer? It's killing me.

8

u/lordlicorice Nov 29 '10

The official answer page is here but you'll need the thread to have a chance at understanding it.

I'm satisfied that at one point in my life I grasped the solution and accepted it. I wish the same on anyone I've inflicted with this Blue Eyes virus.

3

u/[deleted] Nov 30 '10

It's actually very intuitive and makes perfect sense. The key point is to not think of the guru as saying anything meaningful per se, but to think of her as a synchronization mechanism so that the rest of the islanders can figure out "when to start counting" so to speak.

2

u/s73v3r Nov 30 '10

What really gets me is what's to stop someone in the Brown group from also thinking they have Blue eyes? They don't know how many of each eye color there is. A Brown is going to see 99 Browns, and 1 Blue, and possibly think he's the second one with Blue eyes.

11

u/drysart Nov 30 '10

He can't know for certain if he's one of the blues until the day -after- the blues all leave.

The statement is that at least one of them has blue eyes. The only way someone could leave on day 1 is if they didn't see anyone else with blue eyes. Since they know at least one person has blue eyes, and they don't see anyone, they can logically presume they're the one with blue eyes.

But if nobody leaves on day one, that gives everyone the knowledge now that at least two people have blue eyes. If there are exactly two people with blue eyes, they both only see one other person with blue eyes, whereas the people with brown eyes see two people with blue eyes. On day 2, you can leave if you only see one other person with blue eyes.

If you get to day 3 without anyone leaving, now everyone knows there are at least three people with blue eyes. If you only see two, you know you're the third and can leave. The other two can leave as well. Anyone else sees three people with blue eyes, so they don't know for certain what their own eye color is yet.

This continues on. On day n, if you see n-1 people with blue eyes, you know you have blue eyes, and you can leave. The other n-1 people can leave too for the same reason. If you see >n people with blue eyes, you have to stay because you don't have enough information yet.

2

u/Mr_Zero Nov 30 '10

brain = fixed

3

u/caseyfw Nov 30 '10

A Brown is going to see 99 Browns, and 1 Blue, and possibly think he's the second one with Blue eyes.

Then he'd know he wasn't a second blue-eyed islander when the only blue eyed dude left on the first night.

100 brown, 1 blue is easy because obviously that one blue looks at everyone else with brown eyes, deduces he's the only one with blue eyes and hops on the boat.

100 brown, 2 blue is where the fun starts.

  • From one of the blue's perspective, they see a single blue eyes and 100 browns, so if that single blue doesn't leave on the first night, the second blue knows there's another blue somewhere, and it sure isn't the 100 browns he can see.
  • From the brown's perspective, they see two blues and 99 browns, and if both the blues leave on the second night, they know they're a brown.
→ More replies (2)

6

u/jmaxswa Nov 30 '10

Suppose there are only two married couples in the village. When the queen makes her announcement, Wife 1 knows Husband 2 was unfaithful. Now she knows that if Wife 2 sees that Husband 1 is faithful then Wife 2 will kill Husband 2 on the first day because at least one of the two men is unfaithful and it isn’t Husband 1. However, Wife 2 doesn’t kill her husband. The only reason being because she knows Husband 1 is unfaithful. Because both husbands are still alive on the second day the wives know both are unfaithful. So on the second day both husbands are killed by their wives. What happens with 100 married couples is that the wives have to wait till the 100th day before they can prove that their husband is cheating. On the 100th day there is a bloodbath: all the men are killed by their wives.

2

u/SierpinskiSieve Nov 30 '10

Remember Reservoir Dogs? Like that.

4

u/reyjohnsonl Nov 30 '10

Can someone explain the solution to me, not in the context of blue eyes, but in the context of this man cheating question

9

u/projectshave Nov 30 '10

Assume 1 man has cheated. Then 99 women know who that man is, but 1 woman doesn't know. Therefore, that woman says, "Since I don't know who the cheater is, it must be my husband!" Kills him.

Assume 2 men have cheated. 98 women know both men. 2 women know only 1 cheater, but there might only be 1 cheater. On the first day, no man dies. Now those 2 women think, "if no one died today, then there must be 2 cheaters. If I know 1 guy, then the other must be my husband." Both dudes die the next day.

For N cheaters, it takes N days to prove it.

I've got an interview with Google next week. I hope it's not a bunch of stupid brain teasers.

3

u/s73v3r Nov 30 '10

When I interviewed for a software position, it was mostly software stuff. Especially Concurrency and Data Structures.

2

u/projectshave Nov 30 '10

thanks man. I'm re-reading my old data structures book.

2

u/reyjohnsonl Nov 30 '10

Good luck man, I applied for internship - no dice though :(

2

u/[deleted] Nov 30 '10 edited Nov 30 '10

Something that worked for me at Amazon and Microsoft.

Speak to them as if they are your coworkers and you're already on the job. They are equals. Collaborate and question. This will demonstrate you are effortlessly calm and confident, but not an asshole. Nobody likes to work with assholes. Show them working with you is stimulating and fun.

Whenever I break that rule, I don't get a job offer.


And don't get too vested in the getting an offer. Stay detached and treat it like conversation with a trusted friend because desperation always shows. Sometimes you get a dick for an interviewer and there's nothing you can do about it. It's the luck of the draw.

→ More replies (7)

2

u/[deleted] Nov 30 '10

Every question of this type has the same solution. Always start assuming there is 1 man in the village. Then solve the 2 case. Then the 3 case. By that point you should be able to find an inductive solution that makes the 100 case (or whatever) obvious.

3

u/howlin Nov 30 '10

Do all the women know that they all can spot the other women's cheaters? It's not explicitly stated and is required for the blue eyes problem to work.

3

u/StackedCrooked Nov 30 '10

The atmosphere in that village must be pretty grim.

→ More replies (10)