Amazon Interview Question

given a method private LinkedList doAction(List l1, List l2) { LinkedList ret = new LinkedList(); for (int i = 0; i < l1.size(); i++) { for (int j = 0; j < l2.size(); j++) { if (l1.get(i) == (l2.get(j))) { if (!ret.contains(l1.get(i))) { ret.add(l1.get(i)); } } } } return ret; } questions: * what does the method do * what is bigO for the method * how to improve the method * how to test the method

Interview Answer

Anonymous

Sep 6, 2015

* compares 2 lists and returns it's intersection * O(n^2) - that was wrong, they gave me a hint that get() method of LinkedList works in O(n) time. * use the HashMap instead of LinkedList * make a unit test