Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an object using a creational pattern.
LLD
object = [1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing Observer which is for event handling, not creation.
✗ Incorrect
The Factory pattern is used to create objects without specifying the exact class.
2fill in blank
mediumComplete the code to ensure only one instance of a class is created.
LLD
class Singleton: _instance = None def __new__(cls): if [1] is None: cls._instance = super().__new__(cls) return cls._instance
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cls.instance which is not defined.
✗ Incorrect
The Singleton pattern checks if the instance exists using cls._instance.
3fill in blank
hardFix the error in the Builder pattern method to return the final product.
LLD
class Builder: def __init__(self): self.product = [] def add_part(self, part): self.product.append(part) def get_result(self): return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'product' without self causes a NameError.
✗ Incorrect
The method should return the product stored in self.product.
4fill in blank
hardFill both blanks to create a Factory method that returns a new object.
LLD
def factory_method(type): if type == 'A': return [1]() else: return [2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same product class for both types.
✗ Incorrect
The factory method returns ProductA or ProductB objects based on the type.
5fill in blank
hardFill all three blanks to complete the Singleton pattern implementation.
LLD
class Singleton: _instance = None def __new__(cls): if [1] is None: [2] = super().__new__(cls) return [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
✗ Incorrect
All blanks refer to the class variable cls._instance to ensure only one instance is created and returned.