Skip to solution
hardSystem Design

How would you design a news feed system?

1.2k views
01

Understand the problem

Outline a social feed design.

news-feed
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Designing a News Feed System (Beginner-Friendly Guide)

Target Audience: Junior & Senior Software Engineers preparing for System Design Interviews — no prior system design knowledge assumed. Difficulty: Medium to Hard

How to read this doc: Every concept is explained in plain language first. Right after

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Hybrid fan-out: push for regular users, pull for celebrities
Run Playground
function fanOutPost(post, followerCount, followers, feeds, celebrityPosts, threshold = 5) {
  // post = { id, author, timestamp }
  if (followerCount > threshold) {
    if (!celebrityPosts[post.author]) celebrityPosts[post.author] = [];
    celebrityPosts[post.author].push(post);
  } else {
    for (const follower of followers) {
      if (!feeds[follower]) feeds[follower] = [];
      feeds[follower].push(post);
    }
  }
}

function getFeed(userId, feeds, following, celebrityPosts, limit = 10) {
  let entries = [...(feeds[userId] || [])];
  for (const followed of following[userId] || []) {
    entries = entries.concat(celebrityPosts[followed] || []);
  }
  entries.sort((a, b) => b.timestamp - a.timestamp);
  return entries.slice(0, limit).map(p => p.id);
}

const feeds = {};
const celebrityPosts = {};
const following = {
  bob: ['alice', 'star'],
  carol: ['alice'],
  dave: ['alice'],
};

fanOutPost({ id: 'p1', author: 'alice', timestamp: 100 }, 3, ['bob', 'carol', 'dave'], feeds, celebrityPosts, 5);
fanOutPost({ id: 'p2', author: 'star', timestamp: 200 }, 6, ['bob', 'carol', 'dave', 'eve', 'frank', 'grace'], feeds, celebrityPosts, 5);
fanOutPost({ id: 'p3', author: 'alice', timestamp: 300 }, 3, ['bob', 'carol', 'dave'], feeds, celebrityPosts, 5);

console.log("bob's feed:", getFeed('bob', feeds, following, celebrityPosts));
console.log("carol's feed:", getFeed('carol', feeds, following, celebrityPosts));
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 48 of 99 decoded in the System Design track. One more won't hurt.

Back to track