Bird
0
0

Find the error in this move validation snippet:

medium📝 Analysis Q7 of 15
LLD - Design — Chess Game
Find the error in this move validation snippet:
def validate_knight_move(start, end):
    dx = abs(end[0] - start[0])
    dy = abs(end[1] - start[1])
    if dx == 2 and dy == 1 or dx == 1 and dy == 2:
        return True
    else:
        return False
AIncorrect calculation of dx and dy
BMissing return statement for invalid moves
COperator precedence causes incorrect evaluation
DUsing abs() instead of direct subtraction
Step-by-Step Solution
Solution:
  1. Step 1: Analyze conditional expression

    The condition lacks parentheses, so 'and' and 'or' operators may evaluate incorrectly.
  2. Step 2: Understand operator precedence

    Without parentheses, expression may be read as (dx == 2 and dy == 1) or (dx == 1) and (dy == 2), which is wrong.
  3. Final Answer:

    Operator precedence causes incorrect evaluation -> Option C
  4. Quick Check:

    Use parentheses to group conditions correctly [OK]
Quick Trick: Use parentheses to clarify complex conditions [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Assuming 'and' binds tighter than 'or'
  • Missing parentheses in conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes