Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Develop an algorithm for finding the shortest distance between two words in a document. After the phone interview is over, take a few hours to develop a working example in C++ and send it to the manager.
| Tags: | c++ programming See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (10)
1 of 1 people found this helpful
Simple solution:
- Walk through the string and find each occurrence of word A. Store the middle index of this occurrence
- Walk through again and find each occurrence of word B. Store the middle index of this occurrence.
- Compare the arrays of indicies. Find the smallest difference between each middle position of A and each middle position of B.
- Running time = O(n + ab) where a and b are the number of occurrences of the input strings A and B.
Walk through the document, character by character, looking to match <space>word<space>. If we find such a match, put the index of the middle character into the array. Continue on our walk. Repeat for the second word. Now we have two arrays of integers. Our new task is to find the smallest difference between the elements in each array. Use the mergesort merge method, keeping track of the smallest difference. Return this distance.
Optimization:
Instead of walking one character at a time, when we find a mismatch, skip ahead by an intelligent number of characters – that is, to the next index at which we might find a good match. We can use the Boyer-Moore algorithm for this approach.
Further optimization:
Look for both words in one pass by using Boyer-Moore tables that account for both words. As we walk, keep track of the current shortest distance. Running time is O(n).
Does anyone have a simpler or clearer answer?
Helpful Answer?
Yes |
No
Inappropriate?
Perfect!
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
So here's my take on the algorithm:
1. Find the first instance of word A or B (assume A going forward). Continue searching (e.g. iterating) and each time you find another A, discard last recorded position of A until you reach and record the first B. (If B is the first word reverse A and B in the last sentence). The distance is the first try.
2. Continue searching until you reach the next instance of A or B. Measure the distance to the last recorded instance of the other word. If it's less than the current smallest distance, discard the last recorded instance of the word and record the distance.
3. Repeat 2. until the distance between the words found is greater than the current smallest distance.
4. Keeping track of the distance, start 1 again from current index until you find the next smallest distance or until you run out of words. Replace the recorded smallest distance each time you find one smaller.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 5 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
2 of 3 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
index2 = -1
minDistance = -1
Scan the string for words.
For each word:
1. If it matches 1st word, set index1 to index of word.
2. If it matches 2nd word, set index2 to index of word.
3. If it matches neither word, continue
4. If index1 == -1 or index2 == -1 continue
5. distance = abs( index1 - index2 )
6. minDistance = (minDistance == -1) ? distance : min( distance, minDistance )
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by neakor: