Build out a React component to display event log stream configuration data.
MediumYou're building an internal tool for a team that runs event log streams. Each stream has a config: how long to keep events, how many partitions it writes to, and so on. The team wants a table of every stream where an engineer can open any row to read its raw JSON config without leaving the page.
The scaffold gives you a stubbed component, a mock endpoint, and tested helper functions.
What's in the codebase
src/ConfigTable.jsx. The component you build. The starter renders the heading and column headers with an empty body. This is the only file you change.src/configLogic.js. Finished helpers:toggleExpanded,expandAllIds,applyFilter, andvisibleConfigs. Use them as they are.src/mockApi.js.fetchConfigs()resolves five configs after 50 ms, andfetchConfigsFailing()rejects so you can test the error state. One config has a nested object, and one has an empty config.test_configLogic.jsandtest_component.js. Ten helper tests and six render tests, run with Node's built-in test runner.demo.js. Exercises the helpers end to end with plain Node.src/index.jsxandpublic/index.html. The entry point and page shell. The files useReact.createElementinstead of JSX syntax, so the tests run without a build step.
Out of the box, the ten helper tests and one render test pass, and five render tests fail. demo.js already prints PASS, because it only checks the helpers.
Your task
Build ConfigTable. On mount, call fetchFn (which defaults to fetchConfigs) inside useEffect, and ignore a response that arrives after the component unmounts. Show Loading configs... while fetching, Failed to load configs: plus the error message on failure, and No configs found. for an empty list.
Render one row per config with Name, Stream, Enabled (Yes or No), and a toggle button reading Show config or Hide config. An open row shows JSON.stringify(config, null, 2) in a <pre> element. Keep expansion as a Set of config IDs, and key rows by ID. Add a filter input with the placeholder Filter by name or stream, plus Expand all and Collapse all buttons. Expand all opens only the rows the filter currently shows. When nothing matches, show No configs match your filter. Memoize the row with React.memo, the filtered list and JSON strings with useMemo, and the handlers with useCallback.
The render tests use server rendering, which never runs effects, so they pass data in through two more props. Support both: initialConfigs renders that list straight away and skips the fetch, and initialExpanded is a Set of IDs that start open.
Filter "PAY" -> Payment Events only Filter "audit", then Expand all -> only cfg-audit is expanded Expand Search Events, filter "payments", then clear the filter -> Search Events is still expanded Expand Notification Events -> <pre> shows {} fetchFn = fetchConfigsFailing -> Failed to load configs: Failed to fetch configs
You're done when npm test reports 16 passing tests and the table behaves as described when you click through it.
The scaling follow-up
The interviewer asks what changes if the endpoint returns 5,000 configs instead of 5. Talk through what breaks first, the fetch, the filter, or the render, what you'd change first, and what expand all should mean when the user can see only part of the list.
Using AI on this problem
Frontend screens like this usually allow AI for documentation lookups, such as how useEffect cleanup runs or when React.memo skips a render. Decide the state model yourself: a Set of IDs, keys by ID, and expand all scoped to the visible rows are what the interviewer wants to hear you reason about. If AI drafts the component, check that its list keys aren't array indexes.
Approach
- Hold four pieces of state: the configs, an error, the expanded
Set, and the filter query. Seed them frominitialConfigsandinitialExpandedwhen given. - Fetch in
useEffectunlessinitialConfigswas passed. Set acancelledflag in the cleanup so a late response can't update an unmounted component. - Derive the visible list with
useMemofrom the configs and the query, and build a map from ID to JSON string from the visible list. - Toggle with the functional form of
setExpanded, so the handler never reads a stale set and can be memoized with no dependencies. - Pass the visible list, not the full list, to
expandAllIds. - Wrap the row in
React.memoand give it stable props: the config, its JSON string, a boolean, and the shared toggle handler.
The filter is O(n) per query change, and each config is stringified once per change to the visible list, not on every render. Typing in the filter re-renders the parent, but a row whose props didn't change is skipped by React.memo. Nothing in the filter path touches the expanded set, so a row that's filtered out and back keeps its state.
npm test reports 16 passing tests. With fetchConfigsFailing as fetchFn, the table shows the error message, and filtering to audit before Expand all opens only that row.
What to focus on
- Key by ID, never by index. The filter removes and reorders rows. An index key ties state to a position, so after filtering, an open row's state can land on a different config.
- Scope expand all to the visible rows. Passing the full list to
expandAllIdsopens rows the user can't see, and they appear open when the filter clears. - Clean up the fetch. Without the
cancelledflag, a response that arrives after unmount or after a newer fetch writes stale data into state. - Memoize where it pays.
React.memoon the row only helps if its props are stable, which is why the toggle handler uses the functional update and the JSON strings come from a memoized map. - Treat the empty config as data.
JSON.stringify({}, null, 2)is{}. The row still toggles and shows it.
The scaling follow-up
At 5,000 configs the render breaks first. Every keystroke in the filter re-renders thousands of rows, so windowing comes first: render only the rows in view plus a small buffer, with a library like react-window or a hand-rolled version. Pagination is a simpler alternative if scrolling isn't required.
Next, debounce the filter by 150 to 300 ms so a fast typist triggers one filter pass instead of one per character. If the list keeps growing, move filtering to the server and fetch one page of matches at a time.
The ID-keyed Set still works, since lookups stay constant time and IDs are stable across pages and windows. Expand all is what needs a new rule: opening 5,000 <pre> blocks defeats windowing. Scope it to the current page or window, or render JSON lazily only for open rows that are on screen.
Leveling signals
Interview experiences
1 sharedRelated courses


