Microsoft Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

Do not really get the point, anyway, here is the power function.

#include <iostream>

double power(double x, int y)
{
	if (y < 0)
		return 1 / power(x, -y);

	double res = 1;
	double base = x;

	while (y > 0)
	{
		if (y & 1 == 1)
		{
			res *= base;
		}
		base *= base;
		y >>= 1;
	}

	return res;
};


int main()
{
	double res = power(2, 5);

	return 0;
}

- hao.liu0708 August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(x^y + y^x)+(x^y - y^x)+(x^y/y^x)+(x^y*y^x)
This is your question?

- Vincent August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int improvePowCompute(int obj,int times){

HashMap<Integer,Integer> result_hold=new HashMap<Integer,Integer>();
result_hold.put(0, 1);
result_hold.put(1, obj);
return compute_process(obj,times,result_hold);
}

public static int compute_process(int obj,int times,HashMap<Integer,Integer> result_hold){
if(times<0){
return Integer.MIN_VALUE;
}
if(times%2==0){
if(result_hold.containsKey(times/2)){
int tmp=result_hold.get(times/2);
result_hold.put(times, tmp*tmp);
return tmp*tmp;
}
else{
int tmp=compute_process(obj,times/2,result_hold);
result_hold.put(times, tmp);
return tmp*tmp;
}
}
else{
if(result_hold.containsKey((times-1)/2)){
int tmp=result_hold.get((times-1)/2);
result_hold.put(times, tmp*tmp*obj);
return tmp*tmp*obj;
}
else{
int tmp=compute_process(obj,(times-1)/2,result_hold);
result_hold.put(times, tmp*tmp*obj);
return tmp*tmp*obj;
}
}
}

- Chengyun Zuo August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does the question say X and Y are integers?

- dc360 August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

expressing the power as a sum of powers of two, evaluating each of the terms by left shift and then multiplying them would be a better option.
For example, 5^6=5^(2+4)=(5^2)*(5^4)
These two operations can be done by simple left shift.
How abt this idea??

- madhu August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

by left shifting you can multiply by powers of two
but here you need to multiply 5 by 5 only, not by 2

- Tushar August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ideone.com/n5aqx
 
import java.util.*;
import java.lang.*;
 
class Main
{
        public static void pow(int a, int b)
        {
                double val1 = 1, val2 = 1;
 
                if (b!=0)
                {
                        int counter = Math.abs(b);
                        while(counter>0)
                        {
                                if (b<0)
                                {
                                        val1 /= a;
                                }
                                else
                                {
                                        val1 *= a;
                                }
                                counter--;
                        }
 
                        for(int i=0;i<a;i++)
                        {
                                val2 *= b;
                        }
                }
                else
                {
                        val1 = 0;
                        val2 = 0;
                }
 
                System.out.println("Val1: "+val1);
                System.out.println("Val2: "+Math.ceil(val2));
 
        }
 
        public static void main (String[] args) throws java.lang.Exception
        {
                pow(2, -4);
        }
}

- am15851 August 25, 2012 | 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