Nutanix | Onsite | CoinGame

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2023-10-25Added By Anonymous
Source: Link

Nutanix | Onsite | CoinGame


Given :

1 2class Solution{ 3 String findWinner(char[] arr, int start, int end,int K) 4} 5 6char arr[N] : char array containing a coin 'C' 7start , end : start and end position 8K : rotation length 9 10@return : "A" or "B" or "draw"

Consider a 2 player game , each player takes alternate turn.

There is a coin at start position and has to be moved to end position.

The player who move to end will win.

Find out who will win


A or B or draw

Both players plays most optimally so if any of game state is repeated verdict is draw.

The only movement allowed to move coin is to choose subarray of length K and rotate it.


Condition :

1.subarray being rotated must contain coin.
2.subarray must be of length K i.e. cant choose partial subarray


e.g

[x,C,x,x,x,x,x]

st = 1 ,end = 5, K = 3


Possiblity 1 :

A -- possible subarray for rotation = [0-2] or [1-3] ([0-1] not a valid subarray)

[0-2] wont result in any move (but its a valid move).Lets say A chose [1-3]

[x,C,x,x,x,x,x] --> [x,x,x,C,x,x,x]

B-- chose [3,5]

[x,x,x,C,x,x,x] --> [x,x,x,x,x,C,x]

Verdict : B won


Possiblity 2 :

A : chose [0-2]

[x,C,x,x,x,x,x] --> [x,C,x,x,x,x,x]

B: knows if he chooses [1-3] A will win in next step so he also chooses [0-2], as same game state is repeated so its a draw

Verdict : draw


I tried solving using DP but cant complete the solution. Need help from community how to solve such questions

Comments