Google L4 Phone Screen Experience
Google Phone Screen Experience – Group Buddy Strings
I recently had a Google phone screen and wanted to share the question that was asked.
Problem
Two strings are called buddy strings if the distance between every pair of consecutive characters is the same (cyclic).
Examples:
aaaandzzzare buddies.abcandxyzare buddies.aaaandzzyare not buddies.
The interviewer then asked me to group all buddy strings together.
Function signature:
vector<vector<string>> solve(vector<string>& strs);My Initial Approach
I converted every string into a canonical hash representing the differences between consecutive characters.
Examples:
abc -> 0#1#1
bcd -> 0#1#1
xyz -> 0#1#1
aaa -> 0#0#0
zzz -> 0#0#0
abd -> 0#1#2For every string:
- First character contributes
0. - Every subsequent character contributes the difference from the previous character.
- Strings with the same hash belong to the same group.
I stored them in an unordered_map<hash, vector<string>>.
Time Complexity: O(n × k)
Space Complexity: O(n)
(where n is the number of strings and k is the maximum string length).
First Follow-up
The interviewer asked:
How would you handle single-character strings like
[a],[b],[c]?
I initially answered that all of them should belong to the same group because there are no consecutive characters to compare.
The interviewer clarified that they should be placed in different groups.
I then asked whether [a], [a], [a] should still belong to the same group, and he said yes.
So I modified the solution to use the character itself as the key for strings of length 1.
Second Follow-up
The interviewer then asked:
How will
abcandyzabe handled?
I explained that the differences should be computed cyclically:
(str[i] - prev + 26) % 26For example,
a -> b = 1
y -> z = 1
b -> c = 1
z -> a = 1So both strings generate the same hash and are grouped together.
I also explained why adding 26 before taking modulo avoids negative values.
Final Follow-up
The interviewer asked whether there were any other corner cases.
I mentioned handling special characters.
He then asked me to modify the code accordingly.
Assuming the alphabet becomes the full ASCII character set with cyclic distance, I changed the difference calculation to:
(str[i] - prev + 256) % 256After making that change, the interview ended.
I'm curious how others would have handled the single-character follow-up and the special-character extension. Would you have approached either of those differently?
What are the chances of getting a callback?


