employer cover photo
employer logo
employer logo

Palantir Technologies

Is this your company?

Palantir Technologies interview question

How to write a function that determines the intersection (identical elements) of two arrays.

Interview Answer

Anonymous

May 4, 2011

1. Trivial solution: compare everything in nested loop leads to O(n^2) 2. Better solution is to sort one array first which allows to do binary search when stepping through the second array. This solution is dominated by the time it takes to sort the array and leads to O(nlogn). 3. Create hashtable on one of the arrays which allows for constant time lookups (if the hashtable is sized properly) and therefore an overall linear runtime O(n). I forgot to ask for clarification on how to deal with duplicates but I don't think that was a problem. Three similar style questions followed and it went pretty well. All questions had in common that they didn't require any specific knowledge other than basic data structures and algorithms. Usually there was an obvious trivial solution with bad runtime (e.g. O(n^2)) and the algorithm needed to be improved to get it down to at least O(nlogn) or even O(n).

5