0
0
LLDsystem_design~10 mins

Product, Cart, Order classes in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a Product class with a constructor.

LLD
class Product:
    def __init__(self, id, name, price):
        self.id = id
        self.name = name
        self.price = [1]
Drag options to blanks, or click blank then click option'
Acost
Bprice
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name like 'cost' or 'name' instead of 'price'.
2fill in blank
medium

Complete the code to add a product to the Cart's items list.

LLD
class Cart:
    def __init__(self):
        self.items = []
    def add_product(self, product):
        self.items.[1](product)
Drag options to blanks, or click blank then click option'
Aappend
Bremove
Cpop
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' or 'pop' which remove items instead of adding.
3fill in blank
hard

Fix the error in the Order class constructor to correctly assign the cart parameter.

LLD
class Order:
    def __init__(self, order_id, cart):
        self.order_id = order_id
        self.cart = [1]
Drag options to blanks, or click blank then click option'
Acart
Border_id
Cself.cart
Dorder
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'order_id' or 'self.cart' instead of the parameter 'cart'.
4fill in blank
hard

Fill both blanks to create a method that calculates the total price of items in the cart.

LLD
class Cart:
    def __init__(self):
        self.items = []
    def total_price(self):
        return sum(item.[1] for item in self.[2])
Drag options to blanks, or click blank then click option'
Aprice
Bname
Citems
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' or 'id' instead of 'price' for summing.
5fill in blank
hard

Fill all three blanks to create an Order method that returns a dictionary of product names and their quantities.

LLD
class Order:
    def __init__(self, order_id, cart):
        self.order_id = order_id
        self.cart = cart
    def product_quantities(self):
        quantities = {}
        for item in self.cart.[1]:
            name = item.[2]
            quantities[name] = quantities.get(name, 0) + [3]
        return quantities
Drag options to blanks, or click blank then click option'
Aitems
Bname
C1
Dprice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'price' instead of 'name' as key or incrementing by price instead of 1.