Design Twitter
Expert Answer & Key Takeaways
# System & Data Structure Design
Design problems in DSA interviews test your ability to translate requirements into a functional, efficient, and maintainable class structure. Unlike standard algorithmic problems, the focus here is on State Management and API Design.
### Core Principles
1. Encapsulation: Keep data private and expose functionality through well-defined methods.
2. Trade-offs: Every design choice has a cost. Is it better to have read and write, or vice versa?
3. State Consistency: Ensure that your internal data structures (e.g., a Map and a List) stay in sync after every operation.
### Common Design Patterns
#### 1. HashMap + Doubly Linked List (DLL)
The "Gold Standard" for caching (LRU/LFU).
```text
[Head] <-> [Node A] <-> [Node B] <-> [Node C] <-> [Tail]
^ ^ ^ ^ ^
(MRU) (Data) (Data) (Data) (LRU)
```
- HashMap: Provides lookups for keys to their corresponding nodes.
- DLL: Provides addition/removal of nodes at both ends, maintaining the order of access.
#### 2. Amortized Analysis (Rebalancing)
Commonly used in Queue using Stacks or Dynamic Arrays.
- Instead of doing heavy work on every call, we batch it. Pushing to a stack is , and "flipping" elements to another stack happens only when necessary, averaging per operation.
#### 3. Ring Buffers (Circular Arrays)
Used for fixed-size memory management (e.g., Circular Queue, Hit Counter).
```text
[0] [1] [2] [3] [4] [5]
^ ^ ^
Head (Data) Tail
(Pops) (Next Push)
```
- Use `(index + 1) % capacity` to wrap around the array.
#### 4. Concurrency & Thread Safety
For "Hard" design problems (e.g., Bounded Blocking Queue).
- Use Mutexes (Locks) to prevent data races.
- Use Condition Variables (`wait`/`notify`) to manage producer-consumer logic efficiently without busy-waiting.
### How to Approach a Design Problem
1. Identify the API: What methods do you need to implement? (`get`, `put`, `push`, etc.)
2. Define the State: What variables represent the current state? (Size, Capacity, Pointers).
3. Choose the Data Structures: Select the combination that minimizes time complexity for the most frequent operations.
4. Dry Run: Trace the state changes through a sequence of operations based on your chosen structure.
Design Twitter
Design a simplified version of Twitter where users can post tweets, follow/unfollow others, and see the 10 most recent tweets in their news feed.
Core Operations
postTweet(userId, tweetId)getNewsFeed(userId)follow(followerId, followeeId)unfollow(followerId, followeeId)
Scaling Hint
Pull strategy: When a user requests their feed, merge the recent tweets from themselves and everyone they follow using a Max-Heap.
Examples
Input: postTweet(1, 5), getNewsFeed(1), follow(1, 2), postTweet(2, 6), getNewsFeed(1), unfollow(1, 2), getNewsFeed(1)
Output: [[5], [6, 5], [5]]
Approach 1
Level I: Map of Lists (Brute Force Sort)
Intuition
Store each user's tweets in a
Map<UserId, List<TweetId>>. For getNewsFeed, collect all tweets from the user and their followees into a single list, sort them by timestamp, and return the top 10.⏱ Feed: O(N log N) where N is tweets of followed users. Other ops: O(1).💾 O(TotalTweets + TotalFollows).
Detailed Dry Run
User 1 follows 2. User 1 tweets: [T1]. User 2 tweets: [T2]. Feed: Sort([T1, T2]) -> [T2, T1].
Approach 2
Level II: Global List + Filtering
Intuition
Maintain a single global list of all tweets. When
getNewsFeed(userId) is called, iterate backwards through the list and pick the first 10 tweets that were posted by the user or their followees.⏱ Feed: O(T) where T is total tweets in system. Other ops: O(1).💾 O(T + F).
Approach 3
Level III: Map + Heap (Pull-based Feed)
Intuition
Keep a
User class containing a Set of followees and a LinkedList of tweets. For getNewsFeed, push the first tweet of each user in the followee list into a Max-Heap (sorted by timestamp), and then standard merge-k-sorted-lists logic.⏱ Feed: O(F log F) where F is number of followees. Other ops: O(1).💾 O(U + T) for Users and Tweets.
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.