Problem: reverse "the house is blue", the answer should be "blue is house the". The words are reversed, but the letters are still in order (within the word).

Solution: solving the initial problem of just reversing a string can either be a huge help or a frustrating hinderance. most likely the first attempt will be to solve it the same way, by swapping letters at the front of the string with letters at the back, and then adding some logic to keep the words in order. this attempt will lead to confusion pretty quickly.

For example, if we start by figuring out that "the" is 3 letters long and then try to put the "t" from "the" where the "l" from "blue" is, we encounter a problem. Where do we put the "l" from "blue"? hmm... well we could have also figured out how long "blue" was and that would tell us where to put the "l" at... but the "e" from "blue" needs to go into the space after "the". argh. It's getting quite confusing. in fact, i would be delighted to even see a solution to this problem using this attack method. I don't think it's impossible, but I think it is so complex that it's not worth pursuing.

Here's a hint. Remember before when we just reversed "the house is blue"? What happened?

Look at the result for a minute. Notice anything? If you still don't see it, try this.

The solution can be attained by first reversing the string normally, and then just reversing each word.

Admin

28 יוני 2017

2.

Problem: you have two jars, 50 red marbles, 50 blue marbles. You need to place all the marbles into the jars such that when you blindly pick one marble out of one jar, you maximize the chances that it will be red. (When picking, you'll first randomly pick a jar, and then randomly pick a marble out of that jar) you can arrange the marbles however you like, but each marble must be in a jar.

Solution: chance! chance is easy if you know how to do the formula. We know that we have two choices to make. First we'll pick a jar, and each jar will have a 1/2 chance of being picked. Then we'll pick a marble, and depending how we stack the marbles, we'll have a (# of red marbles in jar)/(# of total marbles in jar) chance of getting a red one.

For example, say we put all the red marbles into jar A and all the blue ones into jar B. then our chances for picking a red one are:

1/2 chance we pick jar A * 50/50 chance we pick a red marble
1/2 chance we pick jar B * 0/50 chance we pick a red marble

Do the math and you get 1/2 chance for a red marble from jar A and a 0/2 chance for a red marble from jar B. add 'em up and you get the result = 1/2 chance for picking a red marble.

Think about it for awhile and see if you can figure out the right combination. We had a 50/50 (guaranteed) chance in picking a red marble from jar A, but we didn't have to have 50 red marbles in there to guarantee those fantastic odds, did we? We could've just left 1 red marble in there and the odds are still 1/1. Then we can take all those other marbles and throw them in jar B to help the odds out there.

Let's look at those chances:

1/2 we pick jar A * 1/1 we pick a red marble
1/2 we pick jar B * 49/99 we pick a red marble

Do the math and add them up to get 1/2 + 49/198 = 148/198, which is almost 3/4.

Admin

28 יוני 2017

3.

String manipulation functions are great programming questions. They test whether the user can understand and translate into code simple algorithms. String functions test pointer arithmetic which usually shows a knowledgeable programmer. Also there are usually multiple solutions, some more efficient than others. Plus people use them all the time so they should understand how they work. My favorite is atoi and i start the problem like this:

int atoi( char* pStr )

Write the definition for this function without using any built-in functions. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its "123A", then return 123). Assume all numbers are positive. plus or minus signs can be considered non-numeric characters. In order to solve this program, the programmer must understand the difference between the integer 0 and the character '0', and how converting '0' to an int, will not result in 0. In other words, they have to understand what ascii is all about. if they are stuck solving this problem, just ask them first to write:

charToInt(char c)

if they can't do that then they basically missed half the problem. Any moderately talented programmer who has a CS degree knows how to convert a char to an int. (note i said convert, not cast. charToInt('9') should return 9.)

when they start to solve the problem you will notice that they must make a choice in how they will process the string - from left to right or right to left. i will discuss both methods and the difficulties encountered in each.

"right to left" - this method starts at the right hand letter of the string and converts that character to an int. it then stores this value after promoting it to its correct "tens" place.

"left to right" - this method keeps adding the number and multiplying the result by ten before continuing to the next number. e.g. if you had "6234" and you processed from left to right you'd have 6, then if you kept reading you'd multiply your result by 10 (6*10) to add a zero for where the next number would go. 60, and then you'd slide the 2 into the zero place you just made. 62. Do it again, 620, slide the next number in, 623.

I think the "left to right" method is a little bit cleaner, or maybe its just cooler. But both are "correct".

Remember that debugging code on paper is somewhat hard. most programmers aren't used to studying code that much when you can just hit F-7, compile and see if the compiler barfs or not. If you notice an error, just ask them to step through a sample string drawing out what is happening with all the variables and the pointers in every step. They should find their mistake then and fix it (no points deducted).

Admin

28 יוני 2017

4.

Two MIT math grads bump into each other at Fairway on the upper west side. They haven't seen each other in over 20 years.

The first grad says to the second: "how have you been?"

Second: "great! i got married and i have three daughters now"

First: "really? How old are they?"

Second: "well, the product of their ages is 72, and the sum of their ages is the same as the number on that building over there..."

First: "right, ok.. Oh wait.. Hmm, I still don't know"

Second: "oh sorry, the oldest one just started to play the piano"

First: "wonderful! My oldest is the same age!"

Problem: how old are the daughters?

Solution: start with what you know. You know there are 3 daughters whose ages multiply to 72. Let's look at the possibilities...

after looking at the building number the man still can't figure out what their ages are (we're assuming since he's an MIT math grad, he can factor 72 and add up the sums), so the building number must be 14, since that is the only sum that has more than one possibility.

