## 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.

Comment hidden because of low score. Click to expand.
0

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..

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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...

Comment hidden because of low score. Click to expand.
0

@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.

Comment hidden because of low score. Click to expand.
0

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

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

something related

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

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

(n+k-1)C(n)

Comment hidden because of low score. Click to expand.
0

what's C?

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.

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
-1
of 1 vote

repetition allowed?

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.

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.

### 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.