Optimal algorithm and data structure for simulating a file system

I have the following question from an interview test (it should be short and easy): I need to create a file system. I need to create an object file, let it have an identifier. I have to create a directory that has an identifier, and I can add some auxiliary data. I can not use any java.util.Collections, and I need to create all the dast myself. the problem is to find the optimal implementation of the directory hierarchy. functions that I need to implement: addDirectory (parentDirectoryId) removeDirectory (DirectoryId), printAll () - logically prints the hierarchy of folders and files.

I thought you had the root directories of the directories and the files under it. It will be realized that each directory D has a list of all directories that are its direct subdirectory. and each directory has a list of its direct and indirect subDirectoriesId-so. If I want to delete a directory, I will go through the list of its subDids and check if there is a file that I will continue to the tree until I reach the directory that I want to delete. The problem is this: Using this implementation, I will have to update id lists recursively, and this is a pain in the head. that after each addition or removal I will have to go back and update the lists. Another problem is that the runtime can be inefficient, because if I have a very wide and long tree, and I need to delete or add a directory that is located at the leaves of all the directories of the tree, I will have to go all over the tree, to do this.

Could you help me find a better implementation of DirectoryFile. I have the freedom to implement it the way I want, I really have a very short time for this. The proposed tree structure is just an idea, and I got a little stuck on it.

+4
source share
2 answers

I'm not sure there is a quick answer to this question, but you can check the download links on this page that give you the source code for MOSS, a file system simulator written in Java.

MOSS must have a good way to do what you need. And since the question of interviewing only needs to do a subset of what MOSS does, then you can just read the parts that do what you need, and then develop your own solution.

0
source

Well, given that Map is not a collection, this may be a good loophole to begin with;)

0
source

Source: https://habr.com/ru/post/1386737/


All Articles