Design a File System (Hash Map and Set)

Data Structures
Medium
Amazon
27.9K views

Design a simplified file system that supports two operations: `addPath` (to create a directory/file path) and `get` (to retrieve a value associated with the path). Use a Hash Map to store paths and values.

Why Interviewers Ask This

Amazon asks this question to evaluate a candidate's ability to select appropriate data structures for specific constraints and to demonstrate clear communication of trade-offs. They are testing if you can efficiently handle path-based lookups while managing memory, ensuring the solution scales logically without unnecessary complexity.

How to Answer This Question

1. Clarify requirements: Ask if paths are case-sensitive, if duplicates overwrite values, and how deeply nested paths should be handled. 2. Propose the core structure: Suggest using a Hash Map where keys are full file paths (strings) and values are the associated data, noting O(1) average time complexity for both insertion and retrieval. 3. Discuss edge cases: Address invalid inputs like null paths, empty strings, or attempts to add paths that already exist. 4. Consider scalability: Briefly mention that while a Hash Map is efficient, a Trie might be better if prefix searches were required, but stick to the Hash Map as requested. 5. Implement and walk through code: Write clean pseudocode or actual code, explaining variable names and logic flow clearly to Amazon's leadership principles of 'Dive Deep' and 'Deliver Results'.

Key Points to Cover

  • Selecting a Hash Map provides O(1) average time complexity for both add and get operations.
  • Using the full path string as the unique key simplifies the logic compared to tree traversal.
  • Handling edge cases like null inputs or duplicate paths shows attention to detail.
  • Explaining why a Hash Map is chosen over a Trie demonstrates architectural decision-making skills.
  • Writing clean, readable code reflects Amazon's expectation for maintainable and scalable solutions.

Sample Answer

To design this simplified file system, I will start by clarifying the requirements. We need an `addPath` method to store a value at a specific string path and a `get` method to retrieve it. Since we only need exact matches and not prefix searches, a Hash Map is the optimal choice. The keys will be the full path strings, such as '/home/user/docs', and the values will be the stored data. This ensures O(1) average time complexity for both operations, which aligns with Amazon's focus on efficiency. For the implementation, I will initialize a private HashMap instance. In the `addPath` function, I will check if the input path is valid; if so, I will insert the key-value pair directly. If the path already exists, I will overwrite the existing value unless specified otherwise. The `get` function will simply return the value associated with the key or a default null if the key does not exist. I must also handle edge cases like empty strings or paths with leading slashes. While a Trie could support wildcard searches, the problem specifically requests a Hash Map for direct path storage, making it the most straightforward and performant solution for these exact constraints. This approach demonstrates a deep understanding of data structure selection based on operational needs.

Common Mistakes to Avoid

  • Overcomplicating the design by implementing a Trie when the requirement only asks for exact path matching.
  • Failing to validate input parameters, leading to potential runtime errors with null or empty strings.
  • Ignoring the scenario where a path already exists and deciding arbitrarily whether to overwrite or reject.
  • Not discussing time and space complexity, missing the opportunity to show analytical depth.

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 73 Amazon questions