Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
@emb's solution is correct
And here is a DP solution for those who does not believe in mathematics ;)
void probability() {
const int N = 10; // number of steps
float P[N+1][2*N+1]; // P[i][x] - probability to be at position 'x'
// after 'i' steps (-N <= x <= N)
memset(P, 0, sizeof(P));
P[0][N + 0] = 1.0f;
int i, x;
for(i = 1; i <= N; i++) {
for(x = -i; x <= i; x++) {
float sum = (x == -N ? 0 : P[i-1][N + x-1]);
if(x < N)
sum += P[i-1][N + x+1];
P[i][N + x] = sum / 2.0f;
}
}
for(x = -N; x <= N; x++) {
printf("Prob P[%d] = %.6f\n", x, P[N][x + N]);
}
}
output:
Prob P[-10] = 0.000977
Prob P[-9] = 0.000000
Prob P[-8] = 0.009766
Prob P[-7] = 0.000000
Prob P[-6] = 0.043945
Prob P[-5] = 0.000000
Prob P[-4] = 0.117188
Prob P[-3] = 0.000000
Prob P[-2] = 0.205078
Prob P[-1] = 0.000000
Prob P[0] = 0.246094
Prob P[1] = 0.000000
Prob P[2] = 0.205078
Prob P[3] = 0.000000
Prob P[4] = 0.117188
Prob P[5] = 0.000000
Prob P[6] = 0.043945
Prob P[7] = 0.000000
Prob P[8] = 0.009766
Prob P[9] = 0.000000
Prob P[10] = 0.000977
Why is the P[-10] and P[-8] different? The probability should be the same.
P[-10] = P[-9] + P(L)
P[-8] = P[-9] + P(R)
Since L and R have the same probablity, P[-8] and P[-10] should be the same.
Here's a straightforward code solution. The answer is: Probability: 0.246094
var X:Int // The initial position
var endPositions:[Int] // All the end positions after 10 moves
X = 0
endPositions = []
func move(currentPos:Int, attemptNumber:Int, maxAttemptsAllowed:Int) {
if attemptNumber == maxAttemptsAllowed {
endPositions.append(currentPos)
} else {
move(currentPos + 1, attemptNumber: attemptNumber + 1, maxAttemptsAllowed: maxAttemptsAllowed)
move(currentPos - 1, attemptNumber: attemptNumber + 1, maxAttemptsAllowed: maxAttemptsAllowed)
}
}
func calcProbability() -> Float {
let sameEndPositionOutcomeCount = endPositions.filter { (pos) -> Bool in
return pos == X
}.count
return Float(sameEndPositionOutcomeCount) / Float(endPositions.count)
}
move(X, attemptNumber:0, maxAttemptsAllowed:10)
print ("Probability: \(calcProbability())")
Probability: 0.246094
var X:Int // The initial position
var endPositions:[Int] // All the end positions after 10 moves
X = 0
endPositions = []
func move(currentPos:Int, attemptNumber:Int, maxAttemptsAllowed:Int) {
if attemptNumber == maxAttemptsAllowed {
endPositions.append(currentPos)
} else {
move(currentPos + 1, attemptNumber: attemptNumber + 1, maxAttemptsAllowed: maxAttemptsAllowed)
move(currentPos - 1, attemptNumber: attemptNumber + 1, maxAttemptsAllowed: maxAttemptsAllowed)
}
}
func calcProbability() -> Float {
let sameEndPositionOutcomeCount = endPositions.filter { (pos) -> Bool in
return pos == X
}.count
return Float(sameEndPositionOutcomeCount) / Float(endPositions.count)
}
move(X, attemptNumber:0, maxAttemptsAllowed:10)
print ("Probability: \(calcProbability())")
How many sequences are there with exactly 5 L and 5 R symbols? C(10, 5)
So the answer is C(10, 5) / 2^10
- emb November 08, 2015