Design a File System (Hash Map and Set)

Data Structures
Medium
Amazon
28K 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 match…

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.

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 184 Amazon questions