Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

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

>> 252 / 1024.
0.24609375

- emb November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@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

- pavel.em November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- haroud November 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@haroud,
P[-8] = 1/2 * (P[-7] + P[-9])
while P[-10] = 1/2 * (P[-9] + 0) since P[-11] == 0
Generally, it holds that:

P(i, x) = 1/2* (P(i-1, x-1) + P(i-1, x+1)) if |x| <= i
and P(i, x) = 0 otherwise

since we cannot move more that 'i' points away from the origin in 'i' steps

- pavel.em November 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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())")

- napo November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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())")

- napostolov November 08, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More