Bird
0
0

Which of the following code snippets correctly updates the current player index to the next player in a circular list of 4 players?

easy🧠 Conceptual Q12 of 15
LLD - Design — Tic-Tac-Toe Game
Which of the following code snippets correctly updates the current player index to the next player in a circular list of 4 players?
Acurrent_index = (current_index + 1) % 4
Bcurrent_index = current_index + 1
Ccurrent_index = current_index - 1 % 4
Dcurrent_index = current_index * 4
Step-by-Step Solution
Solution:
  1. Step 1: Understand circular indexing

    To cycle through players, we add 1 and wrap around using modulo.
  2. Step 2: Check each option

    current_index = (current_index + 1) % 4 correctly uses modulo to wrap index from 3 back to 0.
  3. Final Answer:

    current_index = (current_index + 1) % 4 -> Option A
  4. Quick Check:

    Modulo ensures circular turn cycling [OK]
Quick Trick: Use modulo (%) to cycle player index [OK]
Common Mistakes:
MISTAKES
  • Forgetting modulo causes index overflow
  • Using subtraction incorrectly
  • Multiplying index instead of incrementing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes