Google Interview Question
1,223 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:
How to find the max number of a shift buffer queue. For instance, there is an array like 5, 6,8,11,1,2, how to find the max number, which is 11 in this example.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (10)
0 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
int max = list[0];
for(int i = 1; i < list.length; i++)
{
if (i > max)
{
max = i;
}
}
return max;
Helpful Answer?
Yes |
No
Inappropriate?
2 of 3 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 3 people found this helpful
int max = arr[0];
for (int i =0; i<arr.length; ++i){
if (arr[i] < max){
return arr[i];
}
else{
max = arr[i];
}
}
return max;
Helpful Answer?
Yes |
No
Inappropriate?
bool searchforcyclearray(int cyclearray[],int index, int beginning, int end, int &res,int &time)
{
int mid=0;
time++;
if(beginning+1==end){
if(cyclearray[beginning]==index) {
res = beginning;
return(true);
}
else if(cyclearray[end]==index) {
res = end;
return(true);
}
else return(false);
}
mid = (int)((beginning+end)/2);
int a,b,c;
a = cyclearray[beginning];
c = cyclearray[end];
b = cyclearray[mid];
if(a<b&&b<c){
if(index<b) {
if(a<index) return(searchforcyclearray(cyclearray,index,beginning,mid,res,time));
else if(a==index) {
res = beginning;
return(true);
}
else return(searchforcyclearray(cyclearray,index,mid,end,res,time));
}
else if(index>b) return(searchforcyclearray(cyclearray,index,mid,end,res,time));
else {
res = mid;
return(true);
}
}
else if(a>b&&b<c){
if(index<b) return(searchforcyclearray(cyclearray,index,beginning,mid,res,time));
else if(index>b) {
if(index<c) return(searchforcyclearray(cyclearray,index,mid,end,res,time));
else if(index==c){
res = end;
return(true);
}
else return(searchforcyclearray(cyclearray,index,beginning,mid,res,time));
}
else {
res = mid;
return(true);
}
}
else if(a<b&&b>c){
if(index<b) {
if(a<index) return(searchforcyclearray(cyclearray,index,beginning,mid,res,time));
else if(a==index) {
res = beginning;
return(true);
}
else return(searchforcyclearray(cyclearray,index,mid,end,res,time));
}
else if(index>b) searchforcyclearray(cyclearray,index,mid,end,res,time);
else {
res = mid;
return(true);
}
}
else return(false);
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
I think something like this would work (first call between the first and last indices of the array):
int binarySearchCircularBuffer(int[] buffer,int start,int end){
if(buffer[start]<buffer[end])
return buffer[end];
int middle = (start-end)/2;
if(buffer[middle]>buffer[start])
return binarySearchCircularBuffer(buffer,middle,end);
else
return binarySearchCircularBuffer(buffer,start,middle);
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 3 people found this helpful
by Interview Candidate: