KatInterview

Threaded Comment Feed: Filtering, Nesting, and Persistent Sort

Trees & GraphsModel answer
MediumModel answerTrees & Graphstree-constructiondfslocalStorage

Last updated

Transform a flat array of comments into a sorted, nested, filterable tree while persisting the user's sort preference across sessions.

Threaded Comment Feed

You are building the client-side logic for a comment section. The backend hands you a flat array of comment objects. Your job is to turn that raw data into a sorted, nested, filterable structure — without touching the backend.

Each comment object has these fields:

FieldTypeDescription
idstringUnique identifier
userIdstringID of the author
timestampstring or numberISO 8601 string or epoch ms
contentstringBody text of the comment
parentIdstring or nullID of the parent comment, or null for top-level

Implement the following behaviors:

1. Nested display Reconstruct the thread hierarchy from parentId references alone. A comment whose parentId is null, or whose parent does not exist in the data, should be treated as a top-level comment. Do not modify the flat input array.

2. Sortable list Allow the user to sort comments by either timestamp or userId, in ascending or descending order. Sorting must apply at every level of nesting — siblings are sorted among themselves, not globally across the whole flat list.

3. Persistent sort preference When the user picks a sort field or direction, save that choice so it survives a page refresh. On load, restore the saved preference; if it is missing, invalid, or unreadable, fall back to timestamp ascending.

4. Content filter Provide a text input that filters comments by their content field (case-insensitive substring match). The rule for what to show: if a comment matches the query, show it and every one of its descendants, even if the descendants do not themselves match. If a comment does not match and none of its descendants match, hide it entirely. Non-matching ancestors are not shown.

Assumptions

  • Treat the data as local mock data; no network calls are needed.
  • The filter string is transient — do not persist it.
  • Cycles or self-references in parentId are possible; handle them without crashing.

What to produce Write the core logic functions and explain your approach. Include time and space complexity, and describe the edge cases your solution handles.

Hints

Hint 1

Think of the work as a pipeline with three independent stages: build the tree once, then filter it, then sort it. Each stage should be a pure function that returns new objects rather than mutating the input.

Hint 2

For the filter, a pre-order DFS works well. When you reach a node that matches, you can stop descending — its entire subtree is already included. When a node doesn't match, keep searching its children anyway, because a reply deeper down might match.

Hint 3

localStorage can hold stale or corrupted data. Always validate the loaded value against your list of allowed fields and directions before using it, and fall back to your defaults if anything looks wrong.