Bloomberg LP Interview Question for Software Engineer / Developers






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

You can always easily get the ratio when the denominator is 10^n, where n is determined by the precision

Then you can divide by common denominators (in your example, you have 25/100, based on the precision, and the common denominator is 25, so you end up having 1/4).

To solve the 0.333 issue (you would want to have 1/3, not 333/1000), you can use ranges for the denominator, determined by the precision.
Instead of thinking about 333/1000 only, you think about 333/999, 333/1000 and 333/1001 (all three allowed by 0.001 precision), and pick the one that goes nicest - 333/999 = 1/3

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

Marian, how do you tell the precision of a double?
e.g if val=2.5526

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

void FindRatio(double val, double tolerance, int* numerator, int* denominator) {
int sign = 0;
if (val < 0) {
sign = 1;
val = -val;
}
int p = val, q = 1, t;
double tol = (val*q - p) / q;
while (tol > tolerance || tol < -tolerance) {
t = q / (val * q - p);
p = p * t + q;
q = q * t;
tol = (val * q - p) / q;
}
if (q < 0 && p < 0) {
q = -q;
p = -p;
}
if (sign) {
q = -q;
}
*numerator = p;
*denominator = q;
}

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

Your while loop wont even executed once. because you are assigning p = val and q = 1. then tol becomes = (val*1-val)/1 = 0 and the condition becomes

while (0 > tolerance || 0 < -tolerance)

none of which will be write.

Honestly speaking it is hard to comprehend your code without the provision of comments.

- Anonymous January 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findRatio(double val, double tolerance, int &numerator, int &denominator)
{
int negative = 0;
if (val < 0)
{
negative = 1;
val *= -1;
}
numerator = 0;
denominator = 1;
double result = 0.0;
while (fabs(result-val) > tolerance)
{
if (result < val)
numerator++;
else
denominator++;
result = (double)numerator/(double)denominator;
}
if (negative)
numerator *= -1;
return 0;
}

- Ying February 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

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

Good job, ying

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

clean solution.. nice thinking.

- skys February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to change:

while (fabs(result-val) > tolerance)

To:

while (fabs(result-val) > (tolerance*2))

So that

findRatio(0.24, 0.1) ~ 1/2

and

findRatio(0.26, 0.1) ~ 1/2

:)

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

I don't think while(fabs(result-val) > (tolerance * 2))

It is the difference between the desired value and the given value. For, the two values you considered 0.24 and 0.26, the desired value should be 0.25 and the difference of 0.24 and 0.26 with 0.25 will remain 0.01 thus no need to twice the tolerance. Please correct

- codey modey December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is Java version. Using BigDecimal since double has problems when comparing.

package test1;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Double2Ratio {
	static class Ratio {
		int numerator = 0, denominator = 0;

		public void setNumerator(int numerator) {
			this.numerator = numerator;
		}

		public void setDenominator(int denominator) {
			this.denominator = denominator;
		}

		public int getNumerator() {
			return numerator;
		}

		public int getDenominator() {
			return denominator;
		}
		
	}
	public static void main(String[] args) {
		Ratio result = new Ratio();
		BigDecimal val = new BigDecimal("0.25");
		BigDecimal tolerance = new BigDecimal("0.01");
		
		findRatio(val, tolerance, result);
		System.out.println("The ratio of 0.25" + " is " + result.getNumerator() + "/" + result.getDenominator());
		
		val = new BigDecimal("0.24");
		findRatio(val, tolerance, result);
		System.out.println("The ratio of 0.24" + " is " + result.getNumerator() + "/" + result.getDenominator());
		
		val = new BigDecimal("0.3333333");
		findRatio(val, tolerance, result);
		System.out.println("The ratio of 0.33" + " is " + result.getNumerator() + "/" + result.getDenominator());
		
		val = new BigDecimal("3.1415926");
		findRatio(val, tolerance, result);
		System.out.println("The ratio of 3.1415926" + " is " + result.getNumerator() + "/" + result.getDenominator());
	}

	private static void findRatio(BigDecimal val, BigDecimal tolerance, Ratio ratio)
	{ 
		boolean negative = false; 
		BigDecimal zero = new BigDecimal("0");
		BigDecimal minusOne = new BigDecimal("-1");
		if (val.compareTo(zero) < 0 ) 
		{ 
			negative = true; 
			val.multiply(minusOne); 
		} 
		int numerator = 0; 
		int denominator = 1; 
		BigDecimal result = zero; 
		BigDecimal abs = result.subtract(val).abs();
		while (abs.compareTo(tolerance) > 0) 
		{ 
			if (result.compareTo(val) < 0) 
				numerator++; 
			else 
				denominator++; 
			result = new BigDecimal(numerator).divide(new BigDecimal(denominator), 2, RoundingMode.HALF_EVEN); 
			abs = result.subtract(val).abs();
		} 
		if (negative) 
			numerator *= -1; 
		ratio.setNumerator(numerator); 
		ratio.setDenominator(denominator);
	}
}

The result:
The ratio of 0.25 is 1/4
The ratio of 0.24 is 1/4
The ratio of 0.33 is 1/3
The ratio of 3.1415926 is 22/7

- muyong2009 October 10, 2016 | 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