Amazon Interview Question for SDE1s


Team: Kindle
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
9
of 9 vote

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.

package filesystem;

import java.util.ArrayList;
import java.util.Date;

public class FileSystem {

    public class file {
        private String name;
        private long size;
        private Date timeStamp;
        private file currentDir;
        private file parentDir;

        // a directory is also a file containing reference to other files
        private boolean isDirectory;
        public ArrayList<file> subfiles;

        // Advanced class members if required
        private boolean[] permissions;
        private String owner;
        private String group;
           
        public file(String name, file currentDir, boolean isDir) {
            this.name = name;
            this.currentDir = currentDir;
            this.timeStamp = new Date();
            this.isDirectory = isDir;
            this.size = 0; // initial size
            this.parentDir = currentDir.getParentDirectory();
            if (isDir == true)
                this.subfiles = new ArrayList<file>();
        }
        
        public void updateTimeStamp() {
            this.timeStamp = new Date();
        }
        
        public file getParentDirectory() {
            return this.parentDir;
        }
        
        public void rename(String name) {
            this.name = name;
        }
    
    }
    
    private file root; // root folder

    public FileSystem() {
        // every file system should have a root folder
        this.root = new file("root", null, true);
    }
    
    public void createFile(String name, file curDir, boolean isDir) {
        file f = new file(name, curDir, isDir);
        curDir.subfiles.add(f);
    }
}

- CodeNameEagle April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Pretty nice solution man, thank you

what do u have in mind when u say

"private boolean[] permissions;"

- nr April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 :)

- Anonymous April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nr Sorry for replying as anonymous. I thought I was logged into my account. The above reply was mine.

- CodeNameEagle April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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 April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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 April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@CodeNameEagle that sounds pretty right to me. Boolean array or bitarray should work.. as we would just on/off the bits.

- nr April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 April 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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.

- CodeNameEagle April 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You haven't mentioned a function for a delete operation for a file or a folder, which in my view is an important part.

- Shrenik February 16, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- sharma.mohit09 April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i dont think we dont need to think all that.....
just a basic file system

- nr April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Your code doesn't have a function to delete a file or a directory. I think you are missing an important part

- Shrenik February 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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.

- Anonymous April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please be more descriptive

- nr April 22, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More