So, I had my L3 onsite R2 interview last week. So, there was main question with two follow ups.
Main Question:
You are given a list of conversation in format of:
Conversation(timestamp, senderId, receiverId, message)
You need to need return the userID who is involved in the most number of conversations. Return any userID which is most active one.
My Approach : Use a map of String,Integer to check which userId which are most active per count.
Follow up 1 : Now, if a conversation between userID : A and userId : B has occurred previously, then that acts out as a one single conversation.
So, if A -> B or B -> A have conversed previously, the conversation count shouldn't increment for A and B for this case.
Only unique conversation count to be taken into account and return any one of the most active user in conversations.
My Approach : Used an additional Set to track down whether a particular pair of userID have previously conversed or not. If not, increment their conversation count, otherwise do nothing.
Follow Up 2 : Now, fetch the K most active users from the list of conversations.
As time didn't allow me any further to code it up, so just gave the approach below verbally.
My Approach : Gave 2 options either use a maxheap of Pair(userID,count) but as it will be operational heavy gave an alternate way to use 1 hashmap and 1 treemap one with UserID,Integer and another with Integer,Set. If any count increases for a userID, increase the count for it, and update it in the treemap to reduce the operational effort. By this way, we can get the K most active users.
Any further approaches are appreciated.


