FastPrepTrain Rotation Reachability Probability

Train Rotation Reachability Probability

IMC Trading logoIMC Trading● EasyNEW GRADOA
Learn

Problem statement

A train has carCount distinct cars and one fixed target order. Its only allowed move removes the last car and puts that car at the front. The conductor may make zero or more moves.

For this exercise, assume the initial order is chosen uniformly from all permutations, and generalize the reported three-car question to the supplied carCount. Return the probability that the conductor can reach the target order.

For this exercise, assume a numeric result with relative-or-absolute tolerance 1e-9. Complete trainRotationProbability(carCount).

Function

trainRotationProbability(carCount: int) → double

Examples

Example 1

carCount = 3return = 0.5

For the reported three distinct cars, exactly three of the six initial orders are cyclic rotations of the target. The result is 3/6 = 0.5 under the stated uniform model.

Example 2

carCount = 1return = 1.0

There is only one initial order; zero moves already reaches the target.

Example 3

carCount = 4return = 0.16666666666666666

Exactly four of the 4! = 24 orders are reachable rotations of the target. The probability is 1/6.

Constraints

  • For this exercise, assume 1 <= carCount <= 12.
  • Every car is distinct; the target order is fixed before the random initial order is chosen.
  • The answer is a probability, rather than a count of arrangements or a yes/no answer for one supplied order.

More IMC Trading problems

See IMC Trading hiring insights
public double trainRotationProbability(int carCount) {
    // Write your solution here.
}
carCount3
expected0.5
Checking account…