Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

We can start from this, and we can improve it by adding more fields like Brand, Catalog etc ...

ShoppingCart
	List<ItemOrder> items;
	add(ItemOrder)
	remove(ItemOrder)
	applyCoupan(Coupan)
	getTotal()	

ItemOrder
	Item item;
	int quantity
	double price;
	getPrice()
	getQuantity()
	changeQuantity();

Enum ItemTypes\ or Category

Item
	String name;
	ItemTypes type;
	double price;
	String itemID;
	getName()
	getItemID();
	getItemPrice();

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

We can use doubly linked list for shopping cart item list. At the time of user log out we can persist the data in database against the user. Basic dictionary operations(add , remove , search ) should be implemented . It would be nice to provide sorting also on the basis of various factors such as name of the item , price of the item. Most of the sites now have coupons system.Each item can have an action of moving it to wishlist too. So apply coupon options will be provided. The shopping cart window will have button 'Checkout' which will redirect to the transaction page. Each user will have it's own shopping cart object and it's data will be initialized from the database.

- praveenkcs28 August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class shoppingCart {

	private Map<Integer, Article> articles;
	private Map<Integer, Integer> counts;
	private Map<Integer, Integer> categories;

	public ShoppingCart() {
		articles = new HashMap<>();	
		counts = new HashMap<>();
		categories = new HashMap<>();
	}
	
	public ShoppingCart(Article a) {
		this();
		addToCart(a);	   				
	}

	public final addToCart(Article a) {
		if(articles.containsKey(a) {
			increaseCount(a.getId());
		} else {
			articles.put(a.getid(), a);	
			counts.put(a.getId(), 1);
		}
		enrichCategories(a);		
	}

	public increaseCount(int idArticle) {
		if(counts.containsKey(idArticle))
			counts.set(idArticle, counts.get(idArticle) + 1);
	}

	public removeFromCart(Article a) {
		if(articles.containsKey(a)) {
			articles.remove(a.getId());
			counts.remove(a.getId());
			poorishCategories(a);
		} else {
			throw new UnsupportedOperationException();
		}		
	}
	
	public decreaseCount(int idArticle) {
		if(counts.containsKey(idArticle)) {
			if(counts.get(idArticle) == 1) {
				articles.remove(idArticle);
				counts.remove(idArticle);	
			} else {
				counts.set(idArticle, counts.get(idArticle) - 1;	
			}
			poorishCategories(a);
		}	
	}

	private void enrichCategories(Article a) {
		Category cId = a.getCategory().getId();
		if(!categories.contains(cId) {
			categories.put(cId, 1);
		} else {
			categories.put(cId, categories.get(cId) + 1);
		}
	}
	
	private void poorishCategories(Article a) {
		Category cId = a.getCategory().getId();
		if(categories.put.containsKey(cId)) {
			if(categories.get(cId) == 1) {
				categories.remove(cId);
			} else {
				categories.set(cId, categories.get(cId) - 1;	
			}
		}	
	}

	public int getNbArticles() {
		int nb=0;
		for(Entry<Integer, Integer> entry : counts) {
			nb+=entry.getValue();
		}
		return nb;
	}	

	public int getNbCategories() {		
		return categories.size();
	}

	public Map<Integer, Article> getArticles () {
		return articles;
	}

	public double getTotal() {
		double total=0.0;
		for(Entry<Integer, Article> entry : articles) {		
			int id = entry.getKey();
			Article current = entry.getValue();
			double unitPrice = current.getPrice();
			total+= unitPrice * counts.get(id);
		}
		return total;
	}
}

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

Implementation in C++:

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

struct Item {
	string name;
	int quantity;
	int price;
};

class ShoppingCart {
private:
	vector<Item> item;
	int total_price;
public:
	ShoppingCart();
	void addItem(string name, int quantity, int price);
	void removeItem(string name, int quantity);
	int getTotal();
};

ShoppingCart::ShoppingCart() {
	cout << "Creating a new shopping cart" << endl;
	total_price = 0;
}

void ShoppingCart::addItem(string name, int quantity, int price) {
	for (int i = 0; i < item.size(); i++) {
		if (item[i].name == name) {
			item[i].quantity += quantity;
			return;
		}
	}

	Item temp;
	temp.name = name;
	temp.quantity = quantity;
	temp.price = price;
	item.push_back(temp);
}

void ShoppingCart::removeItem(string name, int quantity) {
	for (int i = 0; i < item.size(); i++) {
		if (item[i].name == name) {
			if (item[i].quantity >= quantity) {
				item[i].quantity -= quantity;
				return;
			}
			cout << "Not enough items present in the cart to be removed" << endl;
			return;
		}
	}
	cout << "This item is not present in the cart" << endl;
}

int ShoppingCart::getTotal() {
	total_price = 0;
	for (int i = 0; i < item.size(); i++) {
		total_price += item[i].quantity * item[i].price;
	}
	return total_price;
}

int main() {
	ShoppingCart cart;
	cart.addItem("Maggi", 10, 5);
	cart.addItem("Biryani", 2, 15);
	cart.addItem("Ketchup", 1, 5);
	cout << "Total cost: " << cart.getTotal() << endl;
	cart.addItem("Football", 2, 15);
	cart.removeItem("Maggi", 4);
	cart.removeItem("Ketchup", 2);
	cout << cart.getTotal() << endl;
	cart.addItem("Candy", 15, 2);
	cart.removeItem("Bat", 1);
	cout << "Total cost: " << cart.getTotal() << endl;
}

- akshaycj47 November 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

shopping cart design

Features

Add item in cart
save cart item in data base
Update cart amount on item added in art
Calculate total price
Apply coupon
Get quantity
restrict user can not add more than 10 same items in cart
Remove cart item remove on request
send notification to user that item going to be out of stock
remove item if item is removed from inventory and message and mail to user
Server side Handle thousands of request per second - use load balancer
Data base selection data base sql or no sql - nosql store data in form of tree - design
sql db design

Table Structure

Offer
PromotionCoupan amount
promotionId
GiftCoupan
gift ID amount

Cart
item id user id price discount color size

Item
item id category id itemvariationid item description - very good product

Item Variations
item variation id price

Color
color id color


Item Color
item variation id color id

User
user id

Order Item
item id
user id

Order
user id

More on design
Java classes design - will update later
Java design pattern - will update later

Above is very rough high level design, this can be drilled down and feature and sub feature of system can be identified and data base table can be refined.

Thanks

- Dinkar January 16, 2018 | 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