SkillPilotAI

Frontend system design

Frontend system design questions ("design an autocomplete", "design a news feed") test structured thinking more than code. This module covers the RADIO framework, an autocomplete with debounce + cache + AbortController, news feed design with cursor pagination, and polling vs SSE vs WebSocket.

74.1 Why is it used?

  • Senior interviews ask open design questions where structure matters more than syntax.
  • Most common mistake: jumping straight into useState and useEffect.
  • The interviewer first wants to see you clarify requirements and think about the whole system.
  • Structure first, code later: RADIO gives that structure.

74.2 RADIO framework

StepMeaningWhat you cover
RRequirementsFeatures, scope, accessibility
AArchitectureDiagram of UI, state, API layer, cache
DData modelShape of the state
IInterfaceAPI endpoints, pagination
OOptimizationsPerformance, error handling (spend the most time here)

74.3 Autocomplete: the naive version

JavaScript
async function onType(q) {  const res = await fetch(`/search?q=${q}`);  const items = await res.json();  show(items);} // user types "redmi" quicklyinput.addEventListener("input", (e) =>  onType(e.target.value));
Output
> fetch: r> fetch: re> fetch: red> fetch: redm> fetch: redmiScreen: Red shirt, Red shoes, Redmi Note 14, Redmi 14C
Why the wrong results?
  1. 1.User types "redmi" quickly

  2. 2.Every keystroke: 5 API calls (heavy load with lakhs of users)

  3. 3.Responses arrive in any order (race condition)

  4. 4.Slow "red" response arrives last

  5. 5.Screen shows red results: redmi overwritten

Fix: debounce + cache + abort

74.4 Fix 1: debounce

JavaScript
const cache = new Map();let timer, controller; function onType(q) {  clearTimeout(timer);  if (q.length < 2) return;  timer = setTimeout(() => search(q), 300);}
Output
> fetch: redmiScreen: Redmi Note 14, Redmi 14C
  • Debounce: do not call on every keystroke, wait for the user to pause typing.
  • Every new key clears the old timer; queries shorter than 2 characters are ignored.
  • A new 300 ms timer starts, so "redmi" sends only one request.

74.5 Fix 2: cache + AbortController

JavaScript
async function search(q) {  if (cache.has(q)) return show(cache.get(q));  controller?.abort();  controller = new AbortController();  try {    const res = await fetch(`/search?q=${q}`,      { signal: controller.signal });    const items = await res.json();    cache.set(q, items);    show(items);  } catch (err) {    if (err.name !== "AbortError") throw err;  }}
Output
> fetch: red> aborted: red> fetch: redmiScreen: Redmi Note 14, Redmi 14C
  • Cache (Map): a repeated query is shown instantly with no API call.
  • abort() cancels the old request; the new AbortController's signal goes to fetch.
  • The cancelled request throws AbortError, which is ignored; real errors are re-thrown.
  • Result: a slow old response can never overwrite newer results (race fixed).
Autocomplete request flow (fixed)
  1. 1.Keystroke: clear old timer

  2. 2.Query < 2 chars? ignore

  3. 3.300 ms pause: search(q)

  4. 4.In cache? show instantly, stop

  5. 5.Abort previous request, fetch with new signal

  6. 6.Save in cache, show results

74.6 Checkpoint: how many API calls?

Output
1. type "redmi" and wait2. clear the search box3. type "redmi" again// total API calls = ?
Output
> fetch: redmiScreen: Redmi Note 14, Redmi 14C
  • Only one: the second time, after the debounce, search finds "redmi" in the cache.

74.7 News feed design

  • Pagination: cursor based (see 74.8).
  • Infinite scroll: IntersectionObserver loads the next page at the end of the scroll.
  • Virtualization: out of thousands of posts, keep only the visible ones in the DOM.
  • Like: optimistic update changes the UI at once.
  • New posts: show a "new posts" button instead of making the scroll jump.
  • Images: lazy load with a fixed aspect ratio (no layout shift).

74.8 Offset vs cursor pagination

JavaScript
async function loadMore() {  const res = await fetch(    `/api/feed?offset=${posts.length}&limit=10`  );  posts.push(...(await res.json()));}
JavaScript
async function loadMore() {  const lastId = posts.at(-1)?.id ?? "";  const res = await fetch(    `/api/feed?after=${lastId}&limit=10`  );  posts.push(...(await res.json()));}
PointOffset (wrong for feeds)Cursor (right)
Requestoffset=posts.length&limit=10after=<last post id>&limit=10
New posts added at the topOlder posts shift downNo effect: starts after the last seen id
ResultSame posts again (duplicates)No duplicates

74.9 Real time: polling vs SSE vs WebSocket

PointShort pollingSSEWebSocket
Data directionClient asks every N secServer -> client onlyBoth ways
ConnectionNew request each timeOne open HTTP connectionOne persistent two-way connection
ProsSimplestSimple, normal HTTP, auto reconnectClient and server send any time
ConsMany empty responsesOne way onlySeparate server setup, scaling, reconnect logic
Use forRare updates, simple statusLive score, notifications, AI text streamingChat, multiplayer game
JavaScript
const score = new EventSource("/api/score"); score.onmessage = (event) => {  show(event.data);}; score.onerror = () => {  // browser reconnects on its own};
Output
IND 182/3 (18.1)IND 186/3 (18.2)IND 192/3 (18.3)
  • Live cricket score: data only comes from the server, so SSE is perfect.
  • EventSource is built into the browser; onmessage runs for every event.
  • If the connection breaks, the browser reconnects on its own.
  • On the server it simply streams from a normal route handler.

74.10 Common mistakes

  • Jumping into code before clarifying requirements.
  • A fetch on every keystroke with no debounce, cache or abort.
  • Offset pagination in a feed: duplicates when new posts arrive.
  • Misconception: WebSocket is always best. For one way data (notifications, live score) SSE is simpler; AI chat word-by-word streaming is mostly SSE too. Use WebSocket only when data goes both ways.

74.11 Say these in every design answer

  1. Performance: code splitting, lazy loading, caching.
  2. Keep server state and client state separate.
  3. Error handling: retry and a fallback UI.
  4. Accessibility: keyboard (arrow keys, Escape), screen reader.
  5. Security: prevent XSS, HttpOnly cookies.
  6. Monitoring and testing.

74.12 Interview tips

  • "Design an autocomplete": answer in RADIO order and clarify requirements first.
  • Debounce reduces requests, the cache makes repeated queries instant, AbortController fixes the race condition.
  • Add accessibility: keyboard navigation and the combobox role.
  • Feed: cursor pagination + infinite scroll + virtualization; real time: choose by the direction of data.