STRIPE Coding Question – Solved

7 Live
Java: Social Network Interaction System Design and implement a social media platform where users can follow and unfollow each other, post content, and receive notifications about new posts from the users they follow. You should print a message when a new user is added, when a user follows or unfollows another user, and when a user posts or receives a notification. Complete the following methods: 1. User class: - update(String message, User followee): void, should print: "{UserName} received notification: {followeeName} posted: {message}." - addFollowers(User user): void - removeFollowers(User user): void - notifyFollowers(String message): void - post(String content): void, should print: "{UserName} posted: \"{content}\"" - follow(User user): void - unfollow(User user): void 2. Platform class: - addUser(Integer id, String name): void, should print: "{UserName} added successfully." - follow(Integer followerId, Integer followeeId): void, should print: "{FollowerName} is now following {FolloweeName}." - unfollow(Integer followerId, Integer followeeId): void, should print: "{FollowerName} has unfollowed {FolloweeName}." - post(Integer userId, String content): void Input Format: You are not responsible for reading any input from stdin. Output Format: You should print a message when a new user is added, when a user follows or unfollows another user, and when a user posts or receives a notification. Input Format for Custom Testing: Sample Case 0 Sample Input 0: 8 Add User 0 Alexander Add User 1 Isabella Add User 2 Emma Follow 1 0 Post 0 Hiking in the mountains. Follow 2 0 Post 0 Enjoying a beautiful day! Unfollow 1 0 Sample Output 0: Alexander added successfully. Isabella added successfully. Emma added successfully. Isabella is now following Alexander. Alexander posted: "Hiking in the mountains.". Isabella received notification: Alexander posted: "Hiking in the mountains.". Emma is now following Alexander. Alexander posted: "Enjoying a beautiful day!". Isabella received notification: Alexander posted: "Enjoying a beautiful day!". Emma received notification: Alexander posted: "Enjoying a beautiful day!". Isabella has unfollowed Alexander.

Asked in: STRIPE

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To design and implement this social media interaction system, you need to think carefully about the data structures and interactions between User objects and the Platform that manages them. The goal is to simulate a small social network where users can follow/unfollow others, post content, and notify followers about new posts. You must also print specific messages at each significant event.

---

Step 1: Understanding the Core Classes and Their Responsibilities

1. User class: Each User represents a person on the platform with a unique id, a name, and a list of followers. The User class must manage:
- Followers: Who follows this user, stored in a collection (e.g., a Set or List) to avoid duplicates.
- Followees: Whom this user follows (useful for follow/unfollow logic).
- Posting: When a user posts content, notify all followers.
- Receiving notifications: When notified by a followee, print the corresponding message.

2. Platform class: This manages all users and coordinates operations like adding users, following/unfollowing, and posting on behalf of users. It maintains a mapping from user ids to User objects for quick access.

---

Step 2: Data Structures to Use

- For storing followers inside a User, use a Set data structure to prevent duplicate followers and enable efficient add/remove operations.
- The Platform can keep a HashMap or similar map structure mapping user ids (integers) to User instances.
- Each User might also track who they follow to facilitate unfollowing efficiently.

---

Step 3: Method Responsibilities and Flow

- addUser(id, name):
- Create a new User object with the given id and name.
- Store it in the Platform's user map.
- Print "{UserName} added successfully."

- follow(followerId, followeeId):
- Retrieve both User objects from the Platform.
- The follower calls a method to follow the followee:
- Add the follower to the followee’s followers set.
- Add the followee to the follower’s followees set.
- Print "{FollowerName} is now following {FolloweeName}."

- unfollow(followerId, followeeId):
- Retrieve both User objects.
- The follower calls a method to unfollow the followee:
- Remove the follower from the followee’s followers set.
- Remove the followee from the follower’s followees set.
- Print "{FollowerName} has unfollowed {FolloweeName}."

- post(userId, content):
- Retrieve the User object.
- Call the User’s post method:
- Print "{UserName} posted: \"{content}\"."
- Notify each follower by calling their update method, passing the message and the posting User.
- Each follower prints "{UserName} received notification: {followeeName} posted: \"{message}\"."

---

Step 4: Implementation Details and Interaction Flow

1. **User class internal methods:**

- addFollowers(User user):
- Add the given user to the followers set.

- removeFollowers(User user):
- Remove the given user from the followers set.

- follow(User user):
- The current user wants to follow another user.
- Add the current user as a follower of that user by calling the other user's addFollowers(this).
- Optionally track the followee in the current user's followees set.

- unfollow(User user):
- The current user wants to unfollow another user.
- Remove the current user from that user's followers by calling removeFollowers(this).
- Remove the followee from the current user's followees.

- post(String content):
- Print the user's post message.
- Notify all followers by calling notifyFollowers(content).

- notifyFollowers(String message):
- For each follower in the followers set, call follower.update(message, this).

- update(String message, User followee):
- Print the notification message indicating this user received a post from the followee.

2. **Platform class:**

- addUser(id, name):
- Create new User and insert into user map.
- Print success message.

- follow(followerId, followeeId):
- Retrieve users.
- Call follower.follow(followee).
- Print follow message.

- unfollow(followerId, followeeId):
- Retrieve users.
- Call follower.unfollow(followee).
- Print unfollow message.

- post(userId, content):
- Retrieve user.
- Call user.post(content).

---

Step 5: Edge Cases and Validations

- Although not explicitly stated, you should handle cases where follower or followee IDs do not exist gracefully, either ignoring or printing error messages.

- Prevent users from following themselves.

- Ensure duplicate follows do not create duplicate notifications or follower entries.

- When unfollowing, silently ignore if not following.

---

Step 6: Summary of Key Points

- Use user id to manage users centrally in Platform.

- User manages followers and followees sets for fast lookups.

- Posting triggers notifications to followers.

- Printing happens on user addition, follow/unfollow actions, posting, and receiving notifications.

- Maintain clean separation of responsibilities between Platform (orchestrator) and User (entity behavior).

---

Step 7: Testing Your Design

To verify correctness, simulate the sample test case step-by-step:

- Add users and check add messages.

- Follow operations trigger proper follower list updates and print messages.

- Post operation prints the post and sends notifications to all current followers, who print their notification messages.

- Unfollow removes followers and prints correct messages.

This ensures that your design correctly handles social network dynamics and output requirements.
```


Related Questions