Amazon Interview Question
SDE1sTeam: Kindle
Country: United States
Interview Type: Phone Interview
Pretty nice solution man, thank you
what do u have in mind when u say
"private boolean[] permissions;"
Generally speaking permissions are like drwx rwx rwx. In the class above I used a boolean array to keep track of who all can do what with this file. As the owner of this file I can have rwx (read write execute) permissions, root will always have rwx permissions. d means if this file is a directory. You will see this if you do ls -lrt on unix. You can pass this information during file creation or you can also write a method to update it (which is like chmod on unix)
If you liked the answer please vote up :)
@nr Sorry for replying as anonymous. I thought I was logged into my account. The above reply was mine.
@CodeNameEagle thnx for reply again...
wht i meant was how is the permission being set(taking very basic implementation like the boolean array as u told)
@nr that is a very interesting question. I did not thought about it that much when I was writing my class because I was trying to imitate a file system. But now that I think of it, every file can have a default permission of -rw-rw-r-- (similarly directory can have a default of drw-rw-r). This is because root and the current user have read write permissions. Secondly you can code a chmod function that can manipulate this boolean array. if you say chmod (file, 777) it should change the boolean permission array of the file to 0111111111 (0 means its a file) or in other words -rwxrwxrwx
@CodeNameEagle that sounds pretty right to me. Boolean array or bitarray should work.. as we would just on/off the bits.
Hey @CodeNameEagle, just a doubt... what do u think about this doubt?
I should not be able to add file to a file, how do u think of preventing that
if(this.isDirectory){
curDir.subfiles.add(f);
}
@nr
That's a valid point. You see in the constructor above I check
if (isDir == true)
this.subfiles = new ArrayList<file>();
So in the add routine if you check the existence of subfiles member before adding, you are good to go.
It actually depends on how you want to add a file. I assumed that I am already supplying parent directory.
First we have to understand the basic structure and functionality of file system.:
root directory >> individual directory > sub folders> files
space of directory
folder options > create a new file
copy and paste
we can create a m-ary tree having leaves as file name and all internal.nodes as directories.For storing other concerned information like permissions,size we can use restructure node of trees.
A file system can be represented with a Tree data structure. We can have one class called file (a directory is also a file). This class can track its current directory, its parent directory and files in this directory (in case this file is a special file called directory). Then we can create a class to manage file system which is manipulating the nodes of the tree.
This is a bare minimum code to get you started.
- CodeNameEagle April 22, 2013