Bird
0
0
LLDsystem_design~10 mins

Strategy 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 declare the Strategy interface.

LLD
interface [1] {
    execute(): void;
}
Drag options to blanks, or click blank then click option'
AStrategy
BContext
CAlgorithm
DExecutor
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong interface name like 'Context' or 'Algorithm'.
Forgetting to define the execute method.
2fill in blank
medium

Complete the code to define a concrete strategy class implementing the Strategy interface.

LLD
class ConcreteStrategyA implements [1] {
    execute(): void {
        console.log('Strategy A executed');
    }
}
Drag options to blanks, or click blank then click option'
AStrategy
BContext
CExecutor
DAlgorithm
Attempts:
3 left
💡 Hint
Common Mistakes
Implementing the wrong interface like 'Context'.
Missing the execute method implementation.
3fill in blank
hard

Fix the error in the Context class constructor to accept a strategy.

LLD
class Context {
    strategy: [1];

    constructor(strategy: [1]) {
        this.strategy = strategy;
    }

    executeStrategy(): void {
        this.strategy.execute();
    }
}
Drag options to blanks, or click blank then click option'
AAlgorithm
BStrategy
CExecutor
DContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Context' as the type instead of 'Strategy'.
Not defining the strategy property type.
4fill in blank
hard

Fill both blanks to change the strategy at runtime and execute it.

LLD
const context = new Context(new ConcreteStrategyA());
context.executeStrategy();
context.[1] = new ConcreteStrategyB();
context.[2]();
Drag options to blanks, or click blank then click option'
Astrategy
BexecuteStrategy
CsetStrategy
DrunStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name instead of the property to change the strategy.
Calling a non-existent method like 'runStrategy()'.
5fill in blank
hard

Fill all three blanks to implement a new concrete strategy and use it in the context.

LLD
class [1] implements Strategy {
    execute(): void {
        console.log('Strategy C executed');
    }
}

const context = new Context(new [2]());
context.[3]();
Drag options to blanks, or click blank then click option'
AConcreteStrategyC
CexecuteStrategy
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the class and constructor.
Calling a method that does not exist like 'run()'.