Bird
0
0
LLDsystem_design~10 mins

Command pattern for undo 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 the Command interface method for undo functionality.

LLD
interface Command {
    void execute();
    void [1]();
}
Drag options to blanks, or click blank then click option'
Aundo
Bredo
Crun
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'redo' instead of 'undo' as the method name.
Using unrelated method names like 'run' or 'start'.
2fill in blank
medium

Complete the code to add a command to the undo stack after execution.

LLD
public class Invoker {
    private Stack<Command> undoStack = new Stack<>();

    public void executeCommand(Command cmd) {
        cmd.execute();
        undoStack.[1](cmd);
    }
}
Drag options to blanks, or click blank then click option'
Apush
Bremove
Cpeek
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes an element instead of adding.
Using 'peek' which only views the top element.
3fill in blank
hard

Fix the error in the undo method to correctly undo the last command.

LLD
public void undo() {
    if (!undoStack.isEmpty()) {
        Command cmd = undoStack.[1]();
        cmd.undo();
    }
}
Drag options to blanks, or click blank then click option'
Apeek
Bpop
Cpush
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'peek' which does not remove the command from the stack.
Using 'push' which adds instead of removes.
4fill in blank
hard

Fill both blanks to implement redo functionality using a redo stack.

LLD
public class Invoker {
    private Stack<Command> undoStack = new Stack<>();
    private Stack<Command> redoStack = new Stack<>();

    public void undo() {
        if (!undoStack.isEmpty()) {
            Command cmd = undoStack.[1]();
            cmd.undo();
            redoStack.[2](cmd);
        }
    }
}
Drag options to blanks, or click blank then click option'
Apop
Bpush
Cpeek
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'peek' instead of 'pop' to remove from undoStack.
Using 'pop' instead of 'push' to add to redoStack.
5fill in blank
hard

Fill all three blanks to implement redo method that re-executes and updates stacks correctly.

LLD
public void redo() {
    if (!redoStack.isEmpty()) {
        Command cmd = redoStack.[1]();
        cmd.[2]();
        undoStack.[3](cmd);
    }
}
Drag options to blanks, or click blank then click option'
Apop
Bexecute
Cpush
Dundo
Attempts:
3 left
💡 Hint
Common Mistakes
Calling undo instead of execute during redo.
Using peek instead of pop to remove from redoStack.