Location - Google Ireland
YOE - 5 years
Role - SWE
I had a Google phone interview on 7th Sept 2021. The question asked was how to get maximum mutual friend suggestion on a social media.
For example, you should see a friend suggestion with maximum mutual friends.
I implemented it using BFS, by using a frequency map.
Node getMutualFriend(Node root){
if(root == null || root.getFriends() == null){
return root;
}
HashMap<Node, Integer> freqmap = new HashMap<>();
Deque<Node> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
Node node = queue.poll();
freqmap.put(node, freqmap.getOrDefault(node, 0 ));
for(Node friend : node.getFriends()){
queue.offer(friend);
}
}
// logic to get maximum count mutual friend
return freqmap.get(maxfriend);
}