Bird
0
0
LLDsystem_design~10 mins

Memento 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 Memento class that stores the state.

LLD
class Memento:
    def __init__(self, [1]):
        self._state = state
Drag options to blanks, or click blank then click option'
Asave
Bstate
Cdata
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'save' or 'data' instead of 'state'.
2fill in blank
medium

Complete the code to restore the state from the Memento object.

LLD
class Originator:
    def restore(self, memento):
        self._state = memento.[1]
Drag options to blanks, or click blank then click option'
A_state
Bstate
Cget_state()
Dsave_state()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that does not exist like get_state() or save_state().
3fill in blank
hard

Fix the error in saving the current state into a Memento object.

LLD
class Originator:
    def save(self):
        return Memento([1])
Drag options to blanks, or click blank then click option'
Astate
Bself.save_state
Cself._state
Dself.state
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like 'state' or methods like 'save_state'.
4fill in blank
hard

Fill both blanks to implement the Caretaker storing and retrieving mementos.

LLD
class Caretaker:
    def __init__(self):
        self._mementos = []

    def add_memento(self, memento):
        self._mementos.[1](memento)

    def get_memento(self, index):
        return self._mementos[[2]]
Drag options to blanks, or click blank then click option'
Aappend
Bpop
Cindex
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop to add items or remove to get items by index.
5fill in blank
hard

Fill all three blanks to complete the Originator's state change and memento management.

LLD
class Originator:
    def __init__(self):
        self._state = None

    def set_state(self, state):
        self._state = [1]

    def save(self):
        return Memento(self._state)

    def restore(self, memento):
        self._state = memento.[2]

    def get_state(self):
        return self._state

originator = Originator()
originator.set_state('State1')
memento = originator.save()
originator.set_state('State2')
originator.restore(memento)
print(originator.[3]())
Drag options to blanks, or click blank then click option'
Astate
B_state
Cget_state
Dsave_state
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute or method names that do not exist.