Skip to main content
Back to problems
Leetcode
Medium
Design
Hash Maps
Heaps
Queues
Meta
Google
Amazon
Design Twitter

Design a miniature Twitter service that supports posting tweets, following and unfollowing users, and retrieving a personalized news feed.

Acceptance 0%
Problem Statement

Problem

Design a simplified Twitter-like service with the following capabilities:

  • Users can post tweets.
  • Users can follow other users.
  • Users can unfollow users.
  • A user can request their news feed, which should return the most recent tweets from the user and everyone they follow.

The feed should contain up to 10 tweets, ordered from most recent to least recent.

Requirements

Implement a data structure with these operations:

  • postTweet(userId, tweetId): Compose a new tweet.
  • getNewsFeed(userId): Retrieve the 10 most recent tweet IDs in the user's feed.
  • follow(followerId, followeeId): Follower starts following followee.
  • unfollow(followerId, followeeId): Follower stops following followee.

A user's feed should include their own tweets and tweets from accounts they follow.

Notes

  • Tweets are identified by unique integer IDs.
  • More recent tweets should appear earlier in the feed.
  • If there are fewer than 10 tweets available, return all of them.
  • Unfollowing oneself should have no effect.

Input Format

This is a design problem. The exact method signatures are provided by the platform.

Output Format

For each getNewsFeed call, return a list of tweet IDs in descending recency order.

Constraints

  • You may assume user and tweet IDs are integers.
  • The number of operations can be large, so each operation should be efficient.
  • The feed must return at most 10 tweets.

Hints

  • Keep track of each user's follow relationships.
  • Store tweets with a recency marker so you can compare post times.
  • To build a feed efficiently, consider only the newest tweets from relevant users first.

Input Format

Design a class or service with methods similar to:

clike

postTweet(userId, tweetId)
getNewsFeed(userId)
follow(followerId, followeeId)
unfollow(followerId, followeeId)

Output Format

getNewsFeed(userId) returns an array/list of up to 10 tweet IDs ordered from newest to oldest.

Constraints

  • Feed size: at most 10 tweets
  • Tweets are ordered by recency
  • Users can follow many accounts
  • Self-follow behavior should be handled correctly
Examples
Sample cases returned by the problem API.

Example 1

Input

postTweet(1, 5)
getNewsFeed(1)

Output

[5]

Explanation

User 1 has posted one tweet, so their feed contains that tweet.

Example 2

Input

postTweet(1, 5)
follow(1, 2)
postTweet(2, 6)
getNewsFeed(1)

Output

[6, 5]

Explanation

User 1 follows user 2, so the feed includes user 2's newer tweet before user 1's older tweet.

Show 1 more example

Example 3

Input

postTweet(1, 5)
postTweet(1, 3)
postTweet(1, 101)
getNewsFeed(1)

Output

[101, 3, 5]

Explanation

Tweets are returned from most recent to least recent.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.