Finally the man discovers that there is an oldest daughter. That rules out the "2 6 6" possibility since the two oldest would be twins. Therefore, the daughters ages must be "3 3 8".

(caveat: an astute reader pointed out that it is possible for two siblings to have the same age but not be twins, for instance one is born in january, and the next is conceived right away and delivered in october. next october both siblings will be one year old. if a candidate points this out, extra credit points to him/her.)

This question is pretty neat, although there is certainly a bit of an aha factor to it. The clues are given in such a way that you think you are missing information (the building number), but whats important isn't the building number, but the fact that the first man thought that it was enough information, but actually wasn't.

Even if the candidate doesn't know the solution, they could come up with some interesting thoughts. If they just stare at you and shrug "i dunno" then thank them for their time and don't give them a fogcreek pen.

Admin

28 יוני 2017

5.

Problem: this year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards).

10/02/2001
when was the last date that this occurred on? (See if you can do it in your head!)

Solution: we know the year has to be less than 2001 since we already have the palindrome for 10/02. It can't be any year in 1900 because that would result in a day of 91. Same for 1800 down to 1400. It could be a year in 1300 because that would be the 31st day. So whats the latest year in 1300 that would make a month? at first i thought it would be 1321, since that would give us the 12th month, but we have to remember that we want the maximum year in the 1300 century with a valid month, which would actually be 1390, since 09/31 is a valid date.

But of course, a question like this wouldn't be complete without an aha factor. and of course, there are not 31 days in September, only 30. So we have to go back to august 08 which means the correct date would be 08/31/1380.

Palindromes also offer another great string question.
Write a function that tests for palindromes
bool isPalindrome( char* pStr )

If you start a pointer at the beginning and the end of the string and keep comparing characters while moving the pointers closer together, you can test if the string is the same forwards and backwards. Notice that the pointers only have to travel to the middle, not all the way to the other end (to reduce redundancy).

Admin

28 יוני 2017

6.

Problem: you are given a sequence of numbers from 1 to n-1 with one of the numbers repeating once. (Example: 1 2 3 3 4 5) how can you find the repeating number?

Solution:

as a programmer, my first answer to this problem would be make a bit vector of size n, and every time you see the number, set its correspond index bit to 1. If the bit is already set, then that's the repeater. Since there were no constraints in the question, this is an ok answer. It's good because it makes sense if you draw it for someone, whether they are a programmer, mathemetician, or just your grandpa. It's not the most efficient answer though.

now, if i add the constraint that you can only use a fixed amount of memory (i.e. not determined by n) and it must run in O(n) time... how do we solve it. Adding all the numbers up from 1 to n-1 would give us a distinct sum. Subtracting the total sum of all the numbers from the sum of n to n-1 ( which is (n)(n-1)/2 ) would give us the secret extra number.

What if you can only use a fixed amount of memory, and two of the numbers are repeated? We know that the numbers have a distinct sum, and the difference would be equal to the sum of our unknowns
c = a + b
where c is the sum and a and b are the unknowns - c is a constant
if we had another similar formula we could solve the two unknown equations. my first thought was that the numbers would have a distinct product - (n-1)!
if we divide the total product by the (n-1)! product, we would get another equation
c2 = ab
we could then solve the two equations to get them into quadratic formula notation
0 = ax^2 + bx + c
and solve for the two values of x. this answer is correct but factorial grows really fast.

some sort of sum would be better. The sum of the squares from n-1 to 1 would work. That would yield a function of the form
c2 = a^2 + b^2
which could also be solved by using the quadratic equation.

I think its fine to remind someone of the quadratic equation... (Maybe only because i myself had to look it up to solve the problem) i mean really though, the last time i used it was probably in 10th grade. As long as they get the idea that given two unknowns and two equations you can solve for the unknowns - that's the point.

Admin

28 יוני 2017

7.

Five pirates have 100 gold coins. They have to divide up the loot. in order of seniority (suppose pirate 5 is most senior, pirate 1 is least senior), the most senior pirate proposes a distribution of the loot. They vote and if at least 50% accept the proposal, the loot is divided as proposed. Otherwise the most senior pirate is executed, and they start over again with the next senior pirate. What solution does the most senior pirate propose? Assume they are very intelligent and extremely greedy (and that they would prefer not to die).

(To be clear on what 50% means, 3 pirates must vote for the proposal when there are 5 for it to pass. 2 if there are 4. 2 if there are 3. etc...)

solution: most of the time i get people who give answers like "the most senior pirate takes half and divides the rest up among the least senior pirates." um, you missed the whole point to begin with. Sorry.

Any answer without a specific logic behind it is invalid. If i ask you why pirate 5 gave x coins to pirate 1, please don't say "because he's nice".

Now for the real solution. Pirate 5 being the most senior knows that he needs to get 2 other people to vote for his solution in order for him not to be executed. So who can he get to vote for him, and why would they choose to vote for him? if you start thinking that pirate 4 will never vote for him, because he would rather have 5 die and then be in charge and take it all for himself, you are on the right track. but it gets more complicated.

Let's consider if there were only 1 pirate. Obviously he would take it all for himself and no one would complain.

If there were 2 pirates, pirate 2 being the most senior, he would just vote for himself and that would be 50% of the vote, so he's obviously going to keep all the money for himself.

