LLD Social Media Feed System (JS, Python, Java)
JavaScript Implementation
class Post {
constructor(postId, userId, timestamp) {
this.postId = postId;
this.userId = userId;
this.timestamp = timestamp;
}
}
class SocialMedia {
constructor() {
this.userPosts = new Map();
this.following = new Map();
this.time = 0;
}
createPost(userId, postId) {
const post = new Post(postId, userId, ++this.time);
if (!this.userPosts.has(userId)) {
this.userPosts.set(userId, []);
}
this.userPosts.get(userId).push(post);
}
deletePost(userId, postId) {
if (!this.userPosts.has(userId)) return;
const filtered = this.userPosts.get(userId).filter(p => p.postId !== postId);
this.userPosts.set(userId, filtered);
}
follow(followerId, followeeId) {
if (followerId === followeeId) return;
if (!this.following.has(followerId)) {
this.following.set(followerId, new Set());
}
this.following.get(followerId).add(followeeId);
}
unfollow(followerId, followeeId) {
if (this.following.has(followerId)) {
this.following.get(followerId).delete(followeeId);
}
}
getFeed(userId) {
const posts = [];
const followees = new Set(this.following.get(userId) || []);
followees.add(userId);
for (let uid of followees) {
const userPosts = this.userPosts.get(uid) || [];
posts.push(...userPosts.slice(-10));
}
posts.sort((a, b) => b.timestamp - a.timestamp);
return posts.slice(0, 10).map(post => post.postId);
}
}
Python Implementation
import heapq
from collections import defaultdict
class Post:
def __init__(self, post_id, user_id, timestamp):
self.post_id = post_id
self.user_id = user_id
self.timestamp = timestamp
def __lt__(self, other):
return self.timestamp > other.timestamp
class SocialMedia:
def __init__(self):
self.users = set()
self.posts = defaultdict(list)
self.following = defaultdict(set)
self.timestamp = 0
def createPost(self, userId, postId):
self.timestamp += 1
post = Post(postId, userId, self.timestamp)
self.posts[userId].append(post)
def deletePost(self, userId, postId):
self.posts[userId] = [p for p in self.posts[userId] if p.post_id != postId]
def follow(self, followerId, followeeId):
if followerId != followeeId:
self.following[followerId].add(followeeId)
def unfollow(self, followerId, followeeId):
self.following[followerId].discard(followeeId)
def getFeed(self, userId):
heap = []
followees = self.following[userId] | {userId}
for uid in followees:
for post in self.posts[uid][-10:]:
heapq.heappush(heap, post)
return [post.post_id for post in heapq.nsmallest(10, heap)]
Java Implementation
import java.util.*;
class Post {
int postId;
int userId;
long timestamp;
public Post(int postId, int userId, long timestamp) {
this.postId = postId;
this.userId = userId;
this.timestamp = timestamp;
}
}
class SocialMedia {
private Map<Integer, Set<Integer>> following = new HashMap<>();
private Map<Integer, List<Post>> userPosts = new HashMap<>();
private long timestamp = 0;
public void createPost(int userId, int postId) {
timestamp++;
Post post = new Post(postId, userId, timestamp);
userPosts.computeIfAbsent(userId, k -> new ArrayList<>()).add(post);
}
public void deletePost(int userId, int postId) {
if (userPosts.containsKey(userId)) {
userPosts.get(userId).removeIf(p -> p.postId == postId);
}
}
public void follow(int followerId, int followeeId) {
if (followerId != followeeId) {
following.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId);
}
}
public void unfollow(int followerId, int followeeId) {
if (following.containsKey(followerId)) {
following.get(followerId).remove(followeeId);
}
}
public List<Integer> getFeed(int userId) {
PriorityQueue<Post> pq = new PriorityQueue<>((a, b) -> Long.compare(b.timestamp, a.timestamp));
Set<Integer> followees = new HashSet<>(following.getOrDefault(userId, new HashSet<>()));
followees.add(userId);
for (int uid : followees) {
List<Post> posts = userPosts.getOrDefault(uid, new ArrayList<>());
for (int i = Math.max(0, posts.size() - 10); i < posts.size(); i++) {
pq.add(posts.get(i));
}
}
List<Integer> feed = new ArrayList<>();
while (!pq.isEmpty() && feed.size() < 10) {
feed.add(pq.poll().postId);
}
return feed;
}
}