Design Twitter

MediumHeapLeetCode 355 ↗World 9-16
0:00 / 0:00

Build a tiny Twitter with post, follow and a news feed. Hash maps hold tweets and follows, and a max-heap merges the ten newest.

▼

The problem

LeetCode 355 (Medium). Design a small social feed with postTweet(userId, tweetId), follow(followerId, followeeId), unfollow(followerId, followeeId) and getNewsFeed(userId), which returns the ids of the 10 most recent tweets posted by the user or by anyone they follow, newest first.

Example (LeetCode's): postTweet(1, 5); getNewsFeed(1) → [5]; follow(1, 2); postTweet(2, 6); getNewsFeed(1) → [6, 5]; unfollow(1, 2); getNewsFeed(1) → [5].

TRY IT ON LEETCODE ▶

The solution

import heapq
from collections import defaultdict

class Twitter:
    def __init__(self):
        self.time = 0
        self.tweets = defaultdict(list)   # user -> [(time, id)]
        self.follows = defaultdict(set)   # user -> {followees}
    def postTweet(self, user, tweet):
        self.time += 1
        self.tweets[user].append((self.time, tweet))
    def follow(self, a, b):
        self.follows[a].add(b)
    def unfollow(self, a, b):
        self.follows[a].discard(b)
    def getNewsFeed(self, user):
        heap = []
        for u in self.follows[user] | {user}:
            if self.tweets[u]:
                i = len(self.tweets[u]) - 1
                time, tid = self.tweets[u][i]
                heap.append((-time, tid, u, i))
        heapq.heapify(heap)
        feed = []
        while heap and len(feed) < 10:
            _, tid, u, i = heapq.heappop(heap)
            feed.append(tid)
            if i > 0:
                time, tid = self.tweets[u][i - 1]
                heapq.heappush(heap, (-time, tid, u, i - 1))
        return feed

Transcript

Design Twitter. Build a tiny social feed. Users post tweets, follow and unfollow each other, and fetch a news feed: the ten most recent tweets from themselves and everyone they follow, newest first.

User one posts tweet five, so their feed is five. User one follows user two, who posts tweet six. Now the feed is six, then five. After the unfollow, it's just five again.

The simple way gathers every followed tweet into one pile and sorts it by time. With T tweets, that's order T log T on every request, just to keep ten.

Better: keep a global clock, and stamp each post with the next tick. One hash map sends each user to their tweets, as time and id pairs in posting order. Another sends each user to the set of people they follow. Post appends, follow adds, unfollow removes: each is order one.

Now the key idea. Each list is already sorted, newest on top. So merge them with a max-heap: seed it with each one's newest tweet, yourself included. The top is always the newest tweet left. Pop it into the feed, then push that user's next older tweet.

In code, post ticks the clock and appends. The feed seeds the heap, then pops up to ten times, pushing each author's previous tweet.

Let's run it. User one follows two and three, and the town posts thirteen tweets. The heap starts with each user's newest: twelve, thirteen and eleven. Thirteen pops first, and user two's next tweet, ten, takes its place. Then twelve, eleven, ten, and on down. After ten pops the feed is full, so we stop. Three, two and one are never popped.

Post, follow and unfollow are order one. A feed is order F to seed the heap, plus ten pops of log F. Space is order of all tweets and follows.

Stamp every post, keep a set of follows, and merge with a heap. That's Design Twitter.