Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Flatten Threaded Comments for Rendering
00:00
5 left

Flatten Threaded Comments for Rendering

MediumPython

Problem

Write a function to flatten a deeply nested dictionary representing a threaded comment structure into a flat list for rendering.

Each comment is a dictionary with id, body, and an optional replies list containing child comment dictionaries. Return a preorder list of dictionaries containing each comment's id, body, and nesting depth, without mutating the input. Preserve the order of replies.

Function

def flatten_comments(comment):

Constraints

  • The input is a dictionary containing string fields id and body.
  • replies is either absent or a list of valid comment dictionaries.
  • The total number of comments is at most 1,000.
  • The comment structure is a tree with no cycles.
  • The input must not be mutated.

Function Signature

def flatten_comments(comment):
Interviewer

Your question is Flatten Threaded Comments for Rendering. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.