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 whose elements are sorted, return the index of a the first occurrence of a specific integer. Do this in sub-linear time. I.e. do not just go through each element searching for that element.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
Binary search is the way to go, but you do need a slight modification, coz a simple binary search may not necessarily return the index of the first occurence, e.g. given input array
7777777777, what would a simple search return?
The trick would be to seek to the left if the index at the middle of the current partition is the element you are looking for.
The performance would depend on how many duplicates there are on average + log n
If this average is n as in the above example you are looking at O(n/2), but then if you knew that already you could simply return the first element :)
Anyone have a better idea?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by Sid: