Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
You should not inherit the player class. Player must be an interface so that different classes can call .initialize() and .play() functionality depending on what they are playing. Hardcoded inheritance of a class whose implementation varies for different types of files is a bad OOP design.
I have tried by recursion it is working file but giving stack overflow error in case no. of file is very large, I m trying by iterative method also I'll post it soon.
In this program i have taken one stack in which it will push and pop the folders and adding files in the array list.and printing.
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
class FileSearch {
private File file;
private ArrayList<File> files;
private LinkedList<File> folders;
private int count = 1;
public FileSearch(String path) {
file = new File(path);
files = new ArrayList<File>();
folders = new LinkedList<File>();
recurSearch(file);
// TODO Auto-generated constructor stub
}
public void compile(File file) {
if (file.isDirectory()&&!file.isHidden()) {
System.out.println("Folder Path>>>>>>>>>>" +
file.getAbsolutePath());
folders.add(file);
} else if (!file.isHidden()) {
System.out.println("File Path " + file.getAbsolutePath());
files.add(file);
}
}
public void recurSearch(File file) {
System.out.println("Cycle no: " + count++);
if (file.isDirectory() || !file.isHidden()) {
for (File file1 : file.listFiles()) {
compile(file1);
}
} else if (!file.isHidden()) {
files.add(file);
}
if (!folders.isEmpty()) {
recurSearch(folders.removeLast());
}else {
return;
}
}
}
public class RecursiveSearch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FileSearch("E:/");
}
}
Hi,
You can use: HashMap<String, ArrayList<File> files>.
Where, keys will be your play list name and values will be files associated with the play list, so that.
Searching for playlist will give O(1) and we known, for traversal to look for a song with arrays will be good. (Like hash)
Best way to use a hash table here, We can store all file names as key and file path as value. Once a user request to run a file, then simply search that file in hash table which will be O(1) search, this will give file path. From the file name we can find extension and from file path we can load file and play it. Based on the extension, we can load codecs which can run that song.
In constant time, we have file type, file path, file name and if codecs needed then that. This way we can make it run faster.
I think the focus is on OOP here. User provides a file from the list of folders/files and your system should be able to identify which type of file it is and select correct player to play it.
- YetAgain February 21, 2013