HomeChallengesRepsProblemsLeaderboardTake a challenge

Threaded Comments

The API returns a flat list with parentId. The UI needs it nested. This is the same problem as a category menu, an org chart and a file browser.

Intermediate20 min on the clock5 graded checks · 100 pointsData Structures at Work
Graded by running your code against real cases.

What you build

  • buildTree(flat) — the root nodes, each with a children array
  • findPath(flat, id) — the ids from the root down to that node
  • maxDepth(nodes) — how deep the deepest thread goes
  • flatten(nodes) — back to a flat list of ids, parent before children
  • One case nests 20,000 comments on a time budget

Done means

The nesting is right, the path is right, and 20,000 comments nest inside the budget.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. buildTree nests the list+30Trees and recursion
  2. findPath walks from the root to the node+25Trees and recursion
  3. maxDepth measures the deepest thread+15Trees and recursion
  4. flatten puts it back, parent before children+20Trees and recursion
  5. It nests 20,000 comments in time+10The cost of a loop inside a loop

What it teaches

Trees and recursionintermediate

A flat list with a parentId is a tree pretending not to be one, and turning it into real nesting is the same problem behind threaded comments, category menus, org charts and file browsers.

The cost of a loop inside a loopintermediate

A search inside a loop does the work once per row per row: at 20,000 rows that is 400 million comparisons, and it is the single most common reason a working feature is unusably slow.

The rest of Data Structures at Work