Hi all , I am attaching the link of flipkart sde -2 machine coding round .
Exactly 1:30 hrs was provided to write the code and upload onto google form shared by them .
Link to the question - https://docs.google.com/document/d/1MXmAdDwO9SwEkLAQ-cvm7T2C1FyJzS3mi8zGUDrqJSg/edit
My solution -
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
struct Cat{
string catName;
int price;
int quantity;
Cat(string name,int p,int q){
catName=name;
price=p;
quantity=q;
}
};
struct Brand{
string brand;
unordered_map<string,Cat *> invCat;
Brand(string brand){
this->brand=brand;
invCat.clear();
}
};
unordered_map<string,Brand *> invBrand;
struct Order{
string brand;
string category;
int q;
Order(string b,string c,int q){
brand=b;
category=c;
this->q=q;
}
};
struct User{
string name;
string add;
int cartCost;
int wallet;
unordered_map<int,Order *> cart;
vector<Order *> orders;
User(string n,string add,int w){
name=n;
this->add=add;
wallet=w;
cartCost=0;
cart.clear();
orders.clear();
}
};
unordered_map<string,User *> userMp;
void AddItem(string brand,string cat,int price){
Brand *newBrand;
if(invBrand.find(brand)==invBrand.end()){
newBrand=new Brand(brand);
invBrand[brand]=newBrand;
}
Cat *newCat=new Cat(cat,price,0);
invBrand[brand]->invCat[cat]=newCat;
}
void AddInventory(string brand,string cat,int q){
if(invBrand.find(brand)==invBrand.end()){
cout<<"Brand doesnot exist\n";
return ;
}
else if(invBrand[brand]->invCat.find(cat)==invBrand[brand]->invCat.end()){
cout<<"Category doesnot exist\n";
return ;
}
invBrand[brand]->invCat[cat]->quantity+=q;
}
void UpdateInventory(string brand,string cat,int price){
invBrand[brand]->invCat[cat]->price=price;
}
void ViewInventory(){
unordered_map<string,Brand *>::iterator itBrand=invBrand.begin();
while(itBrand!=invBrand.end()){
unordered_map<string,Cat *>::iterator itCat=(*itBrand).second->invCat.begin();
while(itCat!=(*itBrand).second->invCat.end()){
cout<<(*itBrand).first<<" -> "<<(*itCat).first<<" -> "<<(*itCat).second->quantity<<"\n";
itCat++;
}
itBrand++;
}
cout<<"Inv over ----\n";
}
void AddUser(string name,string add,int w){
User *newUser=new User(name,add,w);
userMp[name]=newUser;
}
void AddToCart(string name,string cat,string brand,int u){
if(invBrand.find(brand)==invBrand.end()){
cout<<"Brand doesnot exist\n";
return ;
}
else if(invBrand[brand]->invCat.find(cat)==invBrand[brand]->invCat.end()){
cout<<"Category doesnot exist\n";
return ;
}
Order *newOrder=new Order(brand,cat,u);
int currentCartSize=userMp[name]->cart.size();
userMp[name]->cart[currentCartSize]=newOrder;// 0 - OrderPtr , 1 -OrderPtr
}
void GetCart(string name){
cout<<"Name "<<name<<"\n";
User *p=userMp[name];
int cartSize=p->cart.size();
if(!cartSize){
cout<<"Empty Cart\n";
return;
}
int totalCost=0;
unordered_map<int,Order *>::iterator itOrder=p->cart.begin();
while(itOrder!=p->cart.end()){
string brand=(*itOrder).second->brand;
string cat=(*itOrder).second->category;
int q=(*itOrder).second->q;
int price=(invBrand[brand]->invCat[cat]->price*q);
cout<<brand<<" -> "<<cat<<" -> "<<q<<"\n";
totalCost+=price;
itOrder++;
}
cout<<"Total ->"<<totalCost<<"\n";
p->cartCost=totalCost;
}
void Checkout(string name){
cout<<"Checkout "<<name<<"\n";
User *p=userMp[name];
int cartSize=p->cart.size();
if(!cartSize){
cout<<"No order to checkout\n";
return ;
}
GetCart(name);
//cout<<"Total ->"<<totalCost<<"\n";
if(p->cartCost>p->wallet){
cout<<"Not enough money in wallet\n";
}
else{
unordered_map<int,Order *>::iterator itOrder=p->cart.begin();
while(itOrder!=p->cart.end()){
string brand=(*itOrder).second->brand;
string cat=(*itOrder).second->category;
int q=(*itOrder).second->q;
//cout<<brand<<" "<<cat<<" "<<name<<" "<<invBrand[brand]->invCat[cat]->quantity<<"\n";
if(invBrand[brand]->invCat[cat]->quantity<q){
cout<<"Unsuccessful: Not enough quantity in "<<brand<<" of category "<<cat<<"\n";
return;
}
itOrder++;
}
itOrder=p->cart.begin();
while(itOrder!=p->cart.end()){
string brand=(*itOrder).second->brand;
string cat=(*itOrder).second->category;
int q=(*itOrder).second->q;
cout<<brand<<" pp "<<cat<<" "<<name<<" "<<invBrand[brand]->invCat[cat]->quantity<<"\n";
int price=(invBrand[brand]->invCat[cat]->price*q);
invBrand[brand]->invCat[cat]->quantity-=q;
p->orders.push_back((*itOrder).second);
//p->cart.erase((*itOrder).first);
p->wallet-=price;
itOrder++;
//cout<<"pp\n";
}
p->cart.clear();
cout<<"Successful checkout , new wallet amount: "<<p->wallet<<"\n";
}
}
void SearchItems(vector<string> brand){
cout<<"Items ----\n";
for(auto b:brand){
cout<<b<<"\n";
if(invBrand.find(b)==invBrand.end()){
cout<<"No such brand\n";
}
for(auto i:invBrand[b]->invCat){
if(i.second->quantity){
cout<<b<<" -> "<<i.first<<" -> "<<i.second->quantity<<"\n";
}
}
}
}
void SearchItems(vector<string> brand,vector<string> cat){
cout<<"Items ----\n";
for(auto b:brand){
//cout<<b<<"\n";
if(invBrand.find(b)==invBrand.end()){
cout<<"No such brand\n";
}
for(auto i:cat){
if(invBrand[b]->invCat.find(i)!=invBrand[b]->invCat.end()){
cout<<b<<" -> "<<i<<" -> "<<invBrand[b]->invCat[i]->quantity<<"\n";
}
}
}
}
int main(){
//# AddItem(<item_brand>, <item_category>, <item_price>)
// AddItem("Amul", "Milk", 100);
// AddItem("Amul", "Curd", 50);
AddItem("Nestle", "Milk", 60);
AddItem("Nestle", "Curd", 90);
//# AddInventory(<item_brand>, <item_category>, <units>)
// AddInventory("Amul", "Milk", 10);
AddInventory("Nestle", "Milk", 15);
AddInventory("Nestle", "Curd", 15);
// AddInventory("Amul", "Milk", 10);
// AddInventory("Amul", "Curd", 5);
//# Final View Inventory :
ViewInventory();
// Amul - > Milk -> 20
// Amul -> Curd -> 5
// Nestle -> Milk -> 15
// Nestle -> Curd -> 10
//#AddUser(<Name>,<Address>,<Wallet Amount>)
// AddUser("Jhonny", "Flipkart Bellandur, Bangalore 560068", 600);
// AddUser("Naruto", "BTM Layout, Bengaluru, 560042", 500);
AddUser("Goku", "Vijay Nagar, Indore, MP", 3000);
//# AddToCart(<User_Name>, <item_category>, <item_brand>, <units>)
//AddToCart("Jhonny", "Milk", "Nestle", 5);
//UpdateInventory("Nestle", "Milk", 5);
AddToCart("Goku", "Milk", "Nestle", 10);
AddToCart("Goku", "Curd", "Nestle", 10);
//AddToCart("Naruto", "Curd", "Nestle", 2);
AddToCart("Goku", "Milk", "Amul", 2);
//# GetCart(<User_Name>)
GetCart("Naruto");
ViewInventory();
//Nestle -> Milk - > 10 → Total Price 50
// GetCart("Jhonny");
// //Nestle -> Milk -> 5 -> Total Price : 25
// GetCart("Naruto");
// //Empty Cart
// //# Checkout(<Name>)
// Checkout("Goku");
// //-> Successful checkout , new wallet amount: 2950
// Checkout("Jhonny");
// //-> Successful checkout , new wallet amount: 575
// Checkout("Naruto");
// //-> No order to checkout
// //# expected response <brand, category, price, remaining_inventory_count>
// SearchItems({"Nestle"});
// // Nestle -> Curd-> 90 -> 10
// SearchItems({"Nestle", "Amul"}, {"Curd"});
// // //Amul -> Curd -> 50 -> 5
// // //Nestle -> Curd -> 90 -> 10
// SearchItems({"Nestle"}, {"Curd", "Milk", "Paneer"});
// // Nestle -> Curd -> 90 -> 10
// // Nestle -> Milk -> 60 -> 0
return 0;
}Please let me know where I can improve this .
Feel free to post your own code in the comments .
Code modularity and class/struct relationship was expected from the candidates .
Comments (2)


