Build an Event-Driven Folder Browser
UiPathFrontend Engineer · OnsiteLast updated
This problem tests your ability to model hierarchical data structures and design an efficient state management strategy for an interactive UI component.
The Problem: Event-Driven Folder Browser
You are building the core engine for an interactive, recursive folder browser. You must design a state model and the necessary helper functions to manage a tree of folders. Each folder has a unique id, a name, an optional parentId, and an array of children (subfolders).
Requirements
- Data Model: Define a structure that allows O(1) or O(log n) lookup of any folder by ID.
- Operations: Implement the following functions:
addFolder(parentId, name): Adds a new folder and returns the new state.moveFolder(folderId, newParentId): Moves a folder to a new parent. This must include an invalid-cycle prevention check (a folder cannot be moved into its own descendant).deleteFolder(folderId): Removes a folder and its entire subtree.
Interface
# Example state structure
# state = { 'folders': { 'id': { 'id': '...', 'name': '...', 'parentId': '...', 'children': [] } }, 'rootIds': [...] }
def add_folder(state, parent_id, name):
pass
def move_folder(state, folder_id, new_parent_id):
pass
def delete_folder(state, folder_id):
pass
Constraints
- The tree depth is unbounded.
- The number of operations can reach 10,000.
- Avoid unnecessary deep cloning of the entire state; prefer immutable updates or a normalized state structure.
Example
Input: Move folder 'B' into folder 'C', where 'C' is a child of 'B'. Output: Operation must be rejected (Cycle detected).
Edge Cases
- Moving a folder to its existing parent.
- Deleting the root folder.
- Renaming a folder while it is being dragged or moved.
Hints
Hint 1
Normalize your state by storing folders in a flat dictionary keyed by ID, with a separate list or set representing the tree hierarchy.
Hint 2
To prevent cycles during a move, traverse the prospective parent's ancestors; if you encounter the folder being moved, the operation is invalid.
Hint 3
To optimize delete operations, ensure that your data model allows for easy removal of a folder's ID from its parent's children array.
Follow-up questions
- How would you optimize the tree rendering to avoid re-rendering the entire UI when only one node changes?
- If this data were persisted in a relational database, what schema would you use to efficiently fetch a subtree without loading the entire database into memory?
- How would you implement keyboard navigation (e.g., arrow keys) to collapse/expand nodes and move focus?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.