Google Interview: Filesystem Handling

2441
Nov 13, 2025
You have a file system represented as a Python dictionary. Each node is either a file or a directory. Directories can contain other directories or files as children. For example:
fs = { 
    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 } 
}
Tasks:
  1. Write a function get_size() that returns the total size of for a key
  2. How to optimise the size calculation with caching if needed
  3. Discussion about what is expected from a typical filesystem. I could come up with the following -
    1. No cycles: a directory cannot include itself directly or indirectly
    2. Every file belongs to at least one directory
    3. No two directories share the same child
    4. No directory contains a non-existent child key
Comments (3)