Amazon Interview Question for Software Engineer / Developers


Country: United States




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

Seems like weighted graph is most suitable for data structure and finding max flow.

- Anonymous February 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time Complexity: O(n)

When some node is visted then compute net danger rank(NDR) and also add current node individual rank in the friend node net danger rank(NDR) then remove current node from the friendslist of friend .

for eg: if A has friends B and C
then acc to transitivity B also has friend A......
When you visit A then compute NDR of A with the help of its friends and also add A's IDR in B's NDR and remove A node from B's Friend List. So need of visiting A again while computing NDR for B.

public class PrisionerVO {
	
	private int idr;
	private int ndr;
	
	private List<Character> friendList;

	public int getNdr() {
		return ndr;
	}

	public void setNdr(int ndr) {
		this.ndr = ndr;
	}

	public int getIdr() {
		return idr;
	}

	public void setIdr(int idr) {
		this.idr = idr;
	}

	public List<Character> getFriendList() {
		return friendList;
	}

	public void setFriendList(List<Character> friendList) {
		this.friendList = friendList;
	}
	
	@Override
	public String toString() {
		
		return "\tIndividual Danger Rank:"+ idr +"\nNet Danger Rank:" + ndr+ "\nFriend List:"+ friendList+"\n---------------\n\n";
	}
	
}

public Character computerMaxDangerRank(Map<Character,PrisionerVO> prisionerMap){
		Set<Character> keySet = prisionerMap.keySet();
		
		int maxDR = 0;
		Character maxDRPrisioner = null;
		//key is prisionerName
		for(Character key:keySet){
			PrisionerVO currPrisioner = prisionerMap.get(key);
			List<Character> friendList = currPrisioner.getFriendList();
			
			//idr is individual danger rank
			int idr = currPrisioner.getIdr();
			
			//ndr is net danger rank of a prisioner
			int ndr = idr;
			if(friendList!=null && friendList.size()!=0)
			{
				for(Character friend:friendList){
					PrisionerVO friendPrisioner = prisionerMap.get(friend);	
					
					friendPrisioner.setNdr(friendPrisioner.getNdr()+idr);
					//adding friend idr current ndr
					ndr= ndr +  friendPrisioner.getIdr();
					//removing the current node from the friends list 
					friendPrisioner.getFriendList().remove(key);
					//i++;
				}
			}
			ndr = ndr + currPrisioner.getNdr();
			currPrisioner.setNdr(ndr);				
			
			if(ndr>maxDR){
				maxDR=ndr;
				maxDRPrisioner = key;
			}
		}
		
		return maxDRPrisioner;
	}

- Prateek February 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use the following data structure:

HashMap prisoners;

Class Prisoner {
	int dangerValue;
	int dangerRank;
	ArrayList friends;
}

N is the number of prisoners and V is the average number of friends for each prisoner.

To update the danger value of a prisoner, we need to update danger rank of its friends which takes O(V^2).

To find the most dangerous prisoner we iterate through the prisoner's list and retrieve the maximum which can be done in O(N).

Another solution is to sort the prisoners list by their danger rank. In this case, update is O(V*log(n) + V^2). Finding the most dangerous prisoner is O(1).

- rrahmati February 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I understood that danger Value not need update. Strange for me - "The danger RANK is computed as follows: ... So the danger VALUE of Prisoner 1 is 5+3+4 = 12. "

- zaqwes8811 February 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't it a union-find data structure problem??

- stormc February 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seems like Google PageRank Algorithm

- noob March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the question is somewhat flawed. if you dont have to recompute the danger value, then how can u assure the danger value is right as the graph is built

- catchclrs June 14, 2015 | Flag Reply


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