Tripadvisor Interview Question

I was asked to write a program to find common items between 2 lists.I used java to write the program. I wrote the program which was iterating the smaller array list and used contains. The Hiring manager argues with me saying that the code is not efficient enough because 'contains' method on arraylist would lead to o(n) and that would result in O(m*N)(which is incorrect) In the interview, I was not able to google it and I was taken aback when he was talking so assertively as I assumed that he understands java. However, after I hung up the phone I realized that I am indeed right and that the algorithm I created was very efficient. The interviewer came to the interview with a view to reject me. Its a waste of time. They should let subject mattter experts do the interviews, not the people with background in c/C++ interview java guys or the other way round. The hiring manager seemed very professorial background. I tried to tell recruiter to let them know the problem, but I do not think even she has any clue on how to approach.

Interview Answers

Anonymous

Apr 9, 2015

If it's not sorted, put the smaller list into a Set. If it's sorted, use two pointers to go over the two list.

4

Anonymous

Jul 14, 2015

Is this review someone trolling? I find it hard to believe that you could apply for a senior software java position and not know the runtime of List.contains. And on top of that I assume you went home and googled the runtime for Set.contains and then got all smug about being right. Sounds like a no hire was a good decision.

1

Anonymous

Jan 28, 2018

If it's not sorted, better put the larger list into a Set. Set.contains complexity is O(log N) => if N > M AND N, M are large enough, (log N) * M < (log M) * N (eventually). Tiny anyway.

Anonymous

Jul 28, 2018

Interviewer was indeed right. Use a HashSet and add elements of smaller list in O(n) time. Now iterate the bigger list and find if the element is in set or not, report common elements. This is O(m+n) time and O(n) space , given m>n.