Google Interview experience
1 round over 4 weeks. No offer, meh experience.
The interview kicked off with the interviewer introducing himself over Google Meet. We got started as soon as I opened up the interview document.
Problem:
Given a sorted array of strings and a prefix string, count how many elements in the array begin with the given prefix.
Input:
array = ["apple", "applet", "banana", "berry", "candy", "cane", "carrot", "date", "eggplant", "fig", "grape"]
prefix = "ca"Output:
The expected output would be 3, since the elements "candy," "cane," and "carrot" all start with the prefix "ca."
Solution:
I initially suggested a linear approach with a time complexity of O(n * k) (where n is the length of the array and k is the length of the prefix).
Interview Experience:
The interviewer was looking for a more efficient solution. I then proposed using binary search to find one element that matched the prefix and then using two pointers to count the number of such elements. Unfortunately, this approach still had a worst-case time complexity of O(n * k), which wasn't ideal.
The interviewer hinted that I should use binary search to find both the start and end points of the range where elements match the prefix. I coded two binary search functions: one to find the leftmost occurrence and another for the rightmost.
The interviewer was pleased with the first binary search (for the left end). However, I missed a condition in the second binary search (for the right end), and with the interview time running out (it was 45 minutes in), I couldn’t solve it in time.
Afterward, the interviewer provided feedback. He mentioned that my communication was good, but I needed to be more open-minded about the problem-solving approach.
Optimal implementation:
def binary_search_left(array, prefix):
low, high = 0, len(array)
while low < high:
mid = (low + high) // 2
if array[mid] < prefix:
low = mid + 1
else:
high = mid
return low
def binary_search_right(array, prefix):
low, high = 0, len(array)
while low < high:
mid = (low + high) // 2
if array[mid] <= prefix:
low = mid + 1
else:
high = mid
return low
def count_elements_with_prefix(array, prefix):
# Define the prefix end
prefix_end = prefix[:-1] + chr(ord(prefix[-1]) + 1)
# Use binary search to find the left and right boundaries
left_index = binary_search_left(array, prefix)
right_index = binary_search_right(array, prefix_end)
# The count of elements with the given prefix
count = right_index - left_index
return count
array = ["apple", "applet", "banana", "berry", "candy", "cane", "carrot", "date", "eggplant", "fig", "grape"]
prefix = "ca"
print(count_elements_with_prefix(array, prefix)) # Output: 3Time Complexity:
Since we perform binary searches twice (once for the left boundary and once for the right boundary), and each search operation is O(log n), the total time complexity of the count_elements_with_prefix function is: O(log n) + O(log n) = O(log n)
The next day, the HR called to let me know that the interviewer decided not to move forward with my application but said I could reapply in a year.
Know someone interviewing at Google? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.