Data Structures & AlgorithmsModerateICPC regional · game theory
Problem: Piles of stones are 3, 4 and 5. Two players alternately remove any positive number of stones from one pile, and whoever takes the last stone wins. Run the code and say who wins with optimal play: print 'first' or 'second'.
Problem: Piles of stones are 3, 4 and 5. Two players alternately remove any positive number of stones from one pile, and whoever takes the last stone wins. Run the code and say who wins with optimal play: print 'first' or 'second'.
piles = [3, 4, 5]
x = 0
for p in piles:
x ^= p
print('first' if x else 'second')What is printed?- A. first
- B. second
- C. draw
- D. depends on the piles
Correct answer: A. first
Under Nim with normal play, the first player wins when the XOR of the pile sizes is nonzero. Here 3 XOR 4 XOR 5 = 2, which is nonzero.
More Coding Interview Problems questions
- Problem: A subtraction game lets a player remove 1, 3 or 4 stones. Let g(n) be the Grundy number (mex of the Grundy values reachable in…
- Problem: Two fair six-sided dice are rolled. What is the expected value of the larger of the two faces (the maximum)?
- Problem: Words apple, app, apt, bat, banana are stored in a trie that keeps a count of words passing through each node. What does the code…
- Problem: The longest subarray with equally many 0s and 1s of a = [0, 1, 0, 0, 1, 1, 0] is needed. Run the prefix-balance code and print…
- Problem: Find the 3 smallest pair sums from A = [1, 7, 11] and B = [2, 4, 6], using a heap that starts with each A element paired with…
- Problem: An undirected connected graph has an Euler circuit (a closed walk using every edge exactly once) if and only if which condition…