0
0
LLDsystem_design~10 mins

When to use which creational pattern 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 choose the creational pattern that provides a single instance.

LLD
class DatabaseConnection:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return [1]
Drag options to blanks, or click blank then click option'
Acls._instance
Bself
CDatabaseConnection
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning self instead of the stored instance
Returning None causing errors
2fill in blank
medium

Complete the code to use the Factory Method pattern to create objects without specifying the exact class.

LLD
class AnimalFactory:
    def create_animal(self, animal_type):
        if animal_type == 'dog':
            return Dog()
        elif animal_type == 'cat':
            return [1]()
Drag options to blanks, or click blank then click option'
AAnimal
BDog
CCat
DPet
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Dog instead of Cat
Returning a base class instead of specific class
3fill in blank
hard

Fix the error in the Builder pattern code to correctly build a product step by step.

LLD
class CarBuilder:
    def __init__(self):
        self.car = Car()

    def add_wheels(self):
        self.car.wheels = 4

    def add_engine(self):
        self.car.engine = 'V8'

    def get_car(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aself
Bcar
CCar()
Dself.car
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a new Car instead of the built one
Returning undefined variable
4fill in blank
hard

Fill both blanks to complete the Abstract Factory pattern that creates families of related objects.

LLD
class GUIFactory:
    def create_button(self):
        return [1]()

    def create_checkbox(self):
        return [2]()
Drag options to blanks, or click blank then click option'
AWindowsButton
BLinuxButton
CWindowsCheckbox
DLinuxCheckbox
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing Windows and Linux classes
Using unrelated classes
5fill in blank
hard

Fill all three blanks to complete the Prototype pattern code that clones objects.

LLD
class Document:
    def __init__(self, content):
        self.content = content

    def clone(self):
        return Document([1])

original = Document('Report')
copy = original.clone()
copy.content = [2]
print(original.content, copy.content)  # [3]
Drag options to blanks, or click blank then click option'
Aself.content
B'Summary'
CReport Summary
Dcontent
Attempts:
3 left
💡 Hint
Common Mistakes
Passing undefined variable to clone
Changing original content instead of copy