0
0
LLDsystem_design~10 mins

Flyweight 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 define the Flyweight interface method.

LLD
class Flyweight:
    def [1](self, extrinsic_state):
        pass
Drag options to blanks, or click blank then click option'
Aupdate
Boperation
Cdelete
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using constructor or unrelated method names instead of 'operation'.
2fill in blank
medium

Complete the code to implement intrinsic state storage in the ConcreteFlyweight class.

LLD
class ConcreteFlyweight(Flyweight):
    def __init__(self, [1]):
        self.intrinsic_state = intrinsic_state
Drag options to blanks, or click blank then click option'
Astate
Bcontext
Cextrinsic_state
Dintrinsic_state
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing intrinsic and extrinsic state in constructor parameters.
3fill in blank
hard

Fix the error in the FlyweightFactory method to return existing flyweights or create new ones.

LLD
class FlyweightFactory:
    def __init__(self):
        self._flyweights = {}

    def get_flyweight(self, key):
        if key not in self._flyweights:
            self._flyweights[key] = ConcreteFlyweight([1])
        return self._flyweights[key]
Drag options to blanks, or click blank then click option'
Akey
Bself
CFlyweight
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing incorrect parameters like self or None to the constructor.
4fill in blank
hard

Fill both blanks to complete the operation method using intrinsic and extrinsic state.

LLD
class ConcreteFlyweight(Flyweight):
    def __init__(self, intrinsic_state):
        self.intrinsic_state = intrinsic_state

    def operation(self, [1]):
        print(f"Intrinsic: {self.intrinsic_state}, Extrinsic: { [2] }")
Drag options to blanks, or click blank then click option'
Aextrinsic_state
Bintrinsic_state
Cstate
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Using intrinsic_state as parameter or in print instead of extrinsic_state.
5fill in blank
hard

Fill all three blanks to create a flyweight factory, get flyweights, and perform operations with extrinsic state.

LLD
factory = FlyweightFactory()
flyweight1 = factory.get_flyweight([1])
flyweight2 = factory.get_flyweight([2])
flyweight1.operation([3])
Drag options to blanks, or click blank then click option'
A"A"
B"B"
C"extrinsic1"
D"extrinsic2"
Attempts:
3 left
💡 Hint
Common Mistakes
Using extrinsic state as keys or vice versa.