Google Interview Question
1,224 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Given two sorted integer arrays, write an algorithm to get back the intersection.
| Tags: | algorithm See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
1 of 1 people found this helpful
{
vector<int> 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;
}
Helpful Answer?
Yes |
No
Inappropriate?
0 of 3 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
5 of 7 people found this helpful
if( ar1[i] == ar2[j] )
System.out.println( ar1[i]);
if( ar1[i] < ar2[j])
i++;
else if ( ar1[i] > ar2[j])
j++;
else {
i++; j++;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 1 people found this helpful
by Interview Candidate: