Bird
0
0

Given this pseudocode for a game state manager:

medium📝 Analysis Q4 of 15
LLD - Design — Chess Game
Given this pseudocode for a game state manager:
state = 'MENU'
function changeState(newState) {
  if (newState == 'PLAYING' && state == 'MENU') {
    state = newState
  } else if (newState == 'PAUSED' && state == 'PLAYING') {
    state = newState
  } else if (newState == 'MENU') {
    state = newState
  }
}
changeState('PLAYING')
changeState('PAUSED')
changeState('MENU')

What is the final value of state?
A'PAUSED'
BUndefined
C'PLAYING'
D'MENU'
Step-by-Step Solution
Solution:
  1. Step 1: Trace state changes step-by-step

    Initial state is 'MENU'. changeState('PLAYING') sets state to 'PLAYING' because condition matches. Then changeState('PAUSED') sets state to 'PAUSED'. Finally, changeState('MENU') sets state back to 'MENU'.
  2. Step 2: Confirm final state

    After all calls, state is 'MENU'.
  3. Final Answer:

    'MENU' -> Option D
  4. Quick Check:

    Final state after calls = 'MENU' [OK]
Quick Trick: Trace each state change carefully in order [OK]
Common Mistakes:
  • Assuming state stays at 'PAUSED' after last call
  • Ignoring the last changeState call
  • Confusing conditions in if statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes