Flatten a Nested Drive Folder List With Child Pointers
Last updated: November 29, 2026
Quick Overview
A doubly linked list of Drive items has prev, next and child pointers, where child heads another doubly linked list of items inside that folder, nested to any depth. Flatten it in place into one doubly linked list in depth-first order with all child pointers cleared.
0
0
0 solved
A doubly linked list of Drive items has prev, next and child pointers, where child heads another doubly linked list of items inside that folder, nested to any depth. Flatten it in place into one doubly linked list in depth-first order with all child pointers cleared.
Asked at Google in a Drive file-tree rendering framing, where a nested tree has to become a flat scrollable list. The interviewer checks that prev pointers stay consistent and that you choose an iterative approach for deep nesting.
What the Interviewer Expects
- Produce depth-first order: a folder, then its contents, then its next sibling
- Fix both next and prev pointers at every splice
- Clear child pointers
- Use an explicit stack to avoid recursion limits on deep trees
- O(n) time
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
- Expand only folders the user has opened.
- Make it reversible so a folder can be collapsed again.
- The tree is too large to hold; flatten lazily as the user scrolls.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: head of a doubly linked list whose nodes may have a
childlist. - Output: same head; list is flat, DFS order, all
childNone,prevconsistent. - O(n) time....
High-Level Design
Walk the list with cur. When cur has a child, the child's list must be inserted between cur and cur.next. Push cur.next (if any) onto a stack so we can come back to it, link `cur.next = chil...