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.

Chapter 20 | Hard<br />

20.1 Write a function that adds two numbers. You should not use + or any arithmetic operators.<br />

________________________________________________________________pg 279<br />

20.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle - in o<strong>the</strong>r words,<br />

each 52! permutations of <strong>the</strong> deck has to be equally likely. Assume that you are given<br />

a r<strong>and</strong>om number generator which is perfect.<br />

________________________________________________________________pg 281<br />

20.3 Write a method to r<strong>and</strong>omly generate a set of m integers from an array of size n. Each<br />

element must have equal probability of being chosen.<br />

________________________________________________________________pg 282<br />

20.4 Write a method to count <strong>the</strong> number of 2s between 0 <strong>and</strong> n.<br />

________________________________________________________________pg 283<br />

20.5 You have a large text file containing words. Given any two words, find <strong>the</strong> shortest<br />

distance (in terms of number of words) between <strong>the</strong>m in <strong>the</strong> file. Can you make <strong>the</strong><br />

searching operation in O(1) time? What about <strong>the</strong> space complexity for your solution?<br />

________________________________________________________________pg 285<br />

20.6 Describe an algorithm to find <strong>the</strong> largest 1 million numbers in 1 billion numbers. Assume<br />

that <strong>the</strong> computer memory can hold all one billion numbers.<br />

________________________________________________________________pg 286<br />

20.7 Write a program to find <strong>the</strong> longest word made of o<strong>the</strong>r words in a list of words.<br />

EXAMPLE<br />

Input: test, tester, testertest, testing, testingtester<br />

Output: testingtester<br />

________________________________________________________________pg 287<br />

20.8 Given a string s <strong>and</strong> an array of smaller strings T, design a method to search s for each<br />

small string in T.<br />

________________________________________________________________pg 288<br />

20.9 Numbers are r<strong>and</strong>omly generated <strong>and</strong> passed to a method. Write a program to find<br />

<strong>and</strong> maintain <strong>the</strong> median value as new values are generated.<br />

________________________________________________________________pg 290<br />

20.10 Given two words of equal length that are in a dictionary, write a method to transform<br />

one word into ano<strong>the</strong>r word by changing only one letter at a time. The new word you<br />

get in each step must be in <strong>the</strong> dictionary.<br />

EXAMPLE<br />

9 1<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Additional Review Problems

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

Saved successfully!

Ooh no, something went wrong!