Code RoomComment thread depth
EasyPrep Room Coding #4743

Comment thread depth

CodingAlgorithms & data structuresEntry–Mid~15 min

A discussion board keeps every comment on an article in one chronological list. Entry i of parent_of holds the position of the comment that comment i replies to, or -1 when comment i starts a new thread at the top level. An article can carry several top level comments, and moderators reinstate hidden replies, so a comment is not guaranteed to appear after the one it answers: a parent can sit at a later position than its child. The renderer needs an indent level for every comment. Return a list of the same length in which entry i is the depth of comment i, counting 0 for a top level comment, 1 for a direct reply, 2 for a reply to a reply, and so on. Keep the results in input index order. The input always describes a valid structure with no cycles.

Implement
comment_reply_depths(parent_of: list[int]) → list[int]
Examples
in[[-1,0,0,1]]out[0,1,1,2]
in[[-1,-1,1]]out[0,0,1]
in[[2,-1,1]]out[2,0,1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
[[-1,0,0,1]][0,1,1,2]not run yetsample
[[-1,-1,1]][0,0,1]not run yetsample
[[2,-1,1]][2,0,1]not run yetsample