Design a File System (Trie variant)

Data Structures
Medium
Salesforce
20.8K views

Design a file system class with `createPath` and `get` methods that efficiently handle path creation and lookup. This involves using a variant of a Trie or Hash Map.

Why Interviewers Ask This

Salesforce evaluates this question to assess a candidate's ability to model hierarchical data structures efficiently. They specifically look for proficiency in Trie implementation, path validation logic, and the trade-offs between space complexity and lookup speed when designing systems that manage nested resources like files or configuration trees.

How to Answer This Question

1. Clarify requirements: Confirm if paths are case-sensitive, handle duplicates, or support wildcards. 2. Propose a Trie structure: Explain why a Trie is superior to a flat Hash Map for prefix operations and memory efficiency in deep hierarchies. 3. Define node design: Describe each node storing a boolean flag for 'isPath' and a map of children nodes. 4. Implement createPath: Walk the tree splitting the string by slashes; create missing nodes only if they don't exist, ensuring no parent path was already marked as a file. 5. Implement get: Traverse the tree matching the path segments exactly and return true only if the final node has the 'isPath' flag set. 6. Analyze complexity: State time complexity as O(L) where L is path length and space as O(N*L).

Key Points to Cover

  • Explicitly validating that parent paths do not exist when creating leaf paths
  • Choosing a Trie over a Hash Map for hierarchical prefix optimization
  • Handling edge cases like empty strings or invalid separators gracefully
  • Demonstrating clear time and space complexity analysis relative to path length
  • Maintaining clean separation between traversal logic and state mutation

Sample Answer

To solve this, I would design a Trie-based File System class. First, let's clarify that we need to prevent creating a child path if a parent path already exists as a complete file, such as preventing '/a/b' if '/a' is already created. For the data structure, a Trie is ideal because it naturally represents the hierarchy and allows efficient prefix checks. Each node will contain a hash map for children and a boolean flag indicating if that specific path is valid. When implementing createPath, I split the input by slashes and iterate through the root. If a child node doesn't exist, I create it. Crucially, before marking a new path as valid, I must verify that no ancestor was previously marked as a terminal path. If the path already exists, I return false. For the get method, I simply traverse the tree using the path segments. If I reach the end and the current node's terminal flag is true, I return true; otherwise, false. This approach ensures both methods run in O(K) time, where K is the number of path components, making it highly scalable for Salesforce's large-scale resource management needs. The space complexity is proportional to the total number of unique characters across all stored paths.

Common Mistakes to Avoid

  • Failing to check if an intermediate node is already marked as a complete path before adding children
  • Using a single flat Hash Map which loses the hierarchical relationship and increases search costs
  • Not handling null pointers or missing segments during tree traversal leading to runtime crashes
  • Ignoring case-sensitivity requirements or special character escaping in path strings

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 49 Salesforce questions