Sometimes it seems to me that these types of puzzles are more about testing someone's background than problem solving skills. Some of you might know about this: IBM runs a monthly puzzle challenge (google 'IBM ponder this') where every month they post a puzzle/problem and people send solutions. Some of the puzzles seem technical or just not appealing, so I don't bother every month, but many are quite amusing. And I also feel that often it depends on 'background knowledge'. Two cases to illustrate:Yeah in an interview, I don't think anyone expects you to get as far as proving the strategy is optimal. But for the level that these interviews are testing for, being able to find the solution (typically at least with a couple hints) and analyze the result (i.e., show what the probability of success is--which just requires some discrete math/probability) is reasonable to expect. I saw this problem in a software dev (new grad) interview in SF/south bay area (like Google, Dropbox, FB, etc. but not actually one of those 3). I mean if companies are offering 120k+ salary to new grads, the interviewees better be smart and so there will usually be some tougher questions to gauge that.
Personally I'm not sure if I like this problem as an interview problem. Most people I've shown this problem to end up having more of an "aha!" moment. There are definitely reasonable mathematical/algorithmic approaches to apply & reach the solution, but I find it difficult to give hints that aren't too strong while still being helpful.
I know. And problem 2) is supposedly harderHoly shit.
Googled it, and I have to say, I would literally have never figured that out in a million years. I don't understand how the optimal strategy is any better than picking lockers at random.
Sometimes it seems to me that these types of puzzles are more about testing someone's background than problem solving skills. Some of you might know about this: IBM runs a monthly puzzle challenge (google 'IBM ponder this') where every month they post a puzzle/problem and people send solutions. Some of the puzzles seem technical or just not appealing, so I don't bother every month, but many are quite amusing. And I also feel that often it depends on 'background knowledge'. Two cases to illustrate:
1) Last month was a nice problem, basically a 2-player game. I was trying to formulate a hypothesis based on a couple examples and prove by induction, but it didn't work (the hypothesis was wrong, it turns out there's only a conjecture for the sequence, so induction has very slim to no chance of success). Ultimately, I couldn't solve the problem. Looking at the solution, they noticed that this is a game theory problem whose solution is described by Sprague-Grundy theorem. If you know this theorem, it's not hard to solve the problem. If you don't, unless you're a Sheldon Cooper type of a guy, it's gonna be very tough to solve it from scratch.
2) In June 2012 they also had a nice problem, and I recognized it as an example of Nim game. Nim game has a neat solution, you can check on wiki, and it's easy to apply it to IBM's problem. But if you look at how algorithm for solving Nim goes, you'll see that it's not something that one would come up with easily. If I had not known about Nim, I wouldn't have been able to solve it.
This brings me to the problem 2). I still don't see how to move from 1 as the worst case, and I tried a couple approaches, but they don't work for all inputs. Nor can I see how XOR would help. But, related to the above digression, I can't help to feel that someone well versed in Shannon-type of information theory and theorems would be much better equipped to tackle this.
I would have never figured it out either, and was going for 0.0000000x or so, so that it could easily be shown by experimentation (that would still be an amazing improvement v. random). I still haven't figured it out. I just thought of it as something different to try, and the code is pretty short and easy.Holy shit.
Googled it, and I have to say, I would literally have never figured that out in a million years. I don't understand how the optimal strategy is any better than picking lockers at random.
This brings me to the problem 2). I still don't see how to move from 1 as the worst case, and I tried a couple approaches, but they don't work for all inputs. Nor can I see how XOR would help. But, related to the above digression, I can't help to feel that someone well versed in Shannon-type of information theory and theorems would be much better equipped to tackle this.
I still don't get that, though--Explained a different way, say I'm prisoner 1. I'm looking for the number 1. Locker 1 (in the graph) is a member of exactly one cycle. Thus as long as I start on vertex of this cycle, I will eventually find the number 1. Why? Because SOME vertex in the cycle MUST contain the number 1! (By the definition of cycle.) The only locker that prisoner 1 knows is on the cycle he's looking for is locker 1. He could pick randomly, but then he'd have to be lucky enough to find another member of his cycle (which could be impossible if locker 1's cycle has size 1).
I still don't get that, though--how, with the lockers randomized, his own number has a better chance. FI, #1's search could very well go like so: 1=5, 5=12, 12=90, 90=90--notice, that cycle does not contain his number--or like so: 8=53, 53=2, 2=8; or like so: 72=50, 50=3, 3=1. However, randomizing the first locker makes the chances go to pot, again.
Your example is not possible. Lockers 12 and 90 both contain 90.
Since each number appears only once, only the first locker opened can contain its own number.
I still don't get that, though--how, with the lockers randomized, his own number has a better chance. FI, #1's search could very well go like so: 1=5, 5=12, 12=90, 90=90--notice, that cycle does not contain his number--or like so: 8=53, 53=2, 2=8; or like so: 72=50, 50=3, 3=1. However, randomizing the first locker makes the chances go to pot, again.
Regarding problem 1:
I don't understand how prisoner i can guarantee that cycle i is on the same cycle as the locker with the number i inside.
EDIT: It appears Cerb has the same problem as me
Being shuffled, all lockers and their numbers can be treated as having been swapped. Having been swapped, no matter how matter how many times they've been swapped, they will always create cycles. Also, due to the equivalence to being swapped, no cycles will cross paths. Each path taken will either be a subset of some cycle, or never make it to detecting the cycle.
This is what I don't get.Actually, Cerb's concern is different from yours.If I start in locker i, by traveling along the cycle, I will eventually get back to locker i. This means that some other locker contains i. Hence by starting at locker i, inmate i is guaranteed to find his number (although it might need more than 50 tries if the cycle is longer than 50).
This is what I don't get.
I get that if I start at locker i, I will eventually get back to locker i.
I get that some other locker contains i.
I don't get how the cycle starting at locker i is guaranteed to have i inside one of the lockers.
Oooooooh!!!!!!!!!
But wouldn't that mean for a cycle length > N/2, the chance of finding your own number be 0? Because your number will always be the last number you get before you go back to your own locker, so if the cycle has 51 lockers and you only get to open 50...
Yes, it does mean that, and this is used to calculate the odds: otherwise you'd need to consider cycles of length>N/2 as well in the calculation.
You should. If independent, it would be 0.5^100, but they can arrive with a strategy that gives them >30% chance.I didn't read the above solutions. They all die.
So that means that the probability involved is really the probability of there being at least one chain longer than 50 lockers?
You should. If independent, it would be 0.5^100, but they can arrive with a strategy that gives them >30% chance.
I would never think of XOR here, nor a way similar to puzzle 1, but I can think of a different and very simple approach: The sum of all numbers 1 to N-1 is T = N*(N-1)/2. Now, do a single pass through the list to get their sum S. The duplicate is S-THere's a similar problem that uses the same technique: say I give a list of N numbers in the range [1,N-1] with exactly 1 duplicate (i.e., each number is in there at least once). Find me the duplicate in linear time with constant memory (so you cannot sort & binary search; you also cannot hash). One approach is to XOR all the numbers in the list together, then XOR the result with all the numbers in the range [1,N-1]. Since x^x = 0, and the duplicate is the only number that would be XOR'd an odd number of times, you'll find it.
But can you think of a way to do it using an approach similar to the solution to puzzle 1?
I didn't refer to Shannon exclusively, but generally some sort of information theory background. Like Hamming distanceAs for the 2nd problem,
The general form of this problem is given a (2^N - 1)-bit message from alice to bob, how many bits of information can you transmit to Bob by flipping at most 1 bit of Alice's msg?
Comment: I don't think either of Shannon's main theorems can be applied to this. They dealt with compression bounds and noisy channels. Maybe you can do something with the compression bound... not sure. That said, I've had minimal exposure to information theory so I really don't know. Regardless, anyone who's seen info theory at all before should probably guess the answer is related to log2(numBits), which is accurate; see next spoiler box. Not that knowing this helps you much...
Hint 1: The answer is N bits, i.e., log2(numBits + 1). So with Alice sending a 31 bit msg, I can always send 5 bits.
They can communicate about anything *before* they are taken to the 'locker opening' phase. I'll repeat the example I already gave for N=2 and opening 1 box: they can decide that inmate 1 opens box 1, and inmate 2 opens box 2. No communication is needed after that, and their chance of survival is 50%, because if inmate 1 passes (50% chance), inmate 2 will pass too. If independent, it would be 0.5^2 = 25%.If they can communicate yes. They can't communicate so no.
Edit:
This is what I don't get.
I get that if I start at locker i, I will eventually get back to locker i.
I get that some other locker contains i.
I don't get how the cycle starting at locker i is guaranteed to have i inside one of the lockers.
Also:
So when you say that the inmates have a 30% chance, is that solely due to having cycles longer than N/2? i.e. if all the cycles are shorter than N/2, is the chance of success 100?
Yes, it does mean that, and this is used to calculate the odds: otherwise you'd need to consider cycles of length>N/2 as well in the calculation.
So that means that the probability involved is really the probability of there being at least one chain longer than 50 lockers?
Then the probability of success is 1 - Probability(there exists a cycle of length > N/2). In the start of my last post, I showed that number of arrangements that have a cycle of length k > N/2 is n!/k. The number of total arrangements is n!. So the probability of having a cycle of length k > n/2 is:Ok, so now that we know that each locker is a member of exactly one cycle, under what conditions can the prisoners always succeed? Well, if there are NO cycles with size greater than N/2, the prisoners will always win. Why?
Nope. You can find a strategy where they succeed with a double digit percentage of success. For any problem size even (as long as it's N prisoners opening N/2 lockers, N even.)I didn't read the above solutions. They all die.