Back to problems

Build a Nested Comment Tree from Flat Records

Algorithm · ByteDance · Medium

Build Nested Comments Medium · Topics · Company Tags · Hints You are building the backend for a discussion platform. The database stores each comment as a flat record containing three fields: commentId (a unique integer identifier), parentId (an integer that points to the parent comment, or null for top-level posts), and content (the body text). To render a threaded view, you must restructure the flat array into a proper tree where every reply nests underneath its parent.…

Checking your access…