Problem solution · Java

Design Twitter

Design Twitter: a Java solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
99 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Design Twitter, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 99 lines of Java from the credited upstream file 355.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
  • 2 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDesign Twitter · JavaJava
Use this to learn the idea, then write your own version.
class Tweet {  public int id;  public int time;  public Tweet next = null;  public Tweet(int id, int time) {    this.id = id;    this.time = time;  }} class User {  private int id;  public Set<Integer> followeeIds = new HashSet<>();  public Tweet tweetHead = null;   public User(int id) {    this.id = id;    follow(id);  }   public void follow(int followeeId) {    followeeIds.add(followeeId);  }   public void unfollow(int followeeId) {    followeeIds.remove(followeeId);  }   public void post(int tweetId, int time) {    final Tweet oldTweetHead = tweetHead;    tweetHead = new Tweet(tweetId, time);    tweetHead.next = oldTweetHead;  }} class Twitter {  /** Compose a new tweet. */  public void postTweet(int userId, int tweetId) {    users.putIfAbsent(userId, new User(userId));    users.get(userId).post(tweetId, time++);  }   /**   * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in   * the news feed must be posted by users who the user followed or by the user   * herself. Tweets must be ordered from most recent to least recent.   */  public List<Integer> getNewsFeed(int userId) {    if (!users.containsKey(userId))      return new ArrayList<>();     List<Integer> newsFeed = new ArrayList<>();    Queue<Tweet> maxHeap =        new PriorityQueue<>(Comparator.comparingInt((Tweet tweet) -> - tweet.time));     for (final int followeeId : users.get(userId).followeeIds) {      Tweet tweetHead = users.get(followeeId).tweetHead;      if (tweetHead != null)        maxHeap.offer(tweetHead);    }     int count = 0;    while (!maxHeap.isEmpty() && count++ < 10) {      Tweet tweet = maxHeap.poll();      newsFeed.add(tweet.id);      if (tweet.next != null)        maxHeap.offer(tweet.next);    }     return newsFeed;  }   /**   * Follower follows a followee.   * If the operation is invalid, it should be a no-op.   */  public void follow(int followerId, int followeeId) {    if (followerId == followeeId)      return;    users.putIfAbsent(followerId, new User(followerId));    users.putIfAbsent(followeeId, new User(followeeId));    users.get(followerId).follow(followeeId);  }   /**   * Follower unfollows a followee.   * If the operation is invalid, it should be a no-op.   */  public void unfollow(int followerId, int followeeId) {    if (followerId == followeeId)      return;    if (users.containsKey(followerId) && users.containsKey(followeeId))      users.get(followerId).unfollow(followeeId);  }   private int time = 0;  private Map<Integer, User> users = new HashMap<>(); // {userId: User}} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