FastPrepBuild a File Tree from Streamed Records

Build a File Tree from Streamed Records

Nextdoor logoNextdoor● MediumFULLTIMEPHONE SCREEN

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 is size bytes.
  • PATH id: return the absolute path of node id.
  • SIZE id: return the total size of the files in the subtree of node id.

An ADD event returns nothing. Every PATH and SIZE event returns one string. Return those strings in event order.

Tree rules

  • The node whose parentId is 0 is 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 file a.txt inside a directory docs that 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 id returns the path when node id and every ancestor up to the root have arrived. Otherwise it returns UNRESOLVED.
  • SIZE id returns UNRESOLVED if node id has not arrived. Otherwise it returns, as a decimal string, the total size of every arrived file that reaches node id by following parent links through arrived nodes only. A file counts toward its own SIZE. If no file qualifies, the result is 0.

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 (ID 2) arrives, the file attaches to it, so SIZE 2 is 120.
  • PATH 4 is still UNRESOLVED because docs names parent 1, which has not arrived.
  • Once the root (ID 1) arrives, PATH 4 is /docs/notes.txt.
  • After readme.md (size 30) is added under the root, SIZE 1 is 120 + 30 = 150, and PATH 1 is /.

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 10 is 0: the chain from cat.png stops at the missing node 20, so the file does not reach the root yet.
  • SIZE 30 is 500 because cat.png is a child of photos, and PATH 40 is UNRESOLVED.
  • After home arrives, SIZE 10 becomes 500 and PATH 40 is /home/photos/cat.png.
  • Node 99 never arrives, so SIZE 99 is UNRESOLVED. PATH 10 is /.

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 id and every nonzero parentId is an integer from 1 to 10^9.
  • Each id is added at most once. At most one added node has parentId equal to 0, 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 size is an integer from 0 to 10^5.
  • PATH and SIZE may name any ID from 1 to 10^9, including one that never arrives.

More Nextdoor problems

See Nextdoor hiring insights
Saved
public String[] processFileRecords(String[] events) {
    // Write your code here.
}
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"]
expected["UNRESOLVED", "120", "UNRESOLVED", "/docs/notes.txt", "150", "/"]
Checking account…