Applied AI Engineer (LLM) coding learning path Level 3 This level · 13 of 27

Design Twitter

Medium Design Heap

Problem

Design a simplified Twitter with postTweet, follow, unfollow, and getNewsFeed. The news feed returns the ten most recent tweet ids from the user and the people they follow, newest first.

Original: LeetCode 355: Design Twitter.

Examples

Example 1

Input`Twitter: postTweet(1, 5), getNewsFeed(1), follow(1, 2), postTweet(2, 6), getNewsFeed(1), unfollow(1, 2), getNewsFeed(1)`
Output`[null, [5], null, null, [6, 5], null, [5]]`

Constraints

  • 1 <= userId, followerId, followeeId <= 500
  • 0 <= tweetId <= 10^4
  • All the tweets have unique IDs.
  • At most 3 * 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.
  • A user cannot follow himself.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 355: Design Twitterleetcode.com
  2. Java SE 17 API: PriorityQueuedocs.oracle.com
Previous
Next