Bird
0
0
LLDsystem_design~10 mins

Chain of Responsibility 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 next handler reference in the Chain of Responsibility pattern.

LLD
class Handler {
    Handler [1];
}
Drag options to blanks, or click blank then click option'
AnextHandler
BhandleRequest
Cprocess
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names instead of variable names.
Naming the variable unrelated to the chain.
2fill in blank
medium

Complete the code to forward the request to the next handler if current handler cannot process it.

LLD
void handleRequest(Request req) {
    if (canHandle(req)) {
        process(req);
    } else if ([1] != null) {
        [1].handleRequest(req);
    }
}
Drag options to blanks, or click blank then click option'
AnextHandler
Brequest
Chandler
Dprocess
Attempts:
3 left
💡 Hint
Common Mistakes
Using the request variable instead of the next handler.
Calling process instead of forwarding the request.
3fill in blank
hard

Fix the error in the code to correctly set the next handler in the chain.

LLD
void setNextHandler(Handler [1]) {
    this.nextHandler = next;
}
Drag options to blanks, or click blank then click option'
Ahandler
Bnext
CnextHandler
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the one assigned.
Confusing the instance variable with the parameter.
4fill in blank
hard

Fill both blanks to complete the method that processes the request or forwards it.

LLD
void handleRequest(Request req) {
    if ([1](req)) {
        [2](req);
    } else if (nextHandler != null) {
        nextHandler.handleRequest(req);
    }
}
Drag options to blanks, or click blank then click option'
AcanHandle
Bprocess
ChandleRequest
Dforward
Attempts:
3 left
💡 Hint
Common Mistakes
Calling handleRequest recursively instead of process.
Using incorrect method names.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps handler names to their status if they can handle a request.

LLD
status_map = {handler.[1]: handler.[2] for handler in handlers if handler.[3](request)}
Drag options to blanks, or click blank then click option'
Aname
Bstatus
CcanHandle
Dprocess
Attempts:
3 left
💡 Hint
Common Mistakes
Using process instead of canHandle in the condition.
Mixing up keys and values in the dictionary.