Design Social Networking Site Friend Recommendations
Design a social networking site where users can publish posts, like posts, add comments, manage friend requests, and receive deterministic people you may know recommendations.
Method Signatures
Create the Social Networking Site
SocialNetworkingSite(List<String> userIds)
- Creates the site with the given unique user IDs.
- Each user initially has no posts, friends, or pending friend requests.
Add a Post
boolean addPost(String userId, String postId, String content)
- Adds a post for the specified user.
- Returns
true when the post is added.
- Returns
false if the user does not exist, the post ID already exists, or the content is empty.
Like a Post
boolean likePost(String userId, String postId)
- Adds a like from the user to the specified post.
- Returns
true when the like is added.
- Returns
false if the user or post does not exist, or the user has already liked the post.
Add a Comment
boolean addComment(String userId, String postId, String commentId, String comment)
- Adds a comment to the specified post.
- Returns
true when the comment is added.
- Returns
false if the user or post does not exist, the comment ID already exists, or the comment is empty.
Send a Friend Request
String sendFriendRequest(String fromUserId, String toUserId)
- Sends a friend request from one user to another.
- Returns
"REQUEST_SENT" when the request is created.
- Returns
"INVALID_REQUEST" if either user does not exist, both IDs identify the same user, the users are already friends, or a request between them is already pending.
Accept or Reject a Friend Request
String respondToFriendRequest(String fromUserId, String toUserId, boolean accept)
- Processes the pending request sent by
fromUserId to toUserId.
- Returns
"REQUEST_ACCEPTED" when accept is true.
- Returns
"REQUEST_REJECTED" when accept is false.
- Returns
"REQUEST_NOT_FOUND" when the matching pending request does not exist.
- An accepted request creates a friendship in both directions.
Get People You May Know
List<String> getPeopleYouMayKnow(String userId, int limit)
- Returns at most
limit recommended user IDs.
- Only friends of the user's friends are considered.
- The user, existing friends, and users with a pending friend request in either direction are excluded.
- Recommendations are ordered by the number of mutual friends in descending order.
- Recommendations having the same number of mutual friends are ordered by user ID in lexicographically ascending order.
- Each eligible user appears at most once in the recommendation list.
- Returns an empty list if the user does not exist or no eligible recommendation is available.
General Rules
- User IDs, post IDs, and comment IDs are case-sensitive.
- All IDs and text values contain at least one visible character.
- A user can like a particular post at most once.
- Each post ID and comment ID must be globally unique.
- A friendship is always mutual.
- Rejected friend requests may be sent again later.
- All methods must follow the specified return values without throwing exceptions.
Note:
During the interview round follow-up questions were asked about handling concurrent friend requests. Although you do not have to implement them now.
- How would the system handle a situation in which a single user receives a very large number of friend requests?
- How would concurrency be handled across different operations, and how would dirty reads and race conditions be prevented?
- How would the People You May Know feature be designed, and what recommendation algorithm would it use?
- Where should friendship and connection data be stored, and which type of database should be used for each kind of data?
Constraints
1 ≤ userIds.size() ≤ 10,000
1 ≤ userId.length() ≤ 50
1 ≤ postId.length() ≤ 50
1 ≤ commentId.length() ≤ 50
1 ≤ content.length() ≤ 5,000
1 ≤ comment.length() ≤ 1,000
1 ≤ limit ≤ 100
- All values in
userIds are unique.
Examples
Example 1: Posts, Likes, and Comments
SocialNetworkingSite(userIds = ["anaya", "kabir", "meera"]) addPost(userId = "anaya", postId = "post-17", content = "My first mountain walk") Output: true likePost(userId = "kabir", postId = "post-17") Output: true likePost(userId = "kabir", postId = "post-17") Output: false addComment(userId = "meera", postId = "post-17", commentId = "comment-8", comment = "Beautiful view!") Output: true
Example 2: Friend Requests
SocialNetworkingSite(userIds = ["dev", "isha", "neel"]) sendFriendRequest(fromUserId = "dev", toUserId = "isha") Output: "REQUEST_SENT" respondToFriendRequest(fromUserId = "dev", toUserId = "isha", accept = true) Output: "REQUEST_ACCEPTED" sendFriendRequest(fromUserId = "dev", toUserId = "isha") Output: "INVALID_REQUEST"
Example 3: People You May Know
SocialNetworkingSite(userIds = ["arun", "bina", "chetan", "diya", "farah"]) sendFriendRequest(fromUserId = "arun", toUserId = "bina") Output: "REQUEST_SENT" respondToFriendRequest(fromUserId = "arun", toUserId = "bina", accept = true) Output: "REQUEST_ACCEPTED" sendFriendRequest(fromUserId = "bina", toUserId = "chetan") Output: "REQUEST_SENT" respondToFriendRequest(fromUserId = "bina", toUserId = "chetan", accept = true) Output: "REQUEST_ACCEPTED" sendFriendRequest(fromUserId = "bina", toUserId = "diya") Output: "REQUEST_SENT" respondToFriendRequest(fromUserId = "bina", toUserId = "diya", accept = true) Output: "REQUEST_ACCEPTED" getPeopleYouMayKnow(userId = "arun", limit = 5) Output: ["chetan", "diya"]
Both recommended users have one mutual friend with Arun, so their user IDs are returned in lexicographically ascending order.