If there were 3 pirates, pirate 3 has to convince at least one other person to join in his plan. So who can he convince and how? Here is the leap that needs to be made to solve this problem. Pirate 3 realizes that if his plan is not adopted he will be executed and they will be left with 2 pirates. He already knows what happens when there are 2 pirates as we just figured out. Pirate 2 takes all the money himself and gives nothing to pirate 1. So pirate 3 proposes that he will take 99 gold coins and give 1 coin to pirate 1. Pirate 1 says, well, 1 is better than none, and since i know if i don't vote for pirate 3, i get nothing, i should vote for this plan.

Now we know what happens when there are 3 pirates. So what happens with 4? Well pirate 4 has to convince 1 other person to join in his plan. He knows if he walks the plank then pirate 3 will get 99 coins and pirate 1 will get 1 coin. Pirate 4 could propose giving pirate 1 two coins, and surely pirate 1 would vote for him, since 2 is better than 1. But as greedy as he is, pirate 4 would rather not part with 2 whole coins. He realizes that if he gets executed, then pirate 3's scenario happens and pirate 2 gets the shaft in that scenario (he gets zero coins). so pirate 4 proposes that he will give 1 coin to pirate 2, and pirate 2 seeing that 1 is better than 0 will obviously vote for this plan.

A common objection is that pirate 2 is not guaranteed to vote for this plan since he might hope for the case when there are only 2 pirates and then he gets all the booty. But that is why i said that the pirates are extremely intelligent. pirate 2 realizes that pirate 3 is smart enough to make the optimal proposal, so he realizes that there will never be 2 pirates left, because 3 doesn't want to die and we just showed that 3 has a winning proposal.

So let's sum up at this point

Once you see the pattern it becomes very clear. You have to realize that when a pirate's plan does not succeed then that means you are in the same situation with one less pirate.
1. Pirate 1 needs 0 other people to vote for him. So he votes for himself and takes all the money. 2. Pirate 2 needs 0 other people to vote for him. So he votes for himself and takes all the money. Pirate 1 gets 0. 3. Pirate 3 needs 1 other person to vote for him. He gives 1 coin to pirate 1 for his vote - if we are reduced to 2 pirates, pirate 1 gets 0 so pirate 1 knows 1 is better than none. Pirate 3 takes 99. Pirate 2 gets 0. 4. Pirate 4 needs 1 other person to vote for him. He gives 1 coin to pirate 2 - if we reduce to 3 pirates, pirate 2 gets 0 so pirate 2 knows 1 is better than none. Pirate 4 takes 99. Pirate 3 gets 0. Pirate 1 gets 0. 5. Pirate 5 needs 2 other people to vote for him. It's clear now that the 2 people he needs to convince are the 2 who get shafted in the 4 pirate scenario - pirate 3 and pirate 1. So he can give them each 1 coin (which is better than 0 - what they would get otherwise) and keep 98 for himself.

What happens if there are 15 pirates? Pirate 15 needs 7 other people to vote for him, so he recruits pirates 13,11,9,7,5,3, and 1 with 1 coin each and keeps 93 coins himself. Those pirates will all vote for him because they know that they get 0 coins if he dies and pirate 14 is in charge.

Hope you enjoyed this one. It's my favorite interview question of all. It really allows the candidate to ask a lot of interesting questions and it's really amazing when they reach the solution all by themselves (as all fogcreek employees have done so far).

Admin

28 יוני 2017

8.

Problem: 100 fogcreek programmers are lined up in a row by an assassin. The assassin puts red and blue hats on them. They can't see their own hats, but they can see the hats of the people in front of them. the assassin starts in the back and says "what color is your hat?" the fogcreek programmer can only answer "red" or "blue." the programmer is killed if he gives the wrong answer; then the assassin moves on to the next programmer. The programmers in front get to hear the answers of the programmers behind them, but not whether they live or die. they can consult and agree on a strategy before being lined up, but after being lined up and having the hats put on, they can't communicate in any way other than those already specified. What strategy should they choose to maximize the number of programmers who are guaranteed to be saved?

This is a very difficult problem to solve during an interview (especially if you've already taxed the candidate's brain). Look for obvious solutions first, and the reasoning behind them and then try to lead them to the ultimate solution.

A logical answer could be all the programmers would just say "red" and that way about half of them would survive on average, assuming the hats were distributed randomly.

This is a good start and should naturally lead to having every other programmer say the color of the hat in front of them. The first programmer would say the color of the hat in front of him, then the next programmer would just say that color that was just said. So we can guarantee that half survive - the even numbered programmers (since the person behind them told them the answer). And potentially if the hats were distributed randomly some of the programmers would get lucky and the hat in front of them would be the same color as their own. So this strategy should save more than half, and on average 75% of them would live.

At this point, if the solution is not clear, the candidate may give answers like, "they could agree that if they said their hat color in a soft voice, it means the hat in front of them is the same color, and if they say it in a loud voice, it means the hat in front is a different color". This is definitely good and on the correct track. Another option is they could say "reeeeeeeeeeed" for x number of seconds, where x represented the distribution of hats where a hat was a bit in a binary number, (red = 1, blue = 0). Another interesting answer. There are many others like these that "bend" the rules and come to a solution.

But the real solution acknowledges that the programmers can only say "red" or "blue" and cannot alter their voice in such a convincing way as to signal any information other than the word they said. A good way to get this point across, is simply to change the problem slightly by saying "the assassin gets to hear their plan before she puts the hats on, and so will try to thwart the plan however she can."

So if they decide to all say "red", she'll put blue hats on all of them. If they decide to all say the color of the hat in front of them, she'll alternate the hats on every head, guaranteeing half will die. Even with the assassin hearing their plan, there is still a way to save almost everyone.

