0
0
LLDsystem_design~10 mins

Why chess tests polymorphism and strategy in LLD - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a base class for chess pieces.

LLD
class ChessPiece:
    def move(self, position):
        raise [1]Error("This method should be overridden")
Drag options to blanks, or click blank then click option'
ANotImplemented
BRuntime
CValue
DType
Attempts:
3 left
💡 Hint
Common Mistakes
Using RuntimeError instead of NotImplementedError
Forgetting to raise an error
2fill in blank
medium

Complete the code to override the move method for the Knight piece.

LLD
class Knight(ChessPiece):
    def move(self, position):
        return "L-shaped move to " + [1]
Drag options to blanks, or click blank then click option'
Aself
Bposition
Cmove
Dposition()
Attempts:
3 left
💡 Hint
Common Mistakes
Using self instead of position
Calling position as a function
3fill in blank
hard

Fix the error in the strategy pattern implementation for chess moves.

LLD
class MoveStrategy:
    def execute(self, piece, position):
        [1]
Drag options to blanks, or click blank then click option'
Araise NotImplementedError
Breturn piece.move(position)
Cpass
Dprint('Move executed')
Attempts:
3 left
💡 Hint
Common Mistakes
Using pass which does nothing
Returning a call without implementation
4fill in blank
hard

Fill both blanks to implement a dictionary mapping piece names to their move strategies.

LLD
move_strategies = {
    'Knight': [1],
    'Bishop': [2]
}
Drag options to blanks, or click blank then click option'
AKnightMoveStrategy()
BBishopMoveStrategy()
CPawnMoveStrategy()
DQueenMoveStrategy()
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up strategy classes
Using wrong piece names
5fill in blank
hard

Fill all three blanks to complete the function that selects and executes a move strategy based on piece type.

LLD
def execute_move(piece_type, position):
    strategy = move_strategies.get([1], [2])
    if strategy:
        return strategy.execute([3], position)
    else:
        return "Invalid piece type"
Drag options to blanks, or click blank then click option'
Apiece_type
BNone
Dposition
Attempts:
3 left
💡 Hint
Common Mistakes
Using position as dictionary key
Passing wrong arguments to execute