Facebook Interview Question for Software Engineer / Developers






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

double squareroot(double num)
{
double number;
number =num/2;
do
{
number=(number+num/nuber)/2
}
while (number*number-num >0.00001)

return number;
}




}

- Anonymous April 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is an ideal solution based on Newton's Method.

- Anonymous May 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correction: Write a function to compute the square root of an integer.

- Anonymous April 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

double sqroot(int i)
{
 if(i<0) {
  return -1;
 }

 double t = 1.0;

 double tsq, ival;
 tsq = t*t;
 ival = i*l;
 while(1) {
  tsq = t*t;
  ival = i*1.0;

  if(fabs(tsq-ival)<EPS) {
    break;
  }

  t+=0.001;
  
  if(t>=i)
   break;
 }
 return t;
}

- Manoj April 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops! forgot -

#define EPS (0.01) /* anything like 0.1, 0.01 depending on precision*/

- Manoj April 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple way to compute the sqrt is by adding the odd numbers till it becomes greater than that number.
so, 1 + 3 <= 4. So, we count the number of times we do this operation.
likewise, sqrt (9) would be 1 + 3 + 5. So, it would be 3.
Here's how this looks.

int
sqrt(unsigned int n)
{
        int count = 0;
        int cur_term = 1;
        int next_term = 1;
        if (n < 2) {
                return (n);
        }   

        while (cur_term <= n) {
                cur_term += next_term;
                next_term += 2;
                count++;
        }   
        return (count);
}

- Balaji April 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Balaji

Awesome solution

- Anonymous May 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL at you being awed by that.

1) That solution is O(sqrt(n)), and how different is it from just computing consecutive squares and then comparing?

2) If all we are looking for the the integer part of the square root, just do a binary search, which is O(logn).

3) The solution (Newton's method) given before also computes the non-integral part, and converges really fast (O (log n)) and can be generalized to cube roots, k^th roots etc.


Lot of silly posts on this site.

- LOLer. May 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Balaji's function is too limited. The question was to write a function to compute sqrt of an INTEGER.
That means, if input is 2, answer should be 1.4142...

Above function doesnt do that... For 8 it'll compute 3 for example.

- Nikunj May 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The take away is to use the strategy of sum of odd number is a square number.
We can easily extend this strategy to right shift the number by two places, and performing the same operation till we achieve recurring / temination decimal number.

- Addin June 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wtf? That is all you gathered from this thread?

- LOLer June 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* SQUARE ROOT */

#include<iostream>
#include<cmath>

using namespace std;

double sqrt(int k)
{
       double lo=0,hi=1e10;
       double mid=(lo+hi)/2;
       
       while(abs(mid*mid-k)>1e-9)
       {
             
                                    
                       if(mid*mid<k)
                       lo=mid+1;
                       
                       else
                       hi=mid-1;
                       
                       mid=(lo+hi)/2;
                       }
                    return mid;   
                                            
                       }

- Anonymous July 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pity on you guys :-(
so many crappy posts...we should ask admin people to block these crappy posters once n for all...

- googler August 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

agreed man, they should have an intelligent moderator sifting through all the crap

- bobwobby November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

code for square root:

(define (sqrt-iter guess x)
  (if (good-enough? guess x)
      guess
      (sqrt-iter (improve guess x)
                 x)))

(define (improve guess x)
   (average guess (/ x guess)))

(define (average x y)
  (/ (+ x y) 2))

- lickie August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

float sqroot(float m)
{
   float i=0;
   float x1,x2;
   while( (i*i) <= m )
      i+=0.1;
   x1=i;
   for(int j=0;j<10;j++)
   {
      x2=m;
      x2/=x1;
      x2+=x1;
      x2/=2;
      x1=x2;
   }
   return x2;
}

- manoj August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* C implementation of http://en.wikipedia.org /wiki/Methods_of_computing_square_roots#Rough_estimation
*/

#include <stdio.h>
#include <math.h>


int
rough_estimate(int r)
{
int i = 1,d = 0,n,val;

while (r > i) {
i += i*10;
d++;
}


if ( (d % 2) == 0) {
/* d is even */
n = (d - 2)/2;
val = 6*pow((float)10,(float)n);
} else {
/* d is odd */
n = (d - 1)/2;
val = 2*pow((float)10,(float)n);
}

return val;
}

main()
{
int v,j;
int arg=30000;
float v1;

v = rough_estimate(arg);

v1 = (float)v;
for (j = 0; j < 10; j++) {
v1 = (v1 + arg/v1)/2;
}

printf("Square root of %d = %f\n",arg,v1);
printf("Square root of %d = %f\n",arg,sqrt(arg));
}

- S.M September 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

java implementation.

public class SquareRoot {
public SquareRoot() {}

public double computeSquareRoot(int value) {
double start = 0.0;
double end = value;
double middle = (start+end)/2;
double minError = 0.00001;
double error = middle*middle-value;

while(Math.abs(error) > minError) {
if(error < 0) start = middle;
else if(error > 0) end = middle;
middle = (start+end)/2;
error = middle*middle-value;
}

return middle;
}

public static void main(String[] args) {
SquareRoot sr = new SquareRoot();
double root = sr.computeSquareRoot(12);
System.out.println(root);
}
}

- hero October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mr Googler apart from contributing junk to this thread what have you contributed. Shut off from this board.

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

I see sth here:codinggeeks.blogspot.com/2010/04/computing-square-cube-roots.html
Is this the optimal solution?

- babbler April 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hero's Java code is perfect.

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

C# version:
static double Sqrt(double value)
{
const double Threshold = 0.0000001;

if (value < 0)
{
throw new ArgumentOutOfRangeException("value");
}

double min = 0, max = value;
while (true)
{
double mid = (min + max) / 2;
double diff = mid * mid - value;
if (Math.Abs(diff) <= Threshold)
return mid;

if (diff > 0)
max = mid;
else
min = mid;
}
}

- Jerry November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't work for 0.01

- r.ramkumar December 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will return the more accurate square root. If we increase round() function precision, output will be tuned better.

double sqrt(double num)
{
double x = num / 2;
while (Math.Round(x, 7) != Math.Round(((x + num / x) / 2), 7))
x = (x + num / x) / 2;

return x;
}

- Arasu March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double Sqrt(double n)
{
	double x1=n;	
	double x;
	do
	{
		x=x1;
		x1=.5*(x+n/x);	
	}while (fabs(x-x1)>1e-10);

	return x;
}

- RJ April 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double sqrt (double a) {

if (a <= 0) {
return a;
}

double s = a / 2;
double t;
do {
t = s;
s = (s + a / s) / 2;
}
while ( (t - s) > 1e-10 );

return s;
}

- bothell June 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Babylonian method

public double squareRoot(int x){
		double res = (600.0+x/600.0)/2;
		double lastResult = Double.NaN;
		while(res != lastResult){
			lastResult =res;
			res = (res+x/res)/2;
		}
		return res;
	}

- jiahuang August 03, 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