Microsoft Interview Question for Software Engineer / Developers


Country: India




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

cin >> s;
n = strlen(s);

// initialise dp array with all zeros

for(i = 0; i < n; i+=2)
dp[i][i] = s[i];

for(l = 2; l < n; l+=2)
{
for(i = 0; i < n; i+=2)
{
j = i + l;
for(k = i ; k < j; k+=2)
{
dp[i][j] = max(dp[i][j], apply_operator(dp[i][k], dp[k+2][j], s[k+1] )) // here s[k+1] would be be operator
}
}
}
print dp[0][n-1]

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

wrong iteration
when you calculate dp[2,6] then you need dp[2,4] and dp[4,6].
dp[2,4] has been correctly computed in prev. iteration, but dp of [4,6] is still same as it's initialised value i.e. =0

a suggest using recursion and maintaining a set bit with each value of dp.
set bit would be initially 0 for all
then would be set to 1 for dp[i,i]
now if set bit is 0 then dp[] has to be computed and bit should be set to 1 otherwise it can be directly picked up from the table dp

- Anonymous September 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it's correct because when you compute dp[2,6], dp[4,6] should be computed at previous outer iteration, because its length l is smaller than dp[2,6].

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think DP will certainly help here but how dont able to figure it out so please any one post the logic :)

- anonymous September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Similar to chain matrix multiplication.

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

Yes.

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

V(a, op, b)
	if(op == '+')
		return(a+b)
	if(op == '-')
		return(a-b)
	if(op == '*')
		return(a*b)
	if(op == '/')
		return(a/b)	

MVE(i, j)
	return max{V(MVE(i, j-2), a[j-1], a[j]), V(MVE(i+2, j), a[i+1], a[i])}

- Prateek Caire September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

missed the base case

if(i == j)
    return a[i]

- Anonymous September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion; algorithm

;assumption - every operand is single digit, code can be expanded easily to handle operands >9
int toVal(string); converts a string number into integer/double
int EvalExp(string); evaluates the expression from left to right without any precedence
double MaxEvalExp(string expression, int start, int end, bool isFull) {
   char ch; int operand;
   if (start==end) return toVal(expression);
   for (int i=0; i<=(end-start); i++) {
       ch = expression[i];
       switch (ch) {
           case '+': operand = EvalExp(expression.substring(start,(i-start)+1));
                           result = operand + MaxEvalExp(expression, i+1, end, false);
                           if (isFull) && (result>maxValue) maxValue = result;

           case '-':  operand = EvalExp(expression.substring(start,(i-start)+1));
                           result = operand - MaxEvalExp(expression, i+1, end, false);
                           if (isFull) && (result>maxValue) maxValue = result;

           case '*':  operand = EvalExp(expression.substring(start,(i-start)+1));
                           result = operand * MaxEvalExp(expression, i+1, end, false);
                           if (isFull) && (result>maxValue) maxValue = result;
       
           case '/':  operand = EvalExp(expression.substring(start,(i-start)+1));
                           result = operand / MaxEvalExp(expression, i+1, end, false);
                           if (isFull) && (result>maxValue) maxValue = result;
       }
   }
}

int main() {
  string expression;
  cin >> expression;
  MaxEvalExp(expression, 0, expression.length-1, true);
  cout << "Maximum Value: " << maxValue;
  return 0;
}

- IntwPrep.MS December 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int EvalExp(int lVal, int rVal, char op){
	switch(op){
		case'+':
			return (lVal+rVal);
		case'-':
			return (lVal-rVal);
		case'*':
			return (lVal*rVal);
		case'/':
			return (lVal/rVal);
	}
}

int EvalMaxHelper(string str, int start, int end){
	int maxVal;
	int lVal,rVal;
	int val;
	const int NEG_INFINITY=-99999;
	stringstream ss;
	maxVal=NEG_INFINITY;
	for(int i=start; i<end; i++){
		if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str[i]=='/' ){
			lVal=EvalMaxHelper(str,start,i-1);
			rVal=EvalMaxHelper(str,i+1,end);
			val=EvalExp(lVal,rVal,str[i]);
			if (val>maxVal)
				maxVal=val;
		}
	}

	if (maxVal==NEG_INFINITY){
		ss<<str.substr(start,(end-start+1));
		ss>>maxVal;
	}

	return maxVal;
}

void EvalMax(){
	string str="2*3+5-3";
	cout << "Maximum value: " << EvalMaxHelper(str,0,str.length()-1) << endl;
}

- IntwPrep.MS January 04, 2014 | 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