PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates understanding of hierarchical data modeling and path-based mapping, exercising skills in tree-like data structures, associative maps, and string path handling.

  • medium
  • Harvey
  • Coding & Algorithms
  • Software Engineer

Implement a Hierarchical File System

Company: Harvey

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement an in-memory hierarchical file system. Create a class `FileSystem` with the following methods: - `FileSystem()` initializes an empty file system. The root path `/` always exists. - `boolean createPath(String path, int value)` creates a new path and associates it with `value`. - `path` is an absolute path that starts with `/`. - Path components are non-empty lowercase strings separated by `/`, for example `/a`, `/a/b`, or `/projects/demo`. - The method returns `true` if the path is successfully created. - The method returns `false` if the path already exists, if the parent path does not exist, or if the path is invalid. - The root path `/` cannot be created. - `int get(String path)` returns the value associated with `path`, or `-1` if the path does not exist. Example: ```text FileSystem fs = new FileSystem(); fs.createPath("/a", 1) -> true fs.get("/a") -> 1 fs.createPath("/a/b", 2) -> true fs.get("/a/b") -> 2 fs.createPath("/c/d", 3) -> false, because /c does not exist fs.createPath("/a", 4) -> false, because /a already exists fs.get("/missing") -> -1 ``` Discuss the data structures you would use and the time complexity of each operation.

Quick Answer: This question evaluates understanding of hierarchical data modeling and path-based mapping, exercising skills in tree-like data structures, associative maps, and string path handling.

Design an in-memory hierarchical file system. The root path '/' always exists, but it does not store a value. You must support three operations: FileSystem(), createPath(path, value), and get(path). Instead of implementing a class directly, write a function that simulates these operations and returns the result of each one. A path is valid only if it is absolute, starts with '/', is not the root when creating, and every component is a non-empty lowercase string of letters. createPath should return false if the path already exists, if its parent path does not exist, or if the path is invalid. get should return the stored value, or -1 if the path does not exist or is invalid. A good solution uses a hash map from full path to value, which lets you check parents and retrieve values efficiently.

Constraints

  • 1 <= len(operations) <= 10^4
  • operations[0] == 'FileSystem'
  • 1 <= len(path) <= 100
  • 1 <= value <= 10^9
  • Paths may be invalid and must be handled according to the rules

Examples

Input: (['FileSystem', 'createPath', 'get', 'createPath', 'get', 'createPath', 'createPath', 'get'], [[], ['/a', 1], ['/a'], ['/a/b', 2], ['/a/b'], ['/c/d', 3], ['/a', 4], ['/missing']])

Expected Output: [None, True, 1, True, 2, False, False, -1]

Explanation: Creates /a and /a/b successfully, fails to create /c/d because /c does not exist, fails to recreate /a, and returns -1 for a missing path.

Input: (['FileSystem', 'createPath', 'createPath', 'get', 'createPath', 'get'], [[], ['/', 1], ['/ab/', 2], ['/'], ['/x', 5], ['/x']])

Expected Output: [None, False, False, -1, True, 5]

Explanation: The root cannot be created, a trailing slash makes '/ab/' invalid, get('/') returns -1 because root has no value, and /x is created normally.

Input: (['FileSystem', 'createPath', 'createPath', 'createPath', 'createPath', 'get', 'get'], [[], ['/a', 1], ['/a//b', 2], ['/A', 3], ['/a/c', 4], ['/a/c'], ['/A']])

Expected Output: [None, True, False, False, True, 4, -1]

Explanation: Double slashes create an empty component, uppercase letters are invalid, and a valid child '/a/c' can still be created under existing '/a'.

Input: (['FileSystem', 'createPath', 'createPath', 'createPath', 'get', 'get'], [[], ['/projects/demo', 7], ['/projects', 5], ['/projects/demo', 7], ['/projects'], ['/projects/demo']])

Expected Output: [None, False, True, True, 5, 7]

Explanation: A nested path cannot be created before its parent. After creating '/projects', creating '/projects/demo' succeeds.

Hints

  1. You do not need a full tree traversal for this API. Storing each complete path in a hash map is enough.
  2. To create a path like '/a/b/c', only the immediate parent '/a/b' needs to exist. You can find it using the last '/'.
Last updated: May 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Spreadsheet Cell Updates - Harvey (hard)
  • Evaluate Symbol Expressions - Harvey (medium)
  • Implement a Cursor-Based Text Editor - Harvey (medium)
  • Highlight Exact Source Matches - Harvey (medium)
  • Implement a Formula Spreadsheet - Harvey (medium)