TikTok 面试真题解析:设计内存文件系统(In-Memory File System)|路径结构与数据结构设计题

33次阅读
没有评论

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"]

这道题重点考察数据结构设计能力,而不是算法复杂度。文件系统本质是一个树结构,每一层路径都是一个节点。mkdir 需要按路径逐层创建节点,缺失的中间目录也要自动补齐;ls 则需要返回当前节点下的子目录,并按字典序排序。难点在于路径解析和结构组织是否清晰,而不是计算本身。

正文完
 0