We know that the first person is never going to have any information about the color of their hat, so they cannot be guaranteed to survive. But, i'll give you a hint to the solution: i can save every other person for sure.

Solution: they agree that if the number of red hats that the back person can see is even, that programmer will say "red". if they add up to an odd number, they will say "blue". this way number 99 can look ahead and count the red hats. if they add up to an even number and number 100 said "red", then 99 must be wearing a blue hat. if they add up to an even number and number 100 said "blue", signalling an odd number of red hats, number 99 must also be wearing a red hat. number 98 knows that 99 said the correct hat, and so uses that information along with the 97 hats in front to figure out what color hat is on 98's head.

Sample:

This shows #100 wearing a red hat, 99 a blue, 98 a blue, 97 a red, 96 a blue, 95 a red, 94 a blue and 45 red hats - 48 blue hats on the people in front of them.

100 counts up the red hats: 47 total. So 100 says "blue". The assassin kills 100. 99 counts up the red hats in front: 47. 100 said blue, so 100 saw an odd number. 99 sees an odd number, so 99 says "blue" and lives. 98 had counted 47 red hats, and 99 didn't say "red" so that's still the total. 98 says "blue". 97 counts up and finds 46 red hats. 99 and 98 didn't say "red", so his count is missing a red hat (it's on his head, he realizes). He says "red". 96 heard the "red" and now knows that there are an even number of "red" hats in front of 95. 96 sees 46, so he knows he has a "blue" hat. Etc...

Even if the assassin knows the plan, she can't thwart it. She hears the plan, but she still has to put the hats on their heads. The plan doesn't rely on any ordering of the hats, so the worst the assassin can do is to make sure #100 gets killed and that's the worst damage she can do.

Admin

28 יוני 2017

9.

Problem: a bad king has a cellar of 1000 bottles of delightful and very expensive wine. A neighbouring queen plots to kill the bad king and sends a servant to poison the wine. (un)fortunately the bad king's guards catch the servant after he has only poisoned one bottle. alas, the guards don't know which bottle but know that the poison is so strong that even if diluted 1,000,000 times it would still kill the king. Furthermore, it takes one month to have an effect. The bad king decides he will get some of the prisoners in his vast dungeons to drink the wine. Being a clever bad king he knows he needs to murder no more than 10 prisoners - believing he can fob off such a low death rate - and will still be able to drink the rest of the wine at his anniversary party in 5 weeks time.

Explain how...

Solution: I'll give you a hint. 1000 is less than 1024. If there were 1024 or more bottles of wine it would take more than 10 prisoners.

Number the bottles 1 to 1000, and write the number in binary format.

Now take your prisoner's 1 through 10 and let prisoner 1 take a sip from every bottle that has a 1 in its least significant bit. Let prisoner 10 take a sip from every bottle with a 1 in its most significant bit. Etc.

For instance, bottle #924 would be sipped by 10,9,8,5,4 and 3. That way if bottle #924 was the poisoned one, only those prisoners would die.

After four weeks, line the prisoners up in their bit order and read each living prisoner as a 0 bit and each dead prisoner as a 1 bit. The number that you get is the bottle of wine that was poisoned.

Additional question: to increase your chance of living, which prisoner would you want to be?

If there were 1023 bottles, it wouldn't matter since everyone would have to take 512 sips. But there are 23 bottles less, so the people whose bits would have been on from 1001 to 1023 won't have to take a sip. 1001 is [11111 01001] in binary and 1023 is [11111 11111]. the most five significant bits are the most interesting because they would always be on from 1001 to 1023, so all those people are missing out on 23 bottles of wine that they otherwise would have had to drink. so in order to increase your chance of living, you'd probably want to be prisoner 6 to 10. (but depending on how the king determines who is least significant and who is most significant you could get shafted.)

note that if the king was really trying to kill the least number of prisoners, he should have let 999 prisoners each take a sip from their respective bottle numerically (if he had that many prisoners at his disposal). that way only one prisoner would die, and there's a chance of 1/1000 that no one would die, but then the puzzle isn't very fun.

Admin

28 יוני 2017

10.

You have three jars that are all mislabeled. One contains peanut butter jelly beans, another grape jelly jelly beans, and the third has a mix of both (not necessarily a 50/50 mix, could be a 1/99 mix or a 399/22 mix). How many jelly beans would you have to pull out, and out of which jars, to find out how to fix the labels on the jars?

Solution: 1 jelly bean from the p.b./grape jar will do the trick.

The trick here is to realize that every jar is mislabeled. Therefore you know that the peanut butter jelly bean jar is not the penut butter jelly bean jar, and the same goes for the rest.

You also need to realize that it is the jar labeled p.b./grape, labelled as the mix jar, that is your best hope. If you choose a jelly bean out of there, then you will know whether that jar is peanut butter or grape jelly jelly beans. it can't be the mix jar because i already said that every jar is mislabeled.

Once you know that jar 3 is either peanut butter, or grape jelly, then you know the other jars also. If it is peanut butter, then jar 2 must be mixed because it can't be grape (as it's labelled) and it can't be peanut butter (that's jar 3). Hence jar 1 is grape.

