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.

Google
Coding & Algorithms
Frontend Engineer
Google
November 29, 2026
Frontend Engineer
Technical Screen
Coding & Algorithms
Medium

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
doubly linked list
depth-first traversal
stack
pointer surgery
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: head of a doubly linked list whose nodes may have a child list.
  • Output: same head; list is flat, DFS order, all child None, prev consistent.
  • 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...


Submit Your Answer
Markdown supported