Pregunta de entrevista de Google

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

Respuestas de entrevistas

Anónimo

4 de ene de 2012

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

8

Anónimo

1 de dic de 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

Anónimo

26 de nov de 2011

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

Anónimo

3 de dic de 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.