Create and Query Hierarchical Path Values

Quick Overview

Create and Query Hierarchical Path Values. Implement FileSystem. A valid path is one or more segments, each a slash followed by one or more lowercase letters; empty strings and / alone are not paths.

Create and Query Hierarchical Path Values

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement FileSystem. A valid path is one or more segments, each a slash followed by one or more lowercase letters; empty strings and `/` alone are not paths. createPath(path,value) succeeds only if the path is new and its parent already exists, returning whether it was created. Top-level paths may be created directly. get(path) returns the stored value or -1 if absent. ```cpp class FileSystem { public: FileSystem(); bool createPath(string path, int value); int get(string path); }; ``` ### Examples **Example 1:** ```text Input: construct FileSystem(); createPath("/box",7); createPath("/box/item",9); get("/box/item"); createPath("/box",3) Output: true,true,9,false Explanation: The child has an existing parent; the final duplicate cannot replace its value. ``` **Example 2:** ```text Input: construct FileSystem(); createPath("/a/b",2); get("/a"); createPath("/a",5) Output: false,-1,true Explanation: Missing parent /a blocks the child creation, but a top-level path can then be created. ``` ### Constraints - Each supplied path is valid, has length 2 to 100, and uses lowercase letters and slashes. - Values lie in `[1,1000000000]`. - At most 10000 total createPath/get calls occur. ### Contract considerations Keep the native constructor, createPath and get methods. The implicit root has no public stored value or query interface. ```hint Read the boundary condition Can a failed creation or a lookup of a missing path create any path or change an existing value? ``` ### Boundary reminders Keep the native constructor, createPath and get methods. The implicit root has no public stored value or query interface.

Overview: Create and Query Hierarchical Path Values. Implement FileSystem. A valid path is one or more segments, each a slash followed by one or more lowercase letters; empty strings and / alone are not paths.

Google logo
Google
Jun 16, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement FileSystem. A valid path is one or more segments, each a slash followed by one or more lowercase letters; empty strings and / alone are not paths. createPath(path,value) succeeds only if the path is new and its parent already exists, returning whether it was created. Top-level paths may be created directly. get(path) returns the stored value or -1 if absent.

class FileSystem {
public:
    FileSystem();
    bool createPath(string path, int value);
    int get(string path);
};

Examples

Example 1:

Input: construct FileSystem(); createPath("/box",7); createPath("/box/item",9); get("/box/item"); createPath("/box",3)
Output: true,true,9,false
Explanation: The child has an existing parent; the final duplicate cannot replace its value.

Example 2:

Input: construct FileSystem(); createPath("/a/b",2); get("/a"); createPath("/a",5)
Output: false,-1,true
Explanation: Missing parent /a blocks the child creation, but a top-level path can then be created.

Constraints

  • Each supplied path is valid, has length 2 to 100, and uses lowercase letters and slashes.
  • Values lie in [1,1000000000] .
  • At most 10000 total createPath/get calls occur.

Contract considerations

Keep the native constructor, createPath and get methods. The implicit root has no public stored value or query interface.

Boundary reminders

Keep the native constructor, createPath and get methods. The implicit root has no public stored value or query interface.