Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

import java.util.HashMap;

public class Party {

	private HashMap<Integer, String> indexToHostMap= null;
	private HashMap<String, Integer> hostToIndexMap= null;
	private int count;
	
	public Party() {

		count = 0;
		indexToHostMap = new HashMap<Integer, String>();
		hostToIndexMap = new HashMap<String, Integer>();
	}

	//Time Complexity is O(1)
	void addHost(String host) {

		indexToHostMap.put(count, host);
		hostToIndexMap.put(host, count); // host is unique
		count++;
	}

	//Time Complexity is O(1)	
	void removeHost(String host) {

		int index = hostToIndexMap.get(host); // get index

		indexToHostMap.remove(index); //remove index
		hostToIndexMap.remove(host);  //remove host

		if(count-1 == index) {
			// last element removed, no re-ordering required
			count--;
			return;
		}

		// Move last index element to removed index 
		String lastHost = indexToHostMap.get(count-1);
		indexToHostMap.remove(count-1);
		
		indexToHostMap.put(index, lastHost);
		hostToIndexMap.put(lastHost, index);
		count--;

	}

	//Time Complexity is O(1)
	public String getHost() {

		int random = (int) Math.random();
		random = random%count;
		return indexToHostMap.get(random);
	}

}

- Vishwas S P October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use set to store hostnames.
in getHost() method use iterator object & return iterator.next()

calling addHost & removeHost methods, will be less frequent.
calling getHost() will be frequent as with every request come to LB, it will call getHost

- Aniruddha September 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class LoadBalancer {

public static void main(String[] args) {
LB lb = new LB();

lb.addHost("a");
lb.addHost("b");
lb.addHost("c");
lb.addHost("d");
lb.addHost("e");
lb.addHost("f");

System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());

lb.removeHost("d");

System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
System.out.println(lb.getHost());
}
}

class LB {
private Set<String> ips;
private Iterator<String> iterator;

public LB() {
ips = new HashSet<String>();
iterator = ips.iterator();
}

public void addHost(String ip) {
ips.add(ip);
}

public void removeHost(String ip) {
ips.remove(ip);
iterator = ips.iterator();
}

public String getHost() {
if(!iterator.hasNext()) {
iterator = ips.iterator();
}
return iterator.next();
}
}

- Anonymous September 28, 2014 | 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