Sollicitatievraag bij Google

Given two sorted integer arrays, write an algorithm to get back the intersection.

Antwoorden op sollicitatievragen

Anoniem

4 jan 2012

for(int i=0,j=0; i ar2[j]) j++; else { i++; j++; } }

8

Anoniem

1 dec 2011

vector SortedIntersection(int *a, int an, int *b, int bn) { vector result; // assuming asscending order. int *aend = a + an; int *bend = b + bn; while (a != aend && b != bend) { int candidate = *a; ++a; while (*b < candidate && b != bend) { ++b; } if (b != bend && candidate == *b) { result.push_back(candidate); ++b; } } return result; }

1

Anoniem

26 nov 2011

Should clarify what intersection means, after that it's pretty straightforward

Anoniem

3 dec 2011

It can be solved in O(nlogn) . For each number in the 1st array, do a binary search in the second one, if found - try comparing the two subsets.