Interview Question for Software Engineers


Country: United States




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

Naive recursion will be O(2^(max(N, M)). With dynamic programming you can make it O(N*M) which is a *HUGE* improvement.

Going from recursive approach to DP is not very hard if you think about the recursion tree.

C++ implementation with naive recursion and DP approach:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

unsigned scheduler_possibilities(unsigned m, unsigned n) {
	if (m == 0 || n == 0) {
		return 1;
	}
	return scheduler_possibilities(m-1, n)+scheduler_possibilities(m, n-1);
}

unsigned scheduler_possibilities_dp(unsigned m, unsigned n) {
	unsigned dim = max(m+1, n+1);
	vector<vector<unsigned> > dp(dim, vector<unsigned>(dim));

	for (size_t i = 0; i < dim; i++) {
		dp[i][0] = 1;
		dp[0][i] = 1;
	}

	for (size_t i = 1; i < dim; i++) {
		for (size_t j = 1; j < dim; j++) {
			dp[i][j] = dp[i-1][j]+dp[i][j-1];
		}
	}

	return dp[m][n];
}

int main(void) {
	cout << "Enter M and N" << endl;
	cout << "> ";

	unsigned m, n;
	while (cin >> m >> n) {
		//cout << scheduler_possibilities(m, n) << endl;
		cout << scheduler_possibilities_dp(m, n) << endl << "> ";
	}

	return 0;
}

- 010010.bin July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This can be written in a recursive fashion as such:

F(M, N) = F(M- 1, N) + F(M, N- 1)

Because we have to maintain the order of processes. If we started with Ma, next could either be Mb or Na (which is represented by F(M- 1, N)). Likewise, if we start with Na, next could either be Nb or Ma (which is represented by F(M, N - 1)).


In code, something like this could work:

public int num(int m, int n) {
	
	if (m <  0 || n < 0) {

		return 0;
	}

	if (m == 0) {

		return 1;
	}

	if (n == 0) {

		return 1;
	}

	return num(m , n-1) + num (m - 1, n);

}

We can then memoize to make it more efficient.

- SK July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@SK, these are combinations, i.e., C(M + N, N) or C(M + N, M)

indeed, since the order of instructions of one program is fixed, we denote
by 0's the instructions of the 1st program and by 1's - the instructions of the 2nd
Hence the problem reduces to computing the # of ways how to set M ones having M+N places which is combinations.

Example: M = 3, N = 2.
11100 -> Ma Mb Mc Na Nb
01101 -> Na Ma Mb Nb Mc
... etc

- pavel.em October 04, 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