Bird
0
0
LLDsystem_design~10 mins

Interpreter 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 interface method for interpreting expressions.

LLD
interface Expression {
    void [1]();
}
Drag options to blanks, or click blank then click option'
Aexecute
Binterpret
Cevaluate
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic method names like execute or run which are not standard in this pattern.
2fill in blank
medium

Complete the code to implement a terminal expression class that stores a value.

LLD
class NumberExpression implements Expression {
    private int number;
    public NumberExpression(int number) {
        this.number = number;
    }
    public void [1]() {
        System.out.println(number);
    }
}
Drag options to blanks, or click blank then click option'
Aexecute
Brun
Cevaluate
Dinterpret
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface, causing a compile error.
3fill in blank
hard

Fix the error in the non-terminal expression class by completing the method to interpret both sub-expressions.

LLD
class AddExpression implements Expression {
    private Expression left, right;
    public AddExpression(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }
    public void [1]() {
        left.interpret();
        right.interpret();
        System.out.println("Add operation done");
    }
}
Drag options to blanks, or click blank then click option'
Aevaluate
Bexecute
Cinterpret
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not override the interface method, causing runtime errors.
4fill in blank
hard

Fill both blanks to complete the context class that uses an expression to interpret input.

LLD
class Context {
    private Expression expression;
    public Context(Expression expression) {
        this.[1] = expression;
    }
    public void [2]() {
        expression.interpret();
    }
}
Drag options to blanks, or click blank then click option'
Aexpression
Bexpr
Cinterpret
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different field name that does not match the class field.
Using a method name other than interpret.
5fill in blank
hard

Fill all three blanks to complete the client code that builds and interprets an expression tree.

LLD
Expression left = new NumberExpression([1]);
Expression right = new NumberExpression([2]);
Expression add = new AddExpression(left, right);
Context context = new Context([3]);
context.interpret();
Drag options to blanks, or click blank then click option'
A5
B3
Cadd
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong expression to the context.
Using non-integer values for NumberExpression.