If jar 3 is grape, then you know jar 1 must be the mix because it can't be p.b. (as its labelled) and it can't be grape (that's jar 3). Hence jar 2 is peanut butter.

If you pick jelly beans from jar 1 or jar 2, then you would have to pick out all of the jelly beans before you knew what that jar was. This is because jar 1 and 2 could be the mix, so in order to disprove that they were the mix, you would have to pull out every jelly bean just to make sure (since there could just be one bean of the opposite flavor in there).

1.

: reverse "the house is blue", the answer should be "blue is house the". The words are reversed, but the letters are still in order (within the word).Problem: solving the initial problem of just reversing a string can either be a huge help or a frustrating hinderance. most likely the first attempt will be to solve it the same way, by swapping letters at the front of the string with letters at the back, and then adding some logic to keep the words in order. this attempt will lead to confusion pretty quickly.SolutionFor example, if we start by figuring out that "the" is 3 letters long and then try to put the "t" from "the" where the "l" from "blue" is, we encounter a problem. Where do we put the "l" from "blue"? hmm... well we could have also figured out how long "blue" was and that would tell us where to put the "l" at... but the "e" from "blue" needs to go into the space after "the". argh. It's getting quite confusing. in fact, i would be delighted to even see a solution to this problem using this attack method. I don't think it's impossible, but I think it is so complex that it's not worth pursuing.

Here's a hint. Remember before when we just reversed "the house is blue"? What happened?

Look at the result for a minute. Notice anything? If you still don't see it, try this.

The solution can be attained by first reversing the string normally, and then just reversing each word.

2.

: you have two jars, 50 red marbles, 50 blue marbles. You need to place all the marbles into the jars such that when you blindly pick one marble out of one jar, you maximize the chances that it will be red. (When picking, you'll first randomly pick a jar, and then randomly pick a marble out of that jar) you can arrange the marbles however you like, but each marble must be in a jar.Problem: chance! chance is easy if you know how to do the formula. We know that we have two choices to make. First we'll pick a jar, and each jar will have a 1/2 chance of being picked. Then we'll pick a marble, and depending how we stack the marbles, we'll have a (# of red marbles in jar)/(# of total marbles in jar) chance of getting a red one.SolutionFor example, say we put all the red marbles into

jar Aand all the blue ones intojar B. then our chances for picking a red one are:1/2 chance we pick

jar A* 50/50 chance we pick a red marble 1/2 chance we pickjar B* 0/50 chance we pick a red marbleDo the math and you get 1/2 chance for a red marble from

jar Aand a 0/2 chance for a red marble fromjar B. add 'em up and you get the result = 1/2 chance for picking a red marble.Think about it for awhile and see if you can figure out the right combination. We had a 50/50 (guaranteed) chance in picking a red marble from

jar A, but we didn't have to have 50 red marbles in there to guarantee those fantastic odds, did we? We could've just left 1 red marble in there and the odds are still 1/1. Then we can take all those other marbles and throw them injar Bto help the odds out there.Let's look at those chances:

1/2 we pick

jar A* 1/1 we pick a red marble 1/2 we pickjar B* 49/99 we pick a red marbleDo the math and add them up to get 1/2 + 49/198 = 148/198, which is almost 3/4.

3.

String manipulation functions are great programming questions. They test whether the user can understand and translate into code simple algorithms. String functions test pointer arithmetic which usually shows

a knowledgeable programmer. Also there are usually multiple solutions, some more efficient than others. Plus people use them all the time so they should understand how they work. My favorite is atoi and i start the problem like this:int atoi( char* pStr )

Write the definition for this function

without using any built-in functions. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its "123A", then return 123). Assume all numbers are positive. plus or minus signs can be considered non-numeric characters. In order to solve this program, the programmer must understand the difference between the integer 0 and the character '0', and how converting '0' to an int, will not result in 0. In other words, they have to understand what ascii is all about. if they are stuck solving this problem, just ask them first to write:charToInt(char c)

if they can't do that then they basically missed half the problem. Any moderately talented programmer who has a CS degree knows how to convert a char to an int. (note i said convert, not cast. charToInt('9') should return 9.)

when they start to solve the problem you will notice that they must make a choice in how they will process the string - from left to right or right to left. i will discuss both methods and the difficulties encountered in each.

"right to left" - this method starts at the right hand letter of the string and converts that character to an int. it then stores this value after promoting it to its correct "tens" place.

"left to right" - this method keeps adding the number and multiplying the result by ten before continuing to the next number. e.g. if you had "6234" and you processed from left to right you'd have 6, then if you kept reading you'd multiply your result by 10 (6*10) to add a zero for where the next number would go. 60, and then you'd slide the 2 into the zero place you just made. 62. Do it again, 620, slide the next number in, 623.

I think the "left to right" method is a little bit cleaner, or maybe its just cooler. But both are "correct".

Remember that debugging code on paper is somewhat hard. most programmers aren't used to studying code that much when you can just hit F-7, compile and see if the compiler barfs or not. If you notice an error, just ask them to step through a sample string drawing out what is happening with all the variables and the pointers in every step. They should find their mistake then and fix it (no points deducted).

4.

Two MIT math grads bump into each other at Fairway on the upper west side. They haven't seen each other in over 20 years.

The first grad says to the second: "how have you been?"Second: "great! i got married and i have three daughters now"First: "really? How old are they?"Second: "well, the product of their ages is 72, and the sum of their ages is the same as the number on that building over there..."First: "right, ok.. Oh wait.. Hmm, I still don't know"Second: "oh sorry, the oldest one just started to play the piano"First: "wonderful! My oldest is the same age!": how old are the daughters?Problem: start with what you know. You know there are 3 daughters whose ages multiply to 72. Let's look at the possibilities...Solutionafter looking at the building number the man still can't figure out what their ages are (we're assuming since he's an MIT math grad, he can factor 72 and add up the sums), so the building number must be 14, since that is the only sum that has more than one possibility.

Finally the man discovers that there is an oldest daughter. That rules out the "2 6 6" possibility since the two oldest would be twins. Therefore, the daughters ages must be "3 3 8".

(caveat: an astute reader pointed out that it

ispossible for two siblings to have the same age but not be twins, for instance one is born in january, and the next is conceived right away and delivered in october. next october both siblings will be one year old. if a candidate points this out, extra credit points to him/her.)This question is pretty neat, although there is certainly a bit of an

ahafactor to it. The clues are given in such a way that you think you are missing information (the building number), but whats important isn't the building number, but the fact that the first man thought that it was enough information, but actually wasn't.Even if the candidate doesn't know the solution, they could come up with some interesting thoughts. If they just stare at you and shrug "i dunno" then thank them for their time and don't give them a

fogcreekpen.5.

: this year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards).Problem10/02/2001 when was the last date that this occurred on? (See if you can do it in your head!)

: we know the year has to be less than 2001 since we already have the palindrome for 10/02. It can't be any year in 1900 because that would result in a day of 91. Same for 1800 down to 1400. It could be a year in 1300 because that would be the 31st day. So whats the latest year in 1300 that would make a month? at first i thought it would be 1321, since that would give us the 12th month, but we have to remember that we want the maximumSolutionyearin the 1300 century with a valid month, which would actually be 1390, since 09/31 is a valid date.But of course, a question like this wouldn't be complete without an

ahafactor. and of course, there are not 31 days in September, only 30. So we have to go back to august 08 which means the correct date would be08/31/1380.Palindromes also offer another great string question. Write a function that tests for palindromes bool isPalindrome( char* pStr )

If you start a pointer at the beginning and the end of the string and keep comparing characters while moving the pointers closer together, you can test if the string is the same forwards and backwards. Notice that the pointers only have to travel to the middle, not all the way to the other end (to reduce redundancy).

6.

: you are given a sequence of numbers from 1 to n-1 with one of the numbers repeating once. (Example: 1 2 3 3 4 5) how can you find the repeating number?Problem:Solutionas a programmer, my first answer to this problem would be make a bit vector of size n, and every time you see the number, set its correspond index bit to 1. If the bit is already set, then that's the repeater. Since there were no constraints in the question, this is an ok answer. It's good because it makes sense if you draw it for someone, whether they are a programmer, mathemetician, or just your grandpa. It's not the most efficient answer though.

now, if i add the constraint that you can only use a fixed amount of memory (i.e. not determined by n) and it must run in O(n) time... how do we solve it. Adding all the numbers up from 1 to n-1 would give us a distinct sum. Subtracting the total sum of all the numbers from the sum of n to n-1 ( which is (n)(n-1)/2 ) would give us the secret extra number.

What if you can only use a fixed amount of memory, and

twoof the numbers are repeated? We know that the numbers have a distinct sum, and the difference would be equal to the sum of our unknowns c = a + b where c is the sum andaandbare the unknowns - c is a constant if we had another similar formula we could solve the two unknown equations. my first thought was that the numbers would have a distinct product - (n-1)! if we divide the total product by the (n-1)! product, we would get another equation c2 = ab we could then solve the two equations to get them into quadratic formula notation 0 = ax^2 + bx + c and solve for the two values of x. this answer is correct but factorial grows really fast.some sort of sum would be better. The sum of the squares from n-1 to 1 would work. That would yield a function of the form c2 = a^2 + b^2 which could also be solved by using the quadratic equation.

I think its fine to remind someone of the quadratic equation... (Maybe only because i myself had to look it up to solve the problem) i mean really though, the last time i used it was probably in 10th grade. As long as they get the idea that given two unknowns and two equations you can solve for the unknowns - that's the point.

7.

Five pirates have 100 gold coins. They have to divide up the loot. in order of seniority (suppose pirate 5 is most senior, pirate 1 is least senior), the most senior pirate proposes a distribution of the loot. They vote and if at least 50% accept the proposal, the loot is divided as proposed. Otherwise the most senior pirate is executed, and they start over again with the next senior pirate. What solution does the most senior pirate propose? Assume they are very intelligent and extremely greedy (and that they would prefer not to die).

(To be clear on what 50% means, 3 pirates must vote for the proposal when there are 5 for it to pass. 2 if there are 4. 2 if there are 3. etc...)

: most of the time i get people who give answers like "the most senior pirate takes half and divides the rest up among the least senior pirates." um, you missed the whole point to begin with. Sorry.solutionAny answer without a specific logic behind it is invalid. If i ask you why pirate 5 gave x coins to pirate 1, please don't say "because he's nice".

Now for the real solution. Pirate 5 being the most senior knows that he needs to get 2 other people to vote for his solution in order for him not to be executed. So who can he get to vote for him, and why would they choose to vote for him? if you start thinking that pirate 4 will never vote for him, because he would rather have 5 die and then be in charge and take it all for himself, you are on the right track. but it gets more complicated.

Let's consider if there were only 1 pirate. Obviously he would take it all for himself and no one would complain.

If there were 2 pirates, pirate 2 being the most senior, he would just vote for himself and that would be 50% of the vote, so he's obviously going to keep all the money for himself.

If there were 3 pirates, pirate 3 has to convince at least one other person to join in his plan. So who can he convince and how? Here is the leap that needs to be made to solve this problem.

Pirate 3 realizes that if his plan is not adopted he will be executed and they will be left with 2 pirates. He already knows what happens when there are 2 pirates as we just figured out. Pirate 2 takes all the money himself and gives nothing to pirate 1. So pirate 3 proposes that he will take 99 gold coins and give 1 coin to pirate 1. Pirate 1 says, well, 1 is better than none, and since i know if i don't vote for pirate 3, i get nothing, i should vote for this plan.Now we know what happens when there are 3 pirates. So what happens with 4? Well pirate 4 has to convince 1 other person to join in his plan. He knows if he walks the plank then pirate 3 will get 99 coins and pirate 1 will get 1 coin. Pirate 4 could propose giving pirate 1 two coins, and surely pirate 1 would vote for him, since 2 is better than 1. But as greedy as he is, pirate 4 would rather not part with 2 whole coins. He realizes that if he gets executed, then pirate 3's scenario happens and pirate 2 gets the shaft in that scenario (he gets zero coins). so pirate 4 proposes that he will give 1 coin to pirate 2, and pirate 2 seeing that 1 is better than 0 will obviously vote for this plan.

A common objection is that pirate 2 is not guaranteed to vote for this plan since he might hope for the case when there are only 2 pirates and then he gets all the booty. But that is why i said that the pirates are extremely intelligent. pirate 2 realizes that pirate 3 is smart enough to make the optimal proposal, so he realizes that there will never be 2 pirates left, because 3 doesn't want to die and we just showed that 3 has a winning proposal.

So let's sum up at this point

Once you see the pattern it becomes very clear. You have to realize that when a pirate's plan does not succeed then that means you are in the same situation with one less pirate. 1. Pirate 1 needs 0 other people to vote for him. So he votes for himself and takes all the money. 2. Pirate 2 needs 0 other people to vote for him. So he votes for himself and takes all the money. Pirate 1 gets 0. 3. Pirate 3 needs 1 other person to vote for him. He gives 1 coin to pirate 1 for his vote - if we are reduced to 2 pirates, pirate 1 gets 0 so pirate 1 knows 1 is better than none. Pirate 3 takes 99. Pirate 2 gets 0. 4. Pirate 4 needs 1 other person to vote for him. He gives 1 coin to pirate 2 - if we reduce to 3 pirates, pirate 2 gets 0 so pirate 2 knows 1 is better than none. Pirate 4 takes 99. Pirate 3 gets 0. Pirate 1 gets 0. 5. Pirate 5 needs 2 other people to vote for him. It's clear now that the 2 people he needs to convince are the 2 who get shafted in the 4 pirate scenario - pirate 3 and pirate 1. So he can give them each 1 coin (which is better than 0 - what they would get otherwise) and keep 98 for himself.

What happens if there are 15 pirates? Pirate 15 needs 7 other people to vote for him, so he recruits pirates 13,11,9,7,5,3, and 1 with 1 coin each and keeps 93 coins himself. Those pirates will all vote for him because they know that they get 0 coins if he dies and pirate 14 is in charge.

Hope you enjoyed this one. It's my favorite interview question of all. It really allows the candidate to ask a lot of interesting questions and it's really amazing when they reach the solution all by themselves (as all

fogcreekemployees have done so far).8.

: 100Problemfogcreekprogrammers are lined up in a row by an assassin. The assassin puts red and blue hats on them. They can't see their own hats, but they can see the hats of the people in front of them. the assassin starts in the back and says "what color is your hat?" the fogcreek programmer can only answer "red" or "blue." the programmer is killed if he gives the wrong answer; then the assassin moves on to the next programmer. The programmers in front get to hear the answers of the programmers behind them, but not whether they live or die. they can consult and agree on a strategy before being lined up, but after being lined up and having the hats put on, they can't communicate in any way other than those already specified. What strategy should they choose to maximize the number of programmers who are guaranteed to be saved?This is a very difficult problem to solve during an interview (especially if you've already taxed the candidate's brain). Look for obvious solutions first, and the reasoning behind them and then try to lead them to the ultimate solution.

A logical answer could be all the programmers would just say "red" and that way about half of them would survive on average, assuming the hats were distributed randomly.

This is a good start and should naturally lead to having every other programmer say the color of the hat in front of them. The first programmer would say the color of the hat in front of him, then the next programmer would just say that color that was just said. So we can guarantee that half survive - the even numbered programmers (since the person behind them told them the answer). And potentially if the hats were distributed randomly some of the programmers would get lucky and the hat in front of them would be the same color as their own. So this strategy should save more than half, and on average 75% of them would live.

At this point, if the solution is not clear, the candidate may give answers like, "they could agree that if they said their hat color in a soft voice, it means the hat in front of them is the same color, and if they say it in a loud voice, it means the hat in front is a different color". This is definitely good and on the correct track. Another option is they could say "reeeeeeeeeeed" for x number of seconds, where x represented the distribution of hats where a hat was a bit in a binary number, (red = 1, blue = 0). Another interesting answer. There are many others like these that "bend" the rules and come to a solution.

But the real solution acknowledges that the programmers can only say "red" or "blue" and cannot alter their voice in such a convincing way as to signal any information other than the word they said. A good way to get this point across, is simply to change the problem slightly by saying "the assassin gets to hear their plan before she puts the hats on, and so will try to thwart the plan however she can."

So if they decide to all say "red", she'll put blue hats on all of them. If they decide to all say the color of the hat in front of them, she'll alternate the hats on every head, guaranteeing half will die. Even with the assassin hearing their plan, there is still a way to save almost everyone.

We know that the first person is never going to have any information about the color of their hat, so they cannot be guaranteed to survive. But, i'll give you a hint to the solution: i can save every other person for sure.

: they agree that if the number of red hats that the back person can see is even, that programmer will say "red". if they add up to an odd number, they will say "blue". this way number 99 can look ahead and count the red hats. if they add up to an even number and number 100 said "red", then 99 must be wearing a blue hat. if they add up to an even number and number 100 said "blue", signalling an odd number of red hats, number 99 must also be wearing a red hat. number 98 knows that 99 said the correct hat, and so uses that information along with the 97 hats in front to figure out what color hat is on 98's head.SolutionSample:

This shows #100 wearing a red hat, 99 a blue, 98 a blue, 97 a red, 96 a blue, 95 a red, 94 a blue and 45 red hats - 48 blue hats on the people in front of them.

100 counts up the red hats: 47 total. So 100 says "blue". The assassin kills 100. 99 counts up the red hats in front: 47. 100 said blue, so 100 saw an odd number. 99 sees an odd number, so 99 says "blue" and lives. 98 had counted 47 red hats, and 99 didn't say "red" so that's still the total. 98 says "blue". 97 counts up and finds 46 red hats. 99 and 98 didn't say "red", so his count is missing a red hat (it's on his head, he realizes). He says "red". 96 heard the "red" and now knows that there are an even number of "red" hats in front of 95. 96 sees 46, so he knows he has a "blue" hat. Etc...

