Design In-Memory File System

Hard · Trie · about 35 minutes

Asked at Snowflake, Coinbase, DoorDash, Uber, Shopify, Capital One, Vercel, Klarna, Revolut, Toast, Grab, Swiggy, Kraken.

Design an in-memory file system. Paths are absolute, like "/a/b/c".

  • FileSystem() creates an empty system with root "/".
  • ls(path): if path is a file, return a list with just its name; if it's a directory, return the names of

its files and directories in lexicographic order.

  • mkdir(path): create the directory, including any missing parents.
  • addContentToFile(filePath, content): create the file if needed (its parents exist or are created), and append

content.

  • readContentFromFile(filePath): return the file's content.

Related guides