27.10.2014 Views

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

SHOW MORE
SHOW LESS

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.3 Given an integer, print <strong>the</strong> next smallest <strong>and</strong> next largest number that have <strong>the</strong> same<br />

number of 1 bits in <strong>the</strong>ir binary representation.<br />

SOLUTION<br />

pg 58<br />

The Brute Force Approach:<br />

An easy approach is simply brute force: count <strong>the</strong> number of 1’s in n, <strong>and</strong> <strong>the</strong>n increment<br />

(or decrement) until you find a number with <strong>the</strong> same number of 1’s. Easy - but not terribly<br />

interesting. Can we do something a bit more optimal? Yes!<br />

Number Properties Approach for Next Number<br />

Observations:<br />

»»<br />

If we “turn on” a 0, we need to “turn off” a 1<br />

»»<br />

If we turn on a 0 at bit i <strong>and</strong> turn off a 1 at bit j, <strong>the</strong> number changes by 2^i - 2^j.<br />

»»<br />

If we want to get a bigger number with <strong>the</strong> same number of 1s <strong>and</strong> 0s, i must be bigger<br />

than j.<br />

Solution:<br />

1. Traverse from right to left. Once we’ve passed a 1, turn on <strong>the</strong> next 0. We’ve now increased<br />

<strong>the</strong> number by 2^i. Yikes! Example: xxxxx011100 becomes xxxxx111100<br />

2. Turn off <strong>the</strong> one that’s just to <strong>the</strong> right side of that. We’re now bigger by 2^i - 2^(i-1)<br />

Example: xxxxx111100 becomes xxxxx101100<br />

3. Make <strong>the</strong> number as small as possible by rearranging all <strong>the</strong> 1s to be as far right as possible:<br />

Example: xxxxx101100 becomes xxxxx100011<br />

To get <strong>the</strong> previous number, we do <strong>the</strong> reverse.<br />

1. Traverse from right to left. Once we’ve passed a zero, turn off <strong>the</strong> next 1. Example:<br />

xxxxx100011 becomes xxxxx000011.<br />

2. Turn on <strong>the</strong> 0 that is directly to <strong>the</strong> right. Example: xxxxx000011 becomes<br />

xxxxx010011.<br />

3. Make <strong>the</strong> number as big as possible by shifting all <strong>the</strong> ones as far to <strong>the</strong> left as possible.<br />

Example: xxxxx010011 becomes xxxxx011100 .<br />

And now, for <strong>the</strong> code. Note <strong>the</strong> emphasis on pulling common code out into a reusable function.<br />

Your interviewer will look for “clean code” like this.<br />

1 3 5<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Concepts <strong>and</strong> Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!