PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Nextdoor

Flatten Nested Comments

Last updated: Apr 17, 2026

Quick Overview

This question evaluates proficiency with tree traversal and hierarchical data manipulation, specifically recursion versus iterative approaches and enforcing depth limits when flattening nested structures.

  • easy
  • Nextdoor
  • Coding & Algorithms
  • iOS Engineer

Flatten Nested Comments

Company: Nextdoor

Role: iOS Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given a list of top-level posts, where each post may contain a list of nested comments, and each comment may itself contain more nested replies. Write a function that flattens this hierarchical discussion structure into a single array in depth-first order. Each node has the form: - `id`: unique identifier - `content`: text - `children`: array of child comments Example input: - Post A - Comment A1 - Reply A1a - Comment A2 - Post B A valid flattened output in DFS order would be: `[Post A, Comment A1, Reply A1a, Comment A2, Post B]` Follow-up 1: Modify the function so it only flattens nodes up to a given maximum depth. For example, if `maxDepth = 1`, include the root posts and their direct children, but do not include deeper replies. Follow-up 2: Implement the same flattening logic iteratively using a loop instead of recursion.

Quick Answer: This question evaluates proficiency with tree traversal and hierarchical data manipulation, specifically recursion versus iterative approaches and enforcing depth limits when flattening nested structures.

Related Interview Questions

  • Group Photos and Sort Feed Items - Nextdoor (medium)
  • Simulate Transaction Lock Scheduling - Nextdoor (hard)
  • Build Ranked Feed With Photo Batching - Nextdoor (medium)
  • Merge Weekly Time Intervals - Nextdoor (medium)
  • Merge overlapping weekly time intervals - Nextdoor (medium)
Nextdoor logo
Nextdoor
Jan 15, 2026, 12:00 AM
iOS Engineer
Technical Screen
Coding & Algorithms
2
0
Loading...

You are given a list of top-level posts, where each post may contain a list of nested comments, and each comment may itself contain more nested replies.

Write a function that flattens this hierarchical discussion structure into a single array in depth-first order.

Each node has the form:

  • id : unique identifier
  • content : text
  • children : array of child comments

Example input:

  • Post A
    • Comment A1
      • Reply A1a
    • Comment A2
  • Post B

A valid flattened output in DFS order would be: [Post A, Comment A1, Reply A1a, Comment A2, Post B]

Follow-up 1: Modify the function so it only flattens nodes up to a given maximum depth. For example, if maxDepth = 1, include the root posts and their direct children, but do not include deeper replies.

Follow-up 2: Implement the same flattening logic iteratively using a loop instead of recursion.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Nextdoor•More iOS Engineer•Nextdoor iOS Engineer•Nextdoor Coding & Algorithms•iOS Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.