Design a hierarchical path registry
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: Design a hierarchical path registry evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- The root '/' exists implicitly and holds value 0.
- A path can be created only if its immediate parent already exists.
- Creating a path that already exists fails (returns False).
- Path segments consist of lowercase letters; '/' is the separator.
- get on a non-existent path returns -1.
- 0 <= number of operations <= 10^5.
Examples
Input: ([('create', '/a', 1), ('get', '/a'), ('get', '/b')],)
Expected Output: [True, 1, -1]
Explanation: Create /a=1 under root succeeds; get /a returns 1; /b was never created so get returns -1.
Input: ([('create', '/a/b', 5)],)
Expected Output: [False]
Explanation: Parent /a does not exist, so creating /a/b fails.
Input: ([('create', '/a', 1), ('create', '/a/b', 2), ('get', '/a/b'), ('get', '/a')],)
Expected Output: [True, True, 2, 1]
Explanation: Create /a then /a/b (parent now exists), then read both back.
Input: ([('create', '/a', 1), ('create', '/a', 9), ('get', '/a')],)
Expected Output: [True, False, 1]
Explanation: Second create of the existing /a fails; the original value 1 is unchanged.
Input: ([('get', '/')],)
Expected Output: [0]
Explanation: The implicit root '/' holds value 0.
Input: ([],)
Expected Output: []
Explanation: No operations yields an empty result list.
Input: ([('create', '/x', -7), ('create', '/x/y', 3), ('create', '/x/y/z', 0), ('get', '/x/y/z'), ('get', '/x/y'), ('get', '/x'), ('get', '/x/y/w')],)
Expected Output: [True, True, True, 0, 3, -7, -1]
Explanation: Deep chain creation with a negative value and a zero value; the missing sibling /x/y/w returns -1.
Input: ([('create', '/a/b/c', 4), ('get', '/a/b/c')],)
Expected Output: [False, -1]
Explanation: Creating a deep path whose ancestors are absent fails; the path is therefore never stored, so get returns -1.
Hints
- Store every path as a key in a hash map from path-string to integer value; the root '/' starts already present with value 0.
- Derive the parent of a path by cutting at the last '/'. The parent of a top-level path such as '/a' is the root '/'.
- Before creating, check both that the path is absent AND that its parent is present; only then insert. This makes the parent-must-exist rule a single membership test.