Bird
0
0
LLDsystem_design~10 mins

State 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 interface for different states.

LLD
public interface State {
    void [1]();
}
Drag options to blanks, or click blank then click option'
Arun
Bstart
Chandle
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not clearly represent state behavior.
Choosing method names that are too generic or unrelated.
2fill in blank
medium

Complete the code to set the current state in the context class.

LLD
public class Context {
    private State state;

    public void setState([1] state) {
        this.state = state;
    }
}
Drag options to blanks, or click blank then click option'
AState
BObject
CContext
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic Object type instead of the State interface.
Using the Context class type instead of State.
3fill in blank
hard

Fix the error in the state transition method to call the correct method on the current state.

LLD
public class Context {
    private State state;

    public void request() {
        state.[1]();
    }
}
Drag options to blanks, or click blank then click option'
Aexecute
Bhandle
Crun
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that does not exist in the State interface.
Using inconsistent method names between interface and implementation.
4fill in blank
hard

Fill both blanks to implement a concrete state class with the correct method and context reference.

LLD
public class [1]State implements State {
    private Context [2];

    public [1]State(Context context) {
        this.[2] = context;
    }

    @Override
    public void handle() {
        // state-specific behavior
    }
}
Drag options to blanks, or click blank then click option'
AConcrete
BState
Ccontext
Dctx
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic or unclear variable names for the context.
Mismatch between class name and constructor parameter.
5fill in blank
hard

Fill all three blanks to complete the context's constructor and initial state setup.

LLD
public class Context {
    private State [1];

    public Context() {
        this.[1] = new [2]State(this);
    }

    public void setState(State state) {
        this.[1] = state;
    }

    public void request() {
        [3].handle();
    }
}
Drag options to blanks, or click blank then click option'
Astate
BConcrete
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names for the state.
Incorrect class name for the initial state.