Problem · Design
ClayWorkspace Resource Tree
Learn this problemProblem statement
Process commands against an in-memory resource tree rooted at /. Paths are canonical absolute paths; resource names are nonempty and contain neither spaces nor /.
create path: create a new resource when its parent exists and its name is unused.list path: return direct child names in lexicographic order, formatted as[a,b].delete path: recursively remove that resource and its descendants.move source destinationParent: move the whole source subtree under the destination, preserving the source name.
Return OK for a successful mutation and ERROR for an invalid command. Missing paths, duplicate names, deleting or moving /, moving into one's own subtree, and destination-name collisions are invalid. Return one result for every command.
Function
processWorkspace(operations: String[]) → String[]Examples
Example 1
operations = ["create /team","create /team/api","create /team/web","list /team","move /team/web /","list /","delete /team","list /"]return = ["OK","OK","OK","[api,web]","OK","[team,web]","OK","[web]"]The web subtree moves to the root; deleting team also deletes api.
Example 2
operations = ["create /a","create /a","create /missing/x","delete /","move /a /a","list /"]return = ["OK","ERROR","ERROR","ERROR","ERROR","[a]"]Duplicate creation, a missing parent, root deletion, and moving into the source subtree are rejected.
Constraints
1 <= operations.length <= 100000- Every path starts with
/, has no trailing slash unless it is/, and has at most 100 segments. - The total number of path characters is at most
2000000.