Microsoft Interview Question for Software Engineer in Tests






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

Newton Raphson method..

- Paul October 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use binary search:

bool squareRoot(int n, int& ans){
if (n < 0)
return false;
int l = 0;
int r = n;
while (l<=r){
int mid = (l+r)/2;
int square = mid*mid;
if (square == n){
ans = mid;
return true;
}
else if (squre < n)
l = mid + 1;
else
r = mid - 1;
}
return false;
}

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

does dis gives sqrt of any no??
or only perfect squares

- baap October 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect squares of course.
I guess that should be enough for an interview unless it is the second round.

- erm October 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we do this
Pow( 2, .5*log2(n) ) ??

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

w w w.en.wikipedia.org/wiki/Newton's_method#Square_root_of_a_number

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

//! Implementation of Babylonian method for square root
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	long S = 125348;
	long tempS = S;
	long X0 = 1;
	float Xcurr, Xprev, diff;
	int numIterations = 0;
	int numBits = 0;
	//Begin with an arbitrary positive starting value x0 (the closer to the root, the better).
	while(tempS>0){
		tempS=tempS>>1; //shift bits to find the closest 2^k to the given number
		++numBits;
	}
	numBits = floor(numBits/2)+1;
	while(numBits>0){
		X0=X0<<1;
		--numBits;
	}
		
	Xcurr = X0;
	do{
		Xprev = Xcurr;
		Xcurr = .5*(Xprev + (S/Xprev));
		numIterations++;
	}while((Xprev-Xcurr)>0.001);
	cout<< "Square Root of "<< S<< " is "<< Xcurr<<endl;
	cout<< "Number of iterations :" <<numIterations;
}

- chennavarri October 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sry guys i missed one more point, he asked me to truncate it upto 4 decimal points. i used 2 while loops as binary search algo ( one with cond low < high, and another with high-low > 0.0001). Not sure he satisfied with my sol.

Pls input ur thoughts if it can done in a better way. all the best

- Billa October 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Bisection method, the simplest way to compute any invertible function
float SquareRoot(float y,float accuracy){
float x0=0,x1=y,temp=0;
float f0=x0*x0-y; // initially <=0
float f1=x1*x1-y; // initially >=0
float f_temp=temp*temp-y;
while((f0*f0-accuracy*accuracy)>0 && (f1*f1-accuracy*accuracy)>0)
{
temp=(x0+x1)/2;
f_temp=temp*temp-y;
if(f_temp*f0>0)
{
x0=temp;
f0=f_temp;
}
else{
x1=temp;
f1=f_temp;

}
}
if((f0*f0)<(f1*f1))
return x0;
else
return x1;
}

- John October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package careercup.google;

/**
*
* @author learner
*/
public class Squareroot {
private static final int Number = 160000;
private static final double tolerance = 0.0000001;

public static void main(String args[]){
System.out.println("sqrt of " + Number + " is " + sqrt(Number));
}

private static double sqrt(int number){
int count = 0;
double x = number;
double newx = 0.0;
do{
count += 1;
newx = 0.5 * (x + number/x);
if(Math.abs(x - newx) < tolerance){
System.out.println("total iterations : " + count);
return newx;
}
x = newx;
}while(true);
}
}

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

int main()
{
float n=0.09;
if (n < 0)
return false;
float l = 0;
float r = n;
while (l<=r){
int mid = (l+r)/2;
int square = mid*mid;
if (square == n){
ans = mid;
return true;
}
else if (squre < n)
l = mid + 0.1;
else
r = mid - 0.1;
}
return false;
}

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

I tried the code above, that doesn't work

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

Try this

void main()
{
        float x1,x2,n;
        printf("\n Enter a number ");
        scanf("%f",&n);
        x1 = n;
        x2=.5*(x1+n/x1);
        while((x1-x2)!=0)
        {
                x1=x2;
                x2=.5*(x1+n/x1);
                printf("\n x1= %f, x2= %f n/x1=%f",x1,x2,(n/x1));
        }
        printf("\n %f",x2);
}

- Anonymous January 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void main(){
long int n,i,sum=0,k=1,f=0;

printf("ENTER THE NUMBER");
scanf("%ld",&n);
while(1){
sum+=(2*k-1);
if(sum==n)
{
f=1;
break;
}
else if(sum>n)
break;
k++;
}

if(f==1)
printf("This Number is Square Number Square root is:%d",k);
else
printf("This Number is not a Square Number");

}

- debanjan ghosh March 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice application of Mathematics

- arijit March 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/// <summary>
/// Returns the closest square to a given integer
/// </summary>
/// <param name="n">The n.</param>
/// <returns>The closest square to a given integer</returns>
int ClosestSquare(int n)
{
for (int i = n/2; i > 0; i--)
{
if ((i * i) <= n)
{
return i;
}
}

return 0;
}

/// <summary>
/// Calculates sqrt of a number upto 4 decimals
/// </summary>
[TestMethod]
public void Sqrt()
{
double n = 10;

double result = ClosestSquare((int)n);
double r = n - (result*result);
double q = 2 * result;
double dec = 0.1;

while ((r != 0) && (dec > 0.00001))
{
n = r*100;

double nextQ = q*10 + 1;
int i = 1;

while (nextQ * i < n)
{
i++;
nextQ = q*10 + i;
}
i--;
r = n - (q * 10 + i) * i;
q = q*10 + 2*i;

result += i*dec;
dec /= 10;
}
}

- Ashish Kaila March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just apply the newton raphson method :)
its easy
code
float getsquareroots(float num)
{
float x1;
float x2=0;
float prev;
x1=num/2;
if(x1*x1==num)
return x1;
prev=x1;
while(x1!=x2)
{
x1=prev;
x2=x1-(x1*x1-num)/(2*x1);
if(x2*x2==num)
return x2;
prev=x2;
}
return x2;
}

- geeks July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
int number = give any number here;
int num1 = 1;
int num2 = num1;
int square = 1;
while (square < number)
{
square = num1 * num2;
if (square == number)
{
Console.WriteLine(num1);
}
else if (square < number)
{
num1++;
num2 = num1;
continue;
}
else if (square > number)
Console.WriteLine("number not a square");
}
Console.ReadLine();
}

- AN June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var sq = 81;

var num = 1;

var prod = num * num;
while (prod != sq) {
    num++;
    prod = num * num;
 
}
   alert(num);

- Anonymous October 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var sq = 81;

var num = 1;

var prod = num * num;
while (prod != sq) {
    num++;
    prod = num * num;
 
}
   alert(num);

- Anonymous October 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var sq = 81;

var num = 1;

var prod = num * num;
while (prod != sq) {
    num++;
    prod = num * num;
 
}
   alert(num);

- shawnp October 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var sq = 81;

var num = 1;

var prod = num * num;
while (prod != sq) {
    num++;
    prod = num * num;
 
}
   alert(num);

- shawnp October 19, 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