Google Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
int noYears(double x1, double x2, double R )
{
int n=0;
while ( x1 < x2 )
{
n++;
x1 = x1 + x1*R ;
}
if( x1 == x2)
return n;
else
return -1;
}
public static int getN(double r){
HashMap<Integer, Double> map=new HashMap();
int n=1, pn=0;
double v=(1+r), pv=1;
map.put(0, 1.0);
while(v<2){
map.put(n, v);
pn=n;
n*=2;
pv=v;
v=v*v;
}
if(v==2){
return n;
}
int c=pn;
double cv=pv;
while(n>=1){
n=pn;
pn/=2;
if(cv*map.get(pn)<2){
c+=pn;
cv*=map.get(pn);
}
else if(cv*map.get(pn)==2){
c+=pn;
return c;
}
}
return c+1;
}
double Year(double x1,double x2,double R){
- Jeff April 20, 2012// If I need to check data
return log(x2/x1)/log(1+R);
}