Interview Question


Country: United States




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

Here's a straightforward DP recurrence:

F(n, k) = F(n, k-1) + F (n-1, k-1) + F(n-2, k-1) + ... + F (0, k-1) = F(n-1, k) + F(n, k-1).

This equation allows a quadratic time, linear space (if you're clever) solution.

- eugene.yarovoi December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that's the good one ))
this recursion looks pretty much like combinations
where we have C(n,k) = C(n-1,k-1) + C(n-1,k)

so I suppose we can reduce it to C(N,K) finally..

- 111 December 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not entirely sure what you're referring to, but it is pretty much exactly the recurrence relation of the problem where you're asked to find the number of paths from one corner of a grid to another.

- eugene.yarovoi December 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should not it be
F(n,k) = F(n-1, k-1) + F(n, k-1)

- Atanu December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No.

Think of it this way: look at the k slots you have. For each slot, you can either put something there and stay there (n-1 items, still k slots), or not put something there and think about putting something at the next slot (k-1 slots, n items). Your recurrence relation would imply that once I've decided to put something into a slot, I can't contemplate putting a second item there.

- eugene.yarovoi December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it should be: F(n,k) := C(n+k-1,n)
.. not entirely sure though

- 111 December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's C? Are you talking about the number of ways to choose how to place N things in N+K-1 bins? I don't see why that would be the answer...

- eugene.yarovoi December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@asm: Besides, you can verify that theory by comparing the values. If we agree that the recurrence F(n, k) = F(n-1, k) + F(n, k-1) is correct, we can see that the other one isn't right.

- eugene.yarovoi December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah that's what I mean: C(n,k) denotes the number of ways to choose a subset of 'k' elements out of 'n'

I can only show it on an example..
Suppose we have n = 5 and k = 3. Than we can encode any solution
of the above system as a particular subset of 5 elements out of 7
in the following way:

5 = 4 + 1 + 0 -> 1111010
5 = 2 + 2 + 1 -> 1101101
5 = 3 + 2 + 0 -> 1110110
5 = 1 + 1 + 3 -> 1010111
5 = 1 + 0 + 4 -> 1001111
5 = 0 + 5 + 0 -> 0111110
and so on, thus it should give C(n+k-1,n) eventually

ps. your recurrence is definitely correct, it's easy to check

- 111 December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

something related

en.wikipedia.org/wiki/Partition_(number_theory)

- knap December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n+k-1)C(n)

- Anonymous December 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what's C?

- eugene.yarovoi December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

No. of Integral solutions = (N+1)^(K-1)
assuming x[i]={0,1,2...N)

Please correct me if I'm wrong.

- quark December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's definitely wrong. The integers have to add up to N. Not every combination adds up to N -- far from it!

- eugene.yarovoi December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

repetition allowed?

- Anonymous December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

2 obs:
1. We can think of the problem as having to distribute N ( > k ) balls in k bins, such that, no bin is empty. So, the problem can be reduced to one in which every bin is given 1 ball and remaining ( N - k ) balls are distributed over min( N - k, k ) bins.
2. For the reduced problem, we can pick any of min( N - k, k ) bins, and find a solution to it such that a bin being empty ( remember here by empty I mean, it already has one ball from the original description ), is allowed.

- knap December 16, 2011 | 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