KatInterview

React Community Board: Voting, Re-Ranking, and Sticky Posts

System DesignModel answer
MediumModel answerSystem Designreactstate-managementaccessibility

Last updated

Build a React feed where upvotes and downvotes re-sort posts by score and designated sticky posts always stay above the ranked list.

React Community Board: Voting, Re-Ranking, and Sticky Posts

You have two hours to build a community board page in React. The board displays a list of posts fetched from a mock API (or a local JSON file). You need to implement two interactive features on top of that list.

Feature 1 — Voting and live re-ranking

Every post has an upvote button and a downvote button. Clicking either one updates the post's score and immediately re-sorts the entire list in descending score order.

Voting rules to handle:

  • Clicking the same vote direction a second time undoes the vote (score returns to where it was).
  • Clicking the opposite direction switches the vote (net change is ±2, not ±1).
  • A user can hold at most one vote per post at any time.

Feature 2 — Sticky posts

Any post can be marked sticky. A sticky post is always rendered above all non-sticky posts, regardless of score. If multiple posts are sticky, they are ordered among themselves by the same descending-score rule. Non-sticky posts continue to sort by score below them.


Before you start coding, think through these questions

  1. What data shape for each post makes a vote update a small, local change without reconstructing the whole list?
  2. When you re-sort, how do you break ties deterministically so the list does not flicker?
  3. When a post jumps position after a vote or a sticky toggle, what happens to keyboard focus and scroll position? How would you handle that?
  4. If votes and sticky state are persisted to a server, how do you keep the UI responsive while the request is in flight? What do you do if the request fails?

What your implementation should demonstrate

Part 1 — Feed with voting

  • A state shape that makes individual post updates cheap and immutable
  • Correct score arithmetic for all three vote transitions: new vote, undo, and switch
  • A stable, deterministic sort that does not cause unnecessary re-renders
  • Accessible controls: buttons with clear labels, visible focus states, and score announced to screen readers

Part 2 — Sticky behaviour

  • A composited sort that puts sticky posts first, then ranks each group independently by score
  • Correct behaviour when a sticky post is voted on (it stays in the sticky section, but its relative order within that section may change)
  • A considered answer to what should happen to focus when the list reorders

Bonus (discuss, don't necessarily code)

  • Optimistic UI: apply the vote locally before the server responds
  • Rollback: revert to the previous state if the server returns an error
  • Debounce or queue strategy if a user clicks rapidly

Hints

Hint 1

Model each post's vote state as a field like `userVote: 'up' | 'down' | null` stored directly on the post object in your array. The score delta on any new click is then just a function of the current `userVote` and the button pressed — no extra bookkeeping needed outside the post itself.

Hint 2

Derive the sorted list with `useMemo` rather than storing a separate sorted copy in state. Split posts into sticky and non-sticky, sort each group by score (breaking ties by a stable secondary key like creation timestamp or id), then concatenate. This way your source-of-truth state stays flat and easy to update immutably.

Hint 3

For accessibility, give each vote button a descriptive `aria-label` (e.g. "Upvote — currently 42 points") and wrap the score in a `<span aria-live="polite">` so screen readers announce changes. For focus-on-reorder, capture the focused post's id before the update and restore focus to that element after React re-renders using a `useEffect` with a ref.