How to read cracking the coding interview

What is the probability that it will take each branch? 1/2. »What is the probability that it will reach a particular leaf node? 1/2 A j (for some j). »What is the probability that it returns 3 (for example)? We could calculate this by summarizing the probabilities that each how to read cracking the coding interview leaf node will reach a value of 3. Each of these paths has probability 1/2 A j, then we know that the total probability that 3 returns must be a series in terms of reciprocal powers 2 (that is, 1/2 A x + 1/2 A y + 1/2 A z + ...).

However, we also know that the probability of 3 returns must be 1/3 (because rand3 () it should be completely random). Can you get a set of 2 reciprocal powers equal to 1/3? No, because 3 and 2 are relative primes. Similarly we can conclude that we have to accept a small problem (infini¬) to solve this problem little chance) that this process will repeat itself forever.

It's okay. So how do we solve this? To generate a random number between 1 and 7, we just need to evenly generate range more than what we are looking for and then repeatedly sample until we get that number We like. We will generate a lower number of 5 with two places with two calls to the RNG. An integer to integer hash map is an easy and effective (time) solution.


Seo al¬ the gorithm works by flying through the matrix. For each element of x, see the sum of -x in the hash table and, if present, print (x, sum - x). Add an x to the hash table and go to the next item. Alternative solution Definition of complementarity: if we try to find a pair of numbers equal to z, it is the total number z-x is x ment (that is, the number that can be added to x to make z). For example,


If we want to find a pair of numbers that is equal to 12, 17 would have an ending of -5. The algorithm: Suppose we have the following ordering set: {-2-1 0 3 5 6 7 9 13 14}. Come first point to the end of the array and the last point to the end of the array. To get the complement at first, we just move back until we find it. If the first + last sum, then there is no waist plementar first. So we can move on first.


We stop when the first one is bigger than the previous one. Why does this have to get all the endings first? Because the matrix is ordered and we are trying gradually smaller numbers. When the sum of the first and last is less than the sum, we know trying an even smaller number (as it was the last one) won't help us complete it. Why does this have to have all the endings at the end? Because each pair must consist of a first and another latest. We got all the endings first, so we got all the endings last. To investigate this issue, we begin by gaining a deeper understanding of how we contribute numbers. We will work in Base 10 to make it easier to see.


Normally 759 + 674.1 would add digits [0] of each number, carry the head, put digitfl] of each number, carry the one, etc. You can take the same approach in binary: add all the digits and carry the one as needed. Can we make this a little easier? Yes! Imagine I fixed the "reload" and Steps of "behavior". That is, I do the following: 1. Add 759 + 674, but bring "forget". So I get 323. 2. Add 759 + 674 but do the

behavior, instead of adding all the digits. then I get 1110. Now how do we do this in binary? 1. If I combine two binary numbers but forget to carry them, then bits [i] 0 if bits [i] in ay b are 0 or both 1. This is XOR. 2. If I combine two numbers but do not carry them, I will have bit 1 in [i] if bit [i-1] in a and b both are one. This is the AND, translated.