Even if the assassin knows the plan, she can't thwart it. She hears the plan, but she still has to put the hats on their heads. The plan doesn't rely on any ordering of the hats, so the worst the assassin can do is to make sure #100 gets killed and that's the worst damage she can do.

9.

: a bad king has a cellar of 1000 bottles of delightful and very expensive wine. A neighbouring queen plots to kill the bad king and sends a servant to poison the wine. (un)fortunately the bad king's guards catch the servant after he has only poisoned one bottle. alas, the guards don't know which bottle but know that the poison is so strong that even if diluted 1,000,000 times it would still kill the king. Furthermore, it takes one month to have an effect. The bad king decides he will get some of the prisoners in his vast dungeons to drink the wine. Being a clever bad king he knows he needs to murder no more than 10 prisoners - believing he can fob off such a low death rate - and will still be able to drink the rest of the wine at his anniversary party in 5 weeks time.ProblemExplain how...

: I'll give you a hint. 1000 is less than 1024. If there were 1024 or more bottles of wine it would take more than 10 prisoners.SolutionNumber the bottles 1 to 1000, and write the number in binary format.

Now take your prisoner's 1 through 10 and let prisoner 1 take a sip from every bottle that has a 1 in its least significant bit. Let prisoner 10 take a sip from every bottle with a 1 in its most significant bit. Etc.

