Expedia Interview Question


Country: United States
Interview Type: Phone Interview




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

#include "stdafx.h"
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int epsilon = 3;
int cntepsilon = 0;
int cntresult = 0;
int fpcnt = -1;
double x = 9;
double y = 2;
double nom = x;
double denom = y;
double finalResult = 0;
double result = x;
if (x < y) {
while (cntepsilon < epsilon && result != 0)
{
result = result * 10;
result = result - denom;
cntresult += 1;
while (result >= denom)
{
result = result - denom;
cntresult += 1;
}
finalResult = finalResult + cntresult* pow(10, fpcnt);
fpcnt = fpcnt - 1;
cntepsilon += 1;
cntresult = 0;


}
cout << finalResult << endl;

}
else if (x>y)
{
while (result >= denom)
{
result = result - denom;
cntresult += 1;

}
cout << result << endl;
finalResult = cntresult;
cout << finalResult << endl;
if (result != 0)
{
cntresult = 0;
while (cntepsilon < epsilon && result != 0)
{
result = result * 10;
result = result - denom;
cntresult += 1;
while (result >= denom)
{
result = result - denom;
cntresult += 1;
}
finalResult = finalResult + cntresult* pow(10, fpcnt);
fpcnt = fpcnt - 1;
cntepsilon += 1;
cntresult = 0;
}
}
cout << finalResult << endl;

}
else {
finalResult = 1;
}
return 0;
}

- Niloofar Hezarjaribi October 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def find_x_by_y(x, y, e):
	p = 0
	q = x * y
	while abs(p-q) > e:
		m = (p + q) / 2.0
		c = (m * y) - x
		if c < 0:
			p = m
		else:
			q = m
	return p

- montu February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DivisionToEpsilon {
	
	public static void main(String[] args){
		int epsilon = 3;
		
		double d1 = 2.223456;
		double d2 = 2.1;
		
		System.out.println(d1/d2);
		DoubleWithEpsilon doubleWithEpsilon = new DoubleWithEpsilon(epsilon);
		
		DoubleWithEpsilon result = doubleWithEpsilon.divid(d1, d2);
		
		System.out.println(result);
		
	}

}

class DoubleWithEpsilon{
	int epsilon;
	int whole;
	int fraction;
	
	public DoubleWithEpsilon(int epsilon){
		this.epsilon = epsilon;
	}
	
	public DoubleWithEpsilon(int epsilon, double doubleNumber){
		this.epsilon = epsilon;
		if(doubleNumber<1){
			this.whole=0;
		}else{
			this.whole = (int) doubleNumber;
		}
		
		int powerE=1;
		for(int i=0; i<epsilon; i++){
			powerE = powerE*10;
		}
		int powerE1 = powerE*10;
		
		double tempDouble = doubleNumber-this.whole; //get the fraction from double.
		
		int tempInt0 = (int)(tempDouble*powerE);  //get the fist epsilon digits from fraction.
		int tempInt1 = (int)(tempDouble*powerE1); // get the fist epsilon+1 digits from fraction.
		int tempInt = tempInt1-tempInt0*10;       //get last digit of tempInt1.
		
		if(tempInt<5){                            //Round fraction of original double based on tempInt 
			this.fraction = tempInt0;
		}else{
			this.fraction = tempInt0+1;
		}
		
		if(this.fraction>=powerE){
			this.fraction = 0;
			this.whole = this.whole+1;
		}
	}
	
	public DoubleWithEpsilon divid(double d1, double d2){
		
		DoubleWithEpsilon doubleWithEpsilon = new DoubleWithEpsilon(this.epsilon, d1/d2);

		return doubleWithEpsilon;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(this.whole).append(".");
		if(this.fraction<10){
			sb.append("00").append(this.fraction);
		}else if(this.fraction<100){
			sb.append("0").append(this.fraction);
		}else{
			sb.append(this.fraction);
		}
		
		return sb.toString();
	}
	
	
}

- yankeson2013 March 08, 2017 | 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