TikTok Interview Question: Design an In-Memory File System

14 Views
No Comments

Design a data structure that simulates an in-memory file system.

Implement the FileSystem class:

  • FileSystem() Initializes the object of the system
  • List<String> ls(String path)
    • Returns the list of directory names in this directory. The answer should be in lexicographic order.
  • void mkdir(String path)
    • Makes a new directory according to the given path. If the middle directories in the path do not exist, you should create them as well.
Example:FileSystem fileSystem = new FileSystem();
fileSystem.ls("/"); // return []
fileSystem.mkdir("/a/b/c");
fileSystem.ls("/"); // return ["a"]

This problem tests your ability to design a hierarchical data structure that behaves like a real file system. The core idea is to represent directories using a tree-like structure, where each path segment corresponds to a node. When creating directories, you must ensure intermediate folders are also created if missing. For listing operations, maintaining children in sorted order (or sorting before returning) ensures lexicographic output. The challenge lies more in clean data modeling than complex algorithms.

END
 0