4

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.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
mary
  • 869
  • 5
  • 13
  • 26
  • 2
    Um, it sounds like you're tasked with this _right now_ and are basically cheating. – Matt Ball Dec 16 '11 at 16:42
  • nop. the test is not now. really. – mary Dec 16 '11 at 16:48
  • and I'll never cheat on an interview. – mary Dec 16 '11 at 16:49
  • 1
    Why do you have a really short time to do this? – Amir Afghani Dec 16 '11 at 16:50
  • 3
    "Optimal"--optimized for what? In any case, there's a ton of research regarding file systems and optimal data structures. I'm not sure what recursion you're talking about; you'd only need to update the containing directory, unless you're going to implement symlinks a directory can only appear in one location. – Dave Newton Dec 16 '11 at 16:51
  • I don't know what symlinks are. but recursion in the following way: let say I have a root that has b as a son and C as a grandson and C has two sons D and F. if I want to remove C I'll have to update the list of id's in B that it no longer has D and F and C and same for the root Directory of the tree. optimal meaning not having to go over the whole tree in order to delete from it – mary Dec 16 '11 at 16:52
  • I have like 2 hours for design and implementation for the whole thing. really is there a simple and easy implementation for this. It's an interview I don't think they want something hard. – mary Dec 16 '11 at 16:56
  • 2
    @mary You said that each directory had a list of its immediate subdirectories; if you remove `c` the only thing you need to update is `c`'s parent. Oh, no, you said it has both; never mind. Huh, well, good luck--1:45 left. – Dave Newton Dec 16 '11 at 16:59
  • it's an interview I failed and I wanted to learn from my mistakes for the next time. – mary Dec 16 '11 at 17:01
  • I don't understand why a directory needs to know all its descendants. Are we talking about an in-memory structure mimicking a filesystem, or an actual, physical filesystem? – Andrew Spencer Dec 16 '11 at 17:22
  • I think they wanted to test my ability to think object oriented and to think of a design and to implement it. you can think of it as an exercise that tell you, please set a design that best describes relationship between folders in an operating system. for one you can think of as a tree or as some other structure let's say a list. the thing you are given that a file can be in some folder and a folder can have subfolders and that's it. That's what i'm asking- if there is a better implementation that doesn't have to do with having a descendants list- I think I need those because otherwise – mary Dec 16 '11 at 17:49
  • I'm blind to what I have in a subfolder. so In order to search for something I'll have to go over the whole tree. I need some better implementation whether it's a tree or not. – mary Dec 16 '11 at 17:49
  • I'm going to add a reference to the Directory's parent and by this to avoid the recursion !:) – mary Dec 16 '11 at 18:33
  • Without a specification of what is to be optimized and request for "optimal" anything is incomplete. – dmckee --- ex-moderator kitten Dec 16 '11 at 23:06

2 Answers2

0

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

MOSS is bound to have a good way of doing what you need. And since the interview question only requires you to do a subset of what MOSS does, then you can just read up on the parts that do what you need, and then develop your own solution from that.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
0

Well, considering Map not being a Collection, that might be a good loophole to start with ;)

Henrik Paul
  • 66,919
  • 31
  • 85
  • 96