Google Interview Question
1,223 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:
Given inputs from Google Search, you have K chunks. Each chunk is individually alphabetically ordered (apple, banana, cat) ... (*apple, *banan, *cat). You want to merge all chunks into a single list. How would you do it? What limitations are there to your approach? It's on an x86 processor, what does that mean and how does that affect your approach?
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
0 of 1 people found this helpful
2) Pop the highest value from each chunk and store it in the heap along with the chunk index.
3) call findMin on the heap to get the node with the smallest key and it's chunk index
4) insert the key string in the empty result list
5) pop the next value from the chunk where the key string came from using the chunk index, insert it into the heap and repeat step 5. If there are no items left in the chunk just go to step 5.
When all chunks are exhausted, you are done.
6)
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 1 people found this helpful
by Liron:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MergeString {
public static List<Character> merge(List<String> words){
List<Character> list = new LinkedList<Character>();
ArrayList<ArrayList<Character>> chWords = new ArrayList<ArrayList<Character>> ();
for (String str : words){
List<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()){
chars.add(c);
}
chWords.add((ArrayList<Character>) chars);
}
while (true){
int loc = findMin (chWords);
if (loc != -1 ){
list.add(chWords.get(loc).get(0));
List<Character> remove = chWords.get(loc);
remove.remove(0);
chWords.set(loc,(ArrayList<Character>) remove);
}
else{
break;
}
}
return list;
}
public static int findMin(ArrayList<ArrayList<Character>> words){
int min = -1;
boolean firstTime = true;
for (int i=0; i<words.size(); ++i){
if (words.get(i) != null && !words.get(i).isEmpty() && firstTime){
min = i;
firstTime = false;
}
else if (words.get(i) != null && !words.get(i).isEmpty() && words.get(i).get(0) < words.get(min).get(0)){
min = i;
}
}
return min ;
}
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
list.add("aelpp");
list.add("aaabnn");
list.add("act");
List<Character> list2 = merge(list);
for (Character c : list2){
System.out.println(c);
}
}
}