Pergunta de entrevista da empresa Google

Find the intersection of two integer lists

Respostas da entrevista

Sigiloso

7 de abr. de 2012

why do you need this big code? Try to be simple while explaining simple ideas.. 1) add list1 elements to hash. 2) iterate through list2. check if the item is present in hash and add to output list. 3) return output list. Best way is to leave it here and ask politely if he wants to code this simple naive idea? Over explaining simple things will cast impression of small knowledge.

5

Sigiloso

14 de fev. de 2012

Share a blog on this topic: http://codercareer.blogspot.com/2011/11/no-24-intersection-of-sorted-arrays.html

1

Sigiloso

14 de fev. de 2012

I tried to implement it using HashTable technique public ArrayList intersection(int list1[], int list2[]) { int i, max = 0, min = 0; Hashtable hash; ArrayList list = new ArrayList(); if (list1.length > list2.length) { hash = new Hashtable(list1.length, list1.length); max = list1.length; min = list2.length; } else { hash = new Hashtable(list2.length, list2.length); max = list2.length; min = list1.length; } for (i = 0; i < max; i++) { if (max == list1.length) hash.put(list1[i], list1[i]); else hash.put(list2[i], list2[i]); } for (i = 0; i < min; i++) { if (min == list1.length) { if (hash.put(list1[i], list1[i]) != null) { list.add(list1[i]); } } else { if (hash.put(list2[i], list2[i]) != null) { list.add(list2[i]); } } } return list; }