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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 20 | Hard<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 />

SOLUTION<br />

pg 91<br />

We will assume for this question that <strong>the</strong> word order does not matter. This is a question you<br />

should ask your interviewer. If <strong>the</strong> word order does matter, we can make <strong>the</strong> small modification<br />

shown in <strong>the</strong> code below.<br />

To solve this problem, simply traverse <strong>the</strong> file <strong>and</strong> for every occurrence of word1 <strong>and</strong> word2,<br />

compare difference of positions <strong>and</strong> update <strong>the</strong> current minimum.<br />

1 int shortest(String[] words, String word1, String word2) {<br />

2 int pos = 0;<br />

3 int min = Integer.MAX_VALUE / 2;<br />

4 int word1_pos = -min;<br />

5 int word2_pos = -min;<br />

6 for (int i = 0; i < words.length; i++) {<br />

7 String current_word = words[i];<br />

8 if (current_word.equals(word1)) {<br />

9 word1_pos = pos;<br />

10 // Comment following 3 lines if word order matters<br />

11 int distance = word1_pos - word2_pos;<br />

12 if (min > distance)<br />

13 min = distance;<br />

14 } else if (current_word.equals(word2)) {<br />

15 word2_pos = pos;<br />

16 int distance = word2_pos - word1_pos;<br />

17 if (min > distance) min = distance;<br />

18 }<br />

19 ++pos;<br />

20 }<br />

21 return min;<br />

22 }<br />

To solve this problem in less time (but more space), we can create a hash table with each<br />

word <strong>and</strong> <strong>the</strong> locations where it occurs. We <strong>the</strong>n just need to find <strong>the</strong> minimum (arithmetic)<br />

difference in <strong>the</strong> locations (e.g., abs(word0.loc[1] - word1.loc[5])).<br />

To find <strong>the</strong> minimum arithmetic difference, we take each location for word1 (e.g.: 0, 3} <strong>and</strong><br />

do a modified binary search for it in word2’s location list, returning <strong>the</strong> closest number. Our<br />

search for 3, for example, in {2, 7, 9} would return 1. The minimum of all <strong>the</strong>se binary searches<br />

is <strong>the</strong> shortest distance.<br />

2 8 5<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!