Phonepe Machine Coding ( Bad experience )

Anonymous User
2219
Jun 06, 2023

Hi LeetCoders,

I appeared for Phonepe Machine Coding Round in May 2023. First, it was a 2 hour Machine Coding Round followed by 30 minutes discussion.

Question :-

Design an Online MarketPlace (similar to Flipkart & Amazon) with functonalities supporting User Login, Adding Product to Market Place. User can add Items to cart, then checkout cart to place Order & also support showing Order History.

I was able to complete the working code. Below is the code, I wrote:-

#include <bits/stdc++.h>
using namespace std;

class User {
    string userId;
    string name;
    string email;
    string password;
    string address;

public:
    User(const string& userId, const string& name, const string& email, const string& password, const string& address) {
        this->userId = userId;
        this->name = name;
        this->email = email;
        this->password = password;
        this->address = address;
    }

    const string& getAddress() const {
        return address;
    }

    void setAddress(const string& address) {
        this->address = address;
    }

    const string& getEmail() const {
        return email;
    }

    void setEmail(const string& email) {
        this->email = email;
    }

    const string& getName() const {
        return name;
    }

    void setName(const string& name) {
        this->name = name;
    }

    const string& getPassword() const {
        return password;
    }

    void setPassword(const string& password) {
        this->password = password;
    }

    const string& getUserId() const {
        return userId;
    }

    void setUserId(const string& userId) {
        this->userId = userId;
    }
};

class Product {
    string productId;
    double price;
    string name;

public:
    Product(const string& productId, double price, const string& name) {
        this->productId = productId;
        this->price = price;
        this->name = name;
    }

    double getPrice() const {
        return price;
    }

    void setPrice(double price) {
        this->price = price;
    }

    const string& getProductId() const {
        return productId;
    }

    void setProductId(const string& productId) {
        this->productId = productId;
    }

    const string& getName() const {
        return name;
    }

    void setName(const string& name) {
        this->name = name;
    }
};

class Cart {
    string productId;
    string name;
    int quantity;
    double price;

public:
    Cart(const string& productId, const string& name, int quantity, double price) {
        this->productId = productId;
        this->name = name;
        this->quantity = quantity;
        this->price = price;
    }

    const string& getName() const {
        return name;
    }

    void setName(const string& name) {
        this->name = name;
    }

    double getPrice() const {
        return price;
    }

    void setPrice(double price) {
        this->price = price;
    }

    const string& getProductId() const {
        return productId;
    }

    void setProductId(const string& productId) {
        this->productId = productId;
    }

    int getQuantity() const {
        return quantity;
    }

    void setQuantity(int quantity) {
        this->quantity = quantity;
    }
};

class Order {
    string orderId;
    string userId;
    vector<Cart*> items;

public:
    Order(const string& orderId, const string& userId, const vector<Cart*>& items) {
        this->orderId = orderId;
        this->userId = userId;
        this->items = items;
    }

    const vector<Cart*>& getItems() const {
        return items;
    }

    void setItems(const vector<Cart*>& items) {
        this->items = items;
    }

    const string& getOrderId() const {
        return orderId;
    }

    void setOrderId(const string& orderId) {
        this->orderId = orderId;
    }

    const string& getUserId() const {
        return userId;
    }

    void setUserId(const string& userId) {
        this->userId = userId;
    }
};

class Marketplace {
    unordered_map<string, User*> users;
    unordered_map<string, Product*> products;
    unordered_map<string, vector<Cart*>> carts;
    unordered_map<string, vector<Order*>> orderHistory;

public:
    void createUser(User* user) {
        users[user->getUserId()] = user;
    }

    void createProduct(Product* product) {
            products[product->getProductId()] = product;
     }

    User* getUser(const string& userId) {
        if (users.find(userId) != users.end()) {
            return users[userId];
        }

        throw runtime_error("User not found");
    }

    Product* getProduct(const string& productId) {
        if (products.find(productId) != products.end()) {
            return products[productId];
        }

        throw runtime_error("Product not found");
    }

    void addToCart(const string& userId, const string& productId, int quantity) {
        if (users.find(userId) != users.end() && products.find(productId) != products.end()) {
            Cart* cart = new Cart(productId, products[productId]->getName(), quantity, products[productId]->getPrice());

            if (carts.find(userId) != carts.end()) {

                for (Cart* item : carts[userId]) {
                    if (item->getProductId() == productId) {
                        int quantityNew = item->getQuantity() + quantity;
                        item->setQuantity(quantityNew);
                        return;
                    }
                }

                carts[userId].push_back(cart);
            } else {
                vector<Cart*> userCart;
                userCart.push_back(cart);
                carts[userId] = userCart;
            }
        } else {
            throw runtime_error("User or Product not found");
        }
    }

    vector<Cart*> getCart(const string& userId) {
        if (carts.find(userId) != carts.end()) {
            return carts[userId];
        }

        throw runtime_error("User not found");
    }

    void checkout(const string& userId) {
        if (carts.find(userId) != carts.end() && carts[userId].size() > 0) {
            string orderId = "ORDER_" + to_string(orderHistory[userId].size() + 1);
            vector<Cart*> items = carts[userId];
            Order* order = new Order(orderId, userId, items);

            for (Cart* item : items) {
                string productId = item->getProductId();
            }

            orderHistory[userId].push_back(order);
            carts[userId].clear();
        } else {
            throw runtime_error("User or Cart not found or Cart is empty");
        }
    }

    vector<Order*> getOrderHistory(const string& userId) {
        if (orderHistory.find(userId) != orderHistory.end()) {
            return orderHistory[userId];
        }

        throw runtime_error("User not found or no order history");
    }
};

int main() {
    Marketplace marketplace;

    // User registration and login
    User* user = new User("USER_1", "ZywOo", "zywoo@gmail.com", "password", "France");
    marketplace.createUser(user);
    User* retrievedUser = marketplace.getUser("USER_1");

    cout<<"Username = "<<retrievedUser->getName()<<endl;

    // Product fetch
    Product* product = new Product("PRODUCT_1", 10.0, "Example Product");
    marketplace.createProduct(product);
    marketplace.getProduct("PRODUCT_1");



    // Shopping cart
    marketplace.addToCart("USER_1", "PRODUCT_1", 2);
    vector<Cart*> cart = marketplace.getCart("USER_1");

    cout<<"Cart[0]'s productId = "<<cart[0]->getProductId()<<endl;
    cout<<"Cart[0]'s Quantity = "<<cart[0]->getQuantity()<<endl;

    // Checkout
    marketplace.checkout("USER_1");
    vector<Order*> orderHistory = marketplace.getOrderHistory("USER_1");

    cout<<"OrderId = "<<orderHistory[0]->getOrderId()<<endl;

    return 0;
}

Discussion round also went well. I asked for feedback but interviewer said HR will give the feedback. 3-4 days passed no response. I mailed HR, no response. Then, got system generated mail regarding Rejection. Called HR for feedback, she rejected the call and didnt pick again.

I didnt want to proceed further but seriously wanted feedback as to what I did wrong so I dont make same mistake again. Maybe they were looking for someone who can code in Java, idk.

Comments (6)