Google VO Interview Coding Question: File System Tree Serialization

20 Views
No Comments

Given a file system:

root (id=1)
  dir (id=2)
    file1 (id=4): 100b
    file2 (id=5): 200b
  file3 (id=3): 300b

It will be represented as follows:

Filesystem = {1: { type: 'directory', name: 'root', children: [2, 3] },
  2: {type: 'directory', name: 'dir', children: [4, 5] },
  4: {type: 'file', name: 'file1', size: 100},
  5: {type: 'file', name: 'file2', size: 200},
  3: {type: 'file', name: 'file3', size: 300}
}

Return this JSON object.

This problem asks you to represent a file system tree as a JSON object keyed by node id. Directory nodes store their name and a list of child ids, while file nodes store their name and size. The key challenge is preserving the hierarchy and correctly distinguishing directories from files, which naturally fits a tree traversal or recursive construction.

END
 0