Amazon Interview Question for SDE-2s


Team: AWS Infrastructure Planning, Analysis and Optimization
Country: United States
Interview Type: In-Person




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

1. Create Abstract class Item(it will have price and abstract prepare() method)
2. Create Sawdwich class,It will be extending Item class
3. Create Order class,It will have List<item> ,Customer Info
4. Robat as client,for accepting order,it will have list of orders,And will prepare items calling item's prepare method.

- Anonymous January 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good design. I write Java code to implement your idea. This is just a simple example.

public abstract class Item{

	protected int itemId;	
	protected String itemName;	
	protected double price;
	protected int quantity;
	
	public int getItemId(){
		return itemId;
	}
	
	public String getItemName(){
		return itemName;
	}
	
	public double getTotal(){
		return quantity * price;
	}
	
	public abstract void prepare();

}

public class Sandwich extends Item{

	private static final int ID = 10082;
	private static final String NAME = "sandwich";
	private static final double PRICE = 2.99;
	
	public Sandwich(int qty){
		itemId = ID;
		itemName = NAME;
		price = PRICE;
		quantity = qty;
	}
		
	public void prepare(){
		processBread();
		processMeat();
		packSandwich();
	}
	
	private void processBread(){...}
	
	private void processMeat(){...}
	
	private void packSandwich(){...}
	
}

public class Order{

	private Customer customer;
	private List<Item> items;
	
	public Order(Customer customer){
		this.customer = customer;
	}
	
	public Customer getCustomer(){
		return customer;
	}
	
	public addItem(Item item){
		items.add(item);
	}
	
	public cancelItem(Item item){
		int index = list.indexOf(item);
		list.remove(index);
	}
	
	public prepare(){	
		for(int i = 0; i < items.size(); i++){
			Item item = items.get(i);			
			item.prepare();			
		}		
	}
	
	public double getTotal(){
	
		double total = 0.0;	
		for(int i = 0; i < items.size(); i++){
			Item item = items.get(i);			
			total += item.getTotal();			
		}
		
		return total;
	}
}

public class Customer{
	
	private int customerId;
	private double balance;
	
	public Customer(int customerId, double initBalance){
		this.customerId = customerId;
		this.balance = initBalance;
	}
	
	public boolean charge(double amount){
		if(balance >= amount){
			balance -= amount;
			return true;
		}
		else{
			return false;
		}
	}
	
	public void refund(double amount){
		balance += amount;
	}
}

public class Robot{

	private int robotId;
	private List<Order> orders;
	
	public Robot(int robotId = 89757){
		this.robotId = robotId;
		orders = new List<Order>();
	}
	
	public boolean takeOrder(Order order){
	
		Customer customer = order.getCustomer();		
		boolean isSuccess = customer.charge(order.getTotal());
		
		if(isSuccess){
			orders.add(order);
		}
		
		return isSuccess;
	}
	
	public cancelOrder(Order order){
		int index = list.indexOf(order);
		list.remove(index);
		
		Customer customer = order.getCustomer();
		customer.refund(order.getTotal());
	}
	
	public prepare(){
		for(int i = 0; i < orders.size(); i++){
			Order order = order.get(i);
			order.prepare();
		}
	}	
}

// sample use of the robot

public class test{

	public static void main(String[] args){
	
		Robot robot = new Robot(89757);		// make a robot
	
		Customer customerA = new Customer(1, 25.0);		// comes in a customer
	
		Order order = new Order(customerA);		// make a order		
		Sandwich sandwich = new Sandwich(2);		
		order.addItem(sandwich);
		
		robot.takeOrder(order);		// take the order
		
		Customer customerB = new Customer(2, 12.0);		// comes in a customer
	
		order = new Order(customerB);		// make a order		
		Sandwich sandwich = new Sandwich(5);	
		order.addItem(sandwich);
		
		robot.takeOrder(order);		// take the order
		
		robot.prepare();		// prepare the food
		
	}
}

- PoWerOnOff December 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think command pattern will work here :

1. where client is one who gives the order (either a person or request for web service)
2. all robots are the recievers

- Source January 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would consider using template method pattern: www . newthinktank . com/2012/10/template-method-design-pattern-tutorial/

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

PErefect

- sagar.cdafle August 18, 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