Reconstruct the Visible Thread in a Branching Chat History
Last updated: November 29, 2026
Quick Overview
Messages in a conversation form a tree (editing a prompt or regenerating an answer creates a sibling branch). Given messages with id, parent and creation time, plus the user's explicit branch selections, return the ids along the thread the UI should display, from root to leaf.
OpenAI
November 29, 20260
0
0 solved
Messages in a conversation form a tree (editing a prompt or regenerating an answer creates a sibling branch). Given messages with id, parent and creation time, plus the user's explicit branch selections, return the ids along the thread the UI should display, from root to leaf.
ChatGPT lets users edit earlier prompts and regenerate answers, which turns a conversation into a tree where only one root-to-leaf path is shown. OpenAI interviewers use this to check tree construction from parent pointers, a clear default rule for unselected branches, and defensive handling of bad selections or malformed data.
What the Interviewer Expects
- Build a children adjacency list from parent pointers in one pass
- Define the default branch (newest child) and a deterministic tie-break
- Validate that a selection is actually a child of that node
- Guard against cycles or duplicate ids in corrupted data
- Return the path in root-to-leaf order
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Show the branch indicator '2 / 3' for each message on the path: what extra data do you compute?
- The conversation has 50,000 messages and the client only needs the last 50 on the path: can you avoid walking from the root?
- How would you store selections so they sync across devices without conflicts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Each message:
id,parent(None for root),created. selected[parent_id] = child_idrecords a branch the user chose; otherwise show the newest child.- If several roots exist (bad data), use...
High-Level Design
Index messages by id and group them by parent. Starting from the root, repeatedly pick the next child: the selected one if it exists and really is a child of the current node, else the child with the ...