Microsoft Interview Question for Software Engineer in Tests






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

Also assume that a and b are always integers

- Anonymous May 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

See geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-2-5-years-about-algorithms-3

- solved May 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int pow(int a, int b)
{
int num = a;
int result = 0;

for(int i = 0; i < (b - 1); ++i) {
for(int j = 0; j < a; ++j) {
result += num;
}
num = result;
}
return result;
}

- d May 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even I have the same solution. But this will work only for positive powers. How about when b is negative??

My solution ::

public class a_power_b_using_add_subtract {
public static void main(String args[]) {
int a = 10, b = 3;
System.out.println(pow(a, b));
}

public static int pow(int a, int b) {

if (b == 0)
return 1;
else if (b == 1)
return a;
else {
int result = a;
for (int i = b; i > 1; i--) {
int temp = result;
for (int j = a; j > 1; j--) {
result = result + temp;
}
}
return result;

}
}
}

- A May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how will any of the solution mentioned above take care of negative powers?
for example pow (4, -2) ?

- Anonymous May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pow ( a, b)
break b in power of 2s. b= 2^k1 + 2^k2 + ...
now for each break up make left shift or right bit shift of a depending on b is +ve or -ve.
then add the each term by no of times equal to next term.

- neo May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Neo

could you please elaborate your solution.

- addy June 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

correction to dchitkara to work with -ve also
1.Convert -ve base to +ve base.

int pow(int a, int b)
{
int num,end;
int result = 0;
if(a<0)
num=a*(-1);
end=num;
for(int i = 0; i < (b - 1); ++i) {
for(int j = 0; j < end; ++j) {
result += num;
}
num = result;
}
int sign=1;
if(a<0 && b%2)
sign=-1;
return result*sign;
}

- Anonymous May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my solution for positive integers

int pow(int a, int b)
{
int num = a;
int result = 0;

for(int i = 0; i < (b - 1); ++i) {
for(int j = 0; j < a; ++j) {
result += num;
}
num = result;
} 
return result; 
}

- j May 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct! trace pow(4,2)

- csk June 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry about the above comment, it works fine!

- csk June 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, I pasted something else previously....this is the right code

#include <iostream>
using namespace std;

int pow(int a, int b)
{int ans, i, j, temp;
ans = a;
temp = a;
 if(b==0)
	return(1);
for(i=0; i<b-1; i++)
	{
	 for(j=0; j<a-1; j++)
		ans=temp+ans;
	 temp=ans;
	}
 return(ans);
}


int main()
{int a, b;
 cin>> a >> b;
 cout << pow(a,b);
}

- Anonymous May 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

MS IDC makes fool of people.
People have to come to office on weekends due to workload and do night outs, no work life balance. They pay 10-20% more make people labour.

Do take the feedback from employees before joining MS.

And work is junk, all junk wor from Redmond is transferred to IDC. Ask any team, whether they design, implement products or just do porting or maintenance or make tools.

- Anonymous June 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you stop posting this again and again? WE HEARD YOU ALRIGHT!!!

- Anonymous July 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

pow(n,a) = pow(n-1,a)*a
You can use this to build a recursive program to calculate the power
pow(n, val)
{
if(n==1) return val;
int ret = pow(n-1, val);
for(int i = 0 ;i < val;i++)
{
ret+=ret;
}
return ret;
}

- anindya June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int mul( int a , int b )
{

int x = 0 , y = a ;


while ( b )
{
if ( b&1 )
x = x + y ;

y = y + y ;

b = b >> 1 ;
}

return x;
}


int pow( int a , int b )
{
int x = 1 , y = a ;


while ( b )
{
if ( b&1 )
x = mul(x,y) ;

y = mul(y,y) ;

b = b >> 1 ;
}

return x;
}




****************************
This will compute a^b in O((log(b))^2).
mul(a,b) calculates a*b in O(log(b)) and pow(a,b) also similarly calculates a^b.

- Professor utonium + AmeyRB July 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this does not work for mul(3,-2)
pow(-3,2) etc...
I think it is only good for positive integers.
Thanks,

- erappy July 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Professor utonium + AmeyRB:
can u explain your logic in words ?????

- 1234 November 22, 2010 | Flag


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