An editor keeps its settings in a store addressed by paths such as /editor/font/size. A setting can only be created inside one that already exists, so /editor/font must exist before /editor/font/size can be added.
Implement class PathStore with these methods:
create(path, value) stores value at path and returns true. It returns false, and changes nothing, when path already exists or its parent does not.
get(path) returns the value stored at path, or undefined if the path does not exist.
children(path) returns the names of the direct children of path, sorted with the default string order (<), or null if the path does not exist.
remove(path) deletes path together with everything below it and returns how many paths were deleted (0 if it did not exist).
Rules:
A valid path is / followed by one or more segments separated by single / characters. A segment is one or more letters, digits, _ or -. There is no trailing /.
The root / always exists and holds no value: create("/", v) returns false, get("/") returns undefined, children("/") lists the top-level names, and remove("/") returns 0.
Throw a TypeError when a path is not a string or is not valid, and when create is given value === undefined. Any other value, including null, 0 and objects, can be stored.
Segment names are plain data: __proto__, constructor and toString are ordinary names.
Stores can hold 100,000 paths, and the tests make about 100,000 calls of each method. Each call must only touch the path it names (and, for remove, the paths it deletes), never every stored path.
const store = new PathStore();store.create("/editor", {}); // truestore.create("/editor/font/size", 14); // false: /editor/font does not exist yetstore.create("/editor/font", "mono"); // truestore.create("/editor/font/size", 14); // truestore.get("/editor/font/size"); // 14store.create("/editor/font", "sans"); // false: already existsstore.children("/editor"); // ["font"]store.remove("/editor"); // 3store.get("/editor/font"); // undefined
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
2
code listings
6
edge cases
What's inside
The full problem, with examples
Tests that check your solution
Step-by-step
Approach
Solution
Complexity
Edge cases
Follow-up questions
Card worldwide
UPI and net banking in India
One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.