Resolve Root Post and Depth for Nested Comments from Parent Pointers

Last updated: November 29, 2026

Quick Overview

A comments table has (comment_id, parent_id, post_id_hint) where parent_id is either another comment or null for top-level comments, and post_id_hint is only filled on top-level rows. Using pandas joins, compute each comment's depth and root post, flagging orphans whose ancestor chain hits a deleted comment and any cycles caused by bad data.

Meta
Data Manipulation (SQL/Python)
Data Engineer
Meta
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


A comments table has (comment_id, parent_id, post_id_hint) where parent_id is either another comment or null for top-level comments, and post_id_hint is only filled on top-level rows. Using pandas joins, compute each comment's depth and root post, flagging orphans whose ancestor chain hits a deleted comment and any cycles caused by bad data.

Facebook and Instagram comment threads are stored as parent pointers, and analytics like 'engagement by reply depth' need each comment's root and depth. In an onsite data round, this tests iterative self-merges (pointer jumping) in pandas rather than a per-row Python recursion, plus robust handling of missing parents and cycles.

What the Interviewer Expects
  • Model the data as a forest and walk parent pointers with repeated merges
  • Use pointer doubling or level-by-level merges with a bounded number of iterations
  • Detect missing parents (deleted comments) as orphans
  • Detect cycles by an iteration cap and report them
  • Keep the solution vectorized and explain its complexity
Key Topics to Cover
self-merge
pointer jumping
trees in tables
cycle detection
data quality
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you do this in SQL with a recursive CTE, and what are its limits in Presto?
  • How would you maintain depth incrementally as new comments arrive?
  • How would you compute subtree sizes (total replies under each comment)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Depth is 0 for a top-level comment. A comment whose parent_id is not in the table points at a deleted comment: it is an orphan, depth unknown. A chain that never reaches a top-level comment within the...

Approach

Keep a frame with the current ancestor pointer anc (starting at parent_id) and hops (starting at 1 for replies). Each iteration merges anc onto the table to fetch that ancestor's own parent; if ...


Submit Your Answer
Markdown supported