Build a File Tree from Streamed Records
Problem statement
File-system records arrive as a stream. Each record describes one node and names its parent by ID, but a child can arrive before its parent. Build the tree as records arrive and answer queries along the way.
Process the array events from left to right. Each event has one of these forms:
ADD id parentId DIR name: add a directory node.ADD id parentId FILE name size: add a file node whose size issizebytes.PATH id: return the absolute path of nodeid.SIZE id: return the total size of the files in the subtree of nodeid.
An ADD event returns nothing. Every PATH and SIZE event returns one string. Return those strings in event order.
Tree rules
- The node whose
parentIdis0is the root directory. Its path is/, and its name is not part of any path. - Any other node's path is its parent's path followed by its own name. Names are joined with
/, so a filea.txtinside a directorydocsthat sits under the root has path/docs/a.txt. - A record may name a parent that has not arrived yet. Keep the child, and attach it to that parent as soon as the parent arrives.
Query results
PATH idreturns the path when nodeidand every ancestor up to the root have arrived. Otherwise it returnsUNRESOLVED.SIZE idreturnsUNRESOLVEDif nodeidhas not arrived. Otherwise it returns, as a decimal string, the total size of every arrived file that reaches nodeidby following parent links through arrived nodes only. A file counts toward its ownSIZE. If no file qualifies, the result is0.
Function
processFileRecords(events: String[]) → String[]Examples
Example 1
events = ["ADD 4 2 FILE notes.txt 120", "PATH 4", "ADD 2 1 DIR docs", "SIZE 2", "PATH 4", "ADD 1 0 DIR root", "PATH 4", "ADD 3 1 FILE readme.md 30", "SIZE 1", "PATH 1"]return = ["UNRESOLVED", "120", "UNRESOLVED", "/docs/notes.txt", "150", "/"]The file notes.txt (ID 4) arrives first, but its parent 2 has not, so the first PATH 4 is UNRESOLVED.
- After
docs(ID2) arrives, the file attaches to it, soSIZE 2is120. PATH 4is stillUNRESOLVEDbecausedocsnames parent1, which has not arrived.- Once the root (ID
1) arrives,PATH 4is/docs/notes.txt. - After
readme.md(size30) is added under the root,SIZE 1is120 + 30 = 150, andPATH 1is/.
Example 2
events = ["ADD 10 0 DIR root", "ADD 30 20 DIR photos", "ADD 40 30 FILE cat.png 500", "SIZE 10", "SIZE 30", "PATH 40", "ADD 20 10 DIR home", "SIZE 10", "PATH 40", "SIZE 99", "PATH 10"]return = ["0", "500", "UNRESOLVED", "500", "/home/photos/cat.png", "UNRESOLVED", "/"]The root (ID 10) arrives first. photos (ID 30) arrives before its parent home (ID 20), and cat.png (ID 40) arrives inside photos.
SIZE 10is0: the chain fromcat.pngstops at the missing node20, so the file does not reach the root yet.SIZE 30is500becausecat.pngis a child ofphotos, andPATH 40isUNRESOLVED.- After
homearrives,SIZE 10becomes500andPATH 40is/home/photos/cat.png. - Node
99never arrives, soSIZE 99isUNRESOLVED.PATH 10is/.
Example 3
events = ["ADD 7 5 FILE a.log 9", "SIZE 7", "PATH 7", "SIZE 5"]return = ["9", "UNRESOLVED", "UNRESOLVED"]Only the file a.log (ID 7, size 9) has arrived. A file counts toward its own size, so SIZE 7 is 9. Its parent 5 never arrives, so PATH 7 and SIZE 5 are both UNRESOLVED.
Constraints
1 <= events.length <= 2000- Every event follows one of the four forms, with single spaces between fields.
- Every
idand every nonzeroparentIdis an integer from1to10^9. - Each
idis added at most once. At most one added node hasparentIdequal to0, and it is a directory. - When a node's parent arrives, it is a directory. Parent links never form a cycle.
- Two children of the same parent never share a name.
- Each name has 1 to 20 characters chosen from lowercase letters, digits,
.,_, and-. - Every file
sizeis an integer from0to10^5. PATHandSIZEmay name any ID from1to10^9, including one that never arrives.