Google Interview Question
1,069 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 an array of integers which is circularly sorted, how do you find a given integer.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (1)
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 2 people found this helpful
by this:
{
int& arr[SZ] = array_cir[SZ];
enum ArDir {UNK, UP, DN};
bool pivot_found = false;
ArrDir arr_dir = UNK;
int pivot_point = -1; // negative means undetermined
// first check if the integer is in the first three
....
// determine the direction and the pivot point
if (arr[1] <= arr[2] <= arr[3]) {
arr_dir = UP;
} else if (arr[1] >= arr[2] >= arr[3]) {
arr_dir = DN'
} else {
// pivot in the first 3 elements... determine by comparison
.....
pivot_point = X; // value found
}
for (int k = 0; k < N; k++) {
if (arr[k] == data) {
return (true);
if (dir == UP) {
if (arr[k] > data) {
return (false);
if (arr[k+1]) < arr[k]) {
pivot = k+1;
dir = CHANGE_DIR;
break;
}
}
// repeat for the other direction
if (pivout > 0) {
// loop to find the data and return
....
}
// check the end of array and return false
....
return false; // end of the road... did not find data
}