Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Solutions</strong> to Chapter 5 | Bit Manipulation<br />
5.7 An array A[1...n] contains all <strong>the</strong> integers from 0 to n except for one number which is<br />
missing. In this problem, we cannot access an entire integer in A with a single operation.<br />
The elements of A are represented in binary, <strong>and</strong> <strong>the</strong> only operation we can use<br />
to access <strong>the</strong>m is “fetch <strong>the</strong> jth bit of A[i]”, which takes constant time. Write code to<br />
find <strong>the</strong> missing integer. Can you do it in O(n) time?<br />
SOLUTION<br />
pg 58<br />
Picture a list of binary numbers between 0 to n. What will change when we remove one<br />
number? We’ll get an imbalance of 1s <strong>and</strong> 0s in <strong>the</strong> least significant bit. That is, before removing<br />
<strong>the</strong> number k, we have this list of least significant bits (in some order):<br />
0 0 0 0 0 1 1 1 1 1 OR 0 0 0 0 0 1 1 1 1<br />
Suppose we secretly removed ei<strong>the</strong>r a 1 or a 0 from this list. Could we tell which one was<br />
removed?<br />
remove(0 from 0 0 0 0 0 1 1 1 1 1) --> 0 0 0 0 1 1 1 1 1<br />
remove(1 from 0 0 0 0 0 1 1 1 1 1) --> 0 0 0 0 0 1 1 1 1<br />
remove(0 from 0 0 0 0 0 1 1 1 1) --> 0 0 0 0 1 1 1 1<br />
remove(1 from 0 0 0 0 0 1 1 1 1) --> 0 0 0 0 0 1 1 1<br />
Note that if 0 is removed, we always wind up with count(1) >= count(0). If 1 is removed, we<br />
wind up with count(1) < count(0). Therefore, we can look at <strong>the</strong> least significant bit to figure<br />
out in O(N) time whe<strong>the</strong>r <strong>the</strong> missing number has a 0 or a 1 in <strong>the</strong> least significant bit (LSB).<br />
If LSB(missing) == 0, <strong>the</strong>n we can discard all numbers with LSB = 1. If LSB(missing) == 1, we<br />
can discard all numbers with LSB = 0.<br />
What about <strong>the</strong> next iteration, with <strong>the</strong> second least significant bit (SLSB)? We’ve discarded<br />
all <strong>the</strong> numbers with LSB = 1, so our list looks something like this (if n = 5, <strong>and</strong> missing = 3):<br />
00000<br />
00001<br />
00010<br />
-----<br />
00100<br />
00101<br />
00110<br />
00111<br />
01000<br />
01001<br />
01010<br />
01011<br />
01100<br />
01101<br />
Our SLSBs now look like 0 0 1 0 1 0. Using <strong>the</strong> same logic as we applied for LSB, we can figure<br />
out that <strong>the</strong> missing number must have SLSB = 1. Our number must look like xxxx11.<br />
Third iteration, discarding numbers with SLSB = 0:<br />
00000<br />
00001<br />
00010<br />
-----<br />
00100<br />
00101<br />
00110<br />
00111<br />
01000<br />
01001<br />
01010<br />
01011<br />
01100<br />
01101<br />
We can now compute that count(TLSB = 1) = 1 <strong>and</strong> count(TLSB = 1) = 1. Therefore, TLSB = 0.<br />
We can recurse repeatedly, building our number bit by bit:<br />
1 4 1<br />
<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Concepts <strong>and</strong> Algorithms