Statistics

Problem Statement for "BallAndHats"

Problem Statement

A magician has invited you to play a game. For this game, the magician uses a special table. On the table there are three spots in a row. The spots are labeled 0, 1, and 2, in order. He places three hats onto the table, so that each hat covers one of the spots. He then takes a ball and places it under one of the hats. The hats are not transparent, so you cannot see the ball while it is under a hat. Next, the magician shuffles the hats by repeatedly swapping two adjacent hats. Each swap is done by sliding the hats along the table, never showing you the ball. Once the magician finishes swapping the hats, you have to guess the spot where the ball is.

You are given a String hats which describes the contents of the hats in the beginning of the game. The i-th character of hats is 'o' if the ball was initially on the spot i. Otherwise, the i-th character of hats is '.' (a period).

You are also given a int numSwaps. Assume that the magician swapped the hat that contained the ball exactly numSwaps times. Please remember that in our version of the game the magician always swaps two adjacent hats. Also, note that the total number of swaps in the game may be larger than numSwaps, because the magician may sometimes swap two hats that don't contain the ball.

Assume that the magician chose the swaps he makes uniformly at random. That is, in each turn with probability 50% he swapped the hats on spots 0 and 1, and with probability 50% he swapped the hats on spots 1 and 2. Return the number of the spot that is most likely to contain the ball at the end of the game. If multiple spots are tied for the largest probability, return the smallest one of them.

Definition

Class:
BallAndHats
Method:
getHat
Parameters:
String, int
Returns:
int
Method signature:
int getHat(String hats, int numSwaps)
(be sure your method is public)

Notes

  • Two hats are adjacent if their spots differ by 1.

Constraints

  • hats will contain exactly three characters.
  • hats will contain exactly one 'o' character.
  • hats will contain exactly two '.' characters.
  • numSwaps will be between 0 and 1000, inclusive.

Examples

  1. ".o."

    1

    Returns: 0

    The spots 0 and 2 are equally likely to contain the ball after the hat that contains it is swapped once. We return the smallest spot number, which is 0.

  2. "..o"

    0

    Returns: 2

    The ball does not change spots when 0 swaps are performed; therefore, the ball must be at spot 2.

  3. "o.."

    1

    Returns: 1

  4. "..o"

    2

    Returns: 0

  5. "o.."

    101

    Returns: 1

  6. "o.."

    1000

    Returns: 0

  7. ".o."

    1000

    Returns: 1

  8. "..o"

    1000

    Returns: 0

  9. "o.."

    0

    Returns: 0

  10. ".o."

    0

    Returns: 1

  11. "..o"

    1

    Returns: 1

  12. "o.."

    999

    Returns: 1

  13. ".o."

    999

    Returns: 0

  14. "..o"

    999

    Returns: 1

  15. "o.."

    2

    Returns: 0

  16. ".o."

    2

    Returns: 1

  17. "o.."

    17

    Returns: 1

  18. ".o."

    892

    Returns: 1

  19. "..o"

    727

    Returns: 1

  20. "..o"

    213

    Returns: 1

  21. "o.."

    777

    Returns: 1

  22. "o.."

    776

    Returns: 0

  23. ".o."

    774

    Returns: 1

  24. ".o."

    775

    Returns: 0

  25. "..o"

    231

    Returns: 1

  26. "..o"

    232

    Returns: 0

  27. ".o."

    444

    Returns: 1

  28. ".o."

    447

    Returns: 0

  29. "o.."

    3

    Returns: 1

  30. ".o."

    3

    Returns: 0

  31. "..o"

    3

    Returns: 1

  32. "o.."

    6

    Returns: 0

  33. "..o"

    4

    Returns: 0

  34. ".o."

    942

    Returns: 1

  35. ".o."

    101

    Returns: 0

  36. "..o"

    8

    Returns: 0

  37. "..o"

    100

    Returns: 0

  38. "o.."

    14

    Returns: 0

  39. ".o."

    4

    Returns: 1

  40. "..o"

    16

    Returns: 0

  41. ".o."

    399

    Returns: 0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: