Yahoo Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
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.
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;
}
}
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;
}
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
We can start from this, and we can improve it by adding more fields like Brand, Catalog etc ...
- Ajeet August 26, 2014