Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 3 vote

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.

enum type {NONE, MP3, AV4, WAV, AIFF, TGP};

class file
{
   public:
        string fileName;
        string filePath;
        type fileType;
        type fetchType(string name);
        file(string fileName, string filePath) : fileType(NONE), fileName(fileName), filePath(filePath) {fileType = fetchType(fileName);}
};

type file::fetchType(string name)
{
   int posDot = name.find('.');
   string extension(name, posDot+1); //convert tolower
   if(!extension.compare("aiff"))
        fileType = AIFF;
   else if(!extension.compare("mp3"))
        fileType = MP3;
   else if(!extension.compare("av4"))
        fileType = AV4;
   else if(!extension.compare("wav"))
        fileType = WAV;
   else if(!extension.compare("3gp"))
        fileType = TGP;
   return fileType;
}

class Player
{
   string playerName;
   public:
        Player(string name) : playerName(name) {}
        virtual void play(file* fileObj) =0;
};

void Player::play(file* fileObj) {
        if(!fileObj)
                cout<<"I'm "<<playerName<<" and I'm given an Invalid file to play\n";
        else 
        {    // Check for folder and file permissions here
             cout<<"I'm "<<playerName<<" and I'm playing : "<<fileObj->fileName<<" of type: "<<fileObj->fileType<<"\n";
        }
}

class MP3Player: public Player
{
   // MP3Player specific details here
   public:
        MP3Player() : Player("MP3Player") {cout<<"MP3Player instance created\n";}
        virtual void play(file* fileObj) { cout<<"... MP3Player playing...\n";}
};

//... MORE CLASSES HERE ...//

class TGPlayer : public Player
{
   // TGP specific details here
   public:
        TGPlayer() : Player("3GPPlayer") {cout<<"TGPlayer instance created\n";}
        virtual void play(file* fileObj) {cout<<"..TGPlayer playing..\n";}
};

void play(file name)
{
    Player* newPlayer = NULL;
    switch(name.fileType)
    {
        case MP3: newPlayer = new MP3Player();
                break;
        case AV4: newPlayer = new AV4Player();
                break;
        case WAV: newPlayer = new WavPlayer();
                break;
        case AIFF: newPlayer = new AIFFPlayer();
                break;
        case TGP: newPlayer = new TGPlayer();
                break;
        case NONE:
        default: cout<<"Invalid player type ..Aborting\n";
                return;
    }
    newPlayer->play(&name);
}

int main()
{
    string folder[3] = {"/path/folder1", "/path/folder2", "/path/folder3"};
    file input("music.aiff", folder[0]);

    play(input);
}
~

- YetAgain February 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- renegade403 January 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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:/");
	}

}

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

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)

- Manpreet February 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that we should use self balancing BST - to provide search by part of file name. We can't do it by hash map. I think it is more common case then user try to find song just by beginning of file name.

- Alexandr Yegorov February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

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

Main problem with hash is that this will limit many functionality of a player. Although it plays the selected file in o(1) look up time. Searching with few letters cannot be performed here.

- Psycho November 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is something called, Composite design pattern, I think interview would expecting that word from you.

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

Can you please explain this a bit in this perspective!!

- Psycho November 09, 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