I have the following question from an interview test (it has to be short and easy): I have to build a file system. I have to create a file object let's say it has an id. I have to create a Directory it has an id and I can add some assisting data. I can't use any java.util.Collections and I need to create all dast by myself. the problem is to find the optimal implementation of the directories hierarchy. the functions i need to implement are: addDirectory(parentDirectoryId) removeDirectory(DirectoryId), printAll() - print hierarchy of folders and files in a logical manner.
what I was thinking is to have a root Directory Directories and files beneath it. This will be implemented that each Directory D has a list of all directories that are it's direct subDirectory. and in each Directory to have a list of it's direct and indirect subDirectoriesId- so If I want to remove a directory I will go over the list of it's subDids and check if exists I'll continue down the tree till I reach the directory I want to remove. The problem is as follows: Using this implementation I'll have to update the lists of the id's recursively and it's a pain in the head. that after each addition or removal I'll have to go back up and update the lists. another problem is that The running time can be non efficient since In case I have a very wide and long tree and I have to remove or add a Directory that is at the leaves of all the Directories of the tree, I'll have to go over the whole tree in order to do that.
Can you please help me to find a better implementation of the DirectoryFile system. I have the freedom to implement it the way I want, I have really short time to do so. The tree structure I suggested is just an idea and I'm kinda stuck on it.