For instance, bottle #924 would be sipped by 10,9,8,5,4 and 3. That way if bottle #924 was the poisoned one, only those prisoners would die.

After four weeks, line the prisoners up in their bit order and read each living prisoner as a 0 bit and each dead prisoner as a 1 bit. The number that you get is the bottle of wine that was poisoned.

: to increase your chance of living, which prisoner would you want to be?Additional questionIf there were 1023 bottles, it wouldn't matter since everyone would have to take 512 sips. But there are 23 bottles less, so the people whose bits would have been on from 1001 to 1023 won't have to take a sip. 1001 is [11111 01001] in binary and 1023 is [11111 11111]. the most five significant bits are the most interesting because they would always be on from 1001 to 1023, so all those people are missing out on 23 bottles of wine that they otherwise would have had to drink. so in order to increase your chance of living, you'd probably want to be prisoner 6 to 10. (but depending on how the king determines who is least significant and who is most significant you could get shafted.)

note that if the king was really trying to kill the least number of prisoners, he should have let 999 prisoners each take a sip from their respective bottle numerically (if he had that many prisoners at his disposal). that way only one prisoner would die, and there's a chance of 1/1000 that no one would die, but then the puzzle isn't very fun.

10.

You have three jars that are all mislabeled. One contains peanut butter jelly beans, another grape jelly jelly beans, and the third has a mix of both (not necessarily a 50/50 mix, could be a 1/99 mix or a 399/22 mix). How many jelly beans would you have to pull out, and out of which jars, to find out how to fix the labels on the jars?

: 1 jelly bean from the p.b./grape jar will do the trick.SolutionThe trick here is to realize that every jar is mislabeled. Therefore you know that the peanut butter jelly bean jar is not the penut butter jelly bean jar, and the same goes for the rest.

You also need to realize that it is the jar labeled p.b./grape, labelled as the mix jar, that is your best hope. If you choose a jelly bean out of there, then you will know whether that jar is peanut butter or grape jelly jelly beans. it can't be the mix jar because i already said that every jar is mislabeled.

Once you know that jar 3 is either peanut butter, or grape jelly, then you know the other jars also. If it is peanut butter, then jar 2 must be mixed because it can't be grape (as it's labelled) and it can't be peanut butter (that's jar 3). Hence jar 1 is grape.

If jar 3 is grape, then you know jar 1 must be the mix because it can't be p.b. (as its labelled) and it can't be grape (that's jar 3). Hence jar 2 is peanut butter.

If you pick jelly beans from jar 1 or jar 2, then you would have to pick out

allof the jelly beans before you knew what that jar was. This is because jar 1 and 2 could be the mix, so in order to disprove that they were the mix, you would have to pull outeveryjelly bean just to make sure (since there could just be one bean of the opposite flavor in there).