Amazon Interview Question for Dev Leads


Country: India
Interview Type: In-Person




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

class Request{
	int quantity;
	double price;
	public Request(int q,double p){	
		quantity=q;
		price=p;
	}
}
class StockRequest{
	HashMap<String,List<Request>> buy;
	HashMap<String,List<Request>> sell;
	public StockRequest(){
		buy=new HashMap<String,List<Request>>();
		sell=new HashMap<String,List<Request>>();	
	}
	public void sellRequest(String name,int quantity,double price){
		Request req=new Request(quantity,price);
		boolean isDealDone=false;
		if(buy.containsKey(name)){
			List<Request> buyReq=buy.get(name);
			for(Request req:buyReq){
				if(req.quantity==quantity && req.price==price){
					buyReq.delete(req);
					isDealDone=true;
					break;
				}
			}
			
		}
		if(!isDealDone){
			List<Request> sellReqList;
			if(sell.containsKey(name)){
				sellReqList=sell.get(name);
			}
			else	sellReqList=new ArrayList<Request>();
			sellReqList.add(req);
		}
	}
	public void buyRequest(String name,int quantity,double price){
		Request req=new Request(quantity,price);
		boolean isDealDone=false;
		if(sell.containsKey(name)){
			List<Request> sellReq=sell.get(name);
			for(Request req:sellReq){
				if(req.quantity==quantity && req.price==price){
					sellReq.delete(req);
					isDealDone=true;
					break;
				}
			}
			
		}
		if(!isDealDone){
			List<Request> buyReqList;
			if(buy.containsKey(name)){
				buyReqList=buy.get(name);
			}
			else	buyReqList=new ArrayList<Request>();
			buyReqList.add(req);
		}
	}
}

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

class Request{
	int quantity;
	double price;
	public Request(int q,double p){	
		quantity=q;
		price=p;
	}
}
class StockRequestHandler{
	HashMap<String,List<Request>> buy;
	HashMap<String,List<Request>> sell;
	public StockRequestHandler(){
		buy=new HashMap<String,List<Request>>();
		sell=new HashMap<String,List<Request>>();	
	}
	public boolean sellRequest(String name,int quantity,double price){
		Request request=new Request(quantity,price);
		if(buy.containsKey(name)){
			List<Request> buyReq=buy.get(name);
			for(Request req:buyReq){
				if(req.quantity==quantity && req.price==price){
					buyReq.remove(req);
					return true;
				}
			}
			
		}
		
		List<Request> sellReqList;
		if(sell.containsKey(name)){
			sellReqList=sell.get(name);
		}
		else{	sellReqList=new ArrayList<Request>();
				sell.put(name, sellReqList);
		}
		sellReqList.add(request);
		return false;
	}
	public boolean buyRequest(String name,int quantity,double price){
		Request request=new Request(quantity,price);
		if(sell.containsKey(name)){
			List<Request> sellReq=sell.get(name);
			for(Request req:sellReq){
				if(req.quantity==quantity && req.price==price){
					sellReq.remove(req);
					return true;
				}
			}
			
		}
		List<Request> buyReqList;
		if(buy.containsKey(name)){
			buyReqList=buy.get(name);
		}
		else{	buyReqList=new ArrayList<Request>();
				buy.put(name, buyReqList);
		}
		buyReqList.add(request);
		return false;
	}

}

- nony February 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class User {
  Stockmarket sm;
  User(Stockmarket s1) {
	  sm = s1;
  }
  public void sendBuyRequest(String s, int p, int q) {
	  Request req= new Request(p,q);
	  sm.buyRequestHandler(s, req);
  }
  public void sendSellRequest(String s, int p, int q) {
	  Request req= new Request(p,q);
	  sm.sellRequestHandler(s, req);
  }
  
}

class Request implements Comparable<Request> {

	int price;
	int quantity;
	
	Request(int p, int q) {
		price = p;
		quantity = q;
	}

	@Override
	public int compareTo(Request o) {
		if(price == o.price && quantity == o.quantity) return 0;
		return 1;
	}
}
public interface Stockmarket {
 public void buyRequestHandler(String stock, Request req);
 public void sellRequestHandler(String stock, Request req);
}

public class StockServer implements Stockmarket {
     ConcurrentHashMap<String, List<Request>> BuyRequest = new ConcurrentHashMap<>();
     ConcurrentHashMap<String, List<Request>> SellRequest = new ConcurrentHashMap<>();

	
	 public void buyRequestHandler(String s,Request req) {
		 boolean requestCompleted = false;
		 if(SellRequest.containsKey(s) && SellRequest.get(s).contains(req)) {
			 requestCompleted = true;
			 SellRequest.get(s).remove(req);
		 }
		 else
			if(BuyRequest.containsKey(s))
				BuyRequest.get(s).add(req);
			else {
				List<Request> r = new ArrayList<Request>();
				r.add(req);
				BuyRequest.put(s, r);
			}
	 }
	 public void sellRequestHandler(String s, Request req) {
		 boolean requestCompleted = false;
		 if(BuyRequest.containsKey(s) && BuyRequest.get(s).contains(req)) {
			 requestCompleted = true;
			 BuyRequest.get(s).remove(req);
		 }
		 else
			if(SellRequest.containsKey(s))
				SellRequest.get(s).add(req);
			else {
				List<Request> r = new ArrayList<Request>();
				r.add(req);
				SellRequest.put(s, r);
			}
	 }
}

- Amit March 23, 2017 | 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