Interview Question


Country: India
Interview Type: Written Test




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

This was an excellent exercise for me. I implemented my solution using Java, so there is no operand overloading. I have included the main section to display an example of usage.

Note: there are 3 private helper functions: gcd(Greatest Common Divisor), lcm(Least Common Multiple), reduce(to ensure the fraction is reduced after each calculation).

package fraction;

public class Fraction {

    public int num;
    public int den;
    
    public Fraction(){
        
    }
    
    public Fraction(int n, int d){
        num = n;
        den = d;
    }
    
    public Fraction addition(Fraction f1){
        Fraction result;
        int d = lcm(den, f1.den);
        int n = (num * d / den) + (f1.num *  d / f1.den);
        
        result = new Fraction(n, d);
        return reduce(result);
    }
    
    public Fraction subtraction(Fraction f1){
        Fraction result;
        int d = lcm(den, f1.den);
        int n = (num * d / den) - (f1.num *  d / f1.den);
        
        result = new Fraction(n, d);
        return reduce(result);
    }
    
    public Fraction multiplication(Fraction f1){
        Fraction result;
        int d = den * f1.den;
        int n = num * f1.num;
        
        result = new Fraction(n,d);
        return reduce(result);
    }
    
    public Fraction division(Fraction f1){
        Fraction result;
        int n = den * f1.num;
        int d = num * f1.den;
        
        result = new Fraction(n,d);
        return reduce(result);
    }
    
    public void display(){
        System.out.println(num + "/" + den);
    }
    
    public boolean isEqual(Fraction f1){
        int d = lcm(den, f1.den);
        int n1 = num * d / den;
        int n2 = f1.num *  d / f1.den;
        
        return n1 == n2;
    }
    
    public boolean lessThan(Fraction f1){
        int d = lcm(den, f1.den);
        int n1 = num * d / den;
        int n2 = f1.num *  d / f1.den;
        
        return n1 < n2;
    }
    
    public boolean greaterThan(Fraction f1){
        int d = lcm(den, f1.den);
        int n1 = num * d / den;
        int n2 = f1.num *  d / f1.den;
        
        return n1 > n2;
    }
    
    private int gcd(int a, int b){
        int temp;
        
        while(b > 0){
            temp = b;
            b = a % b;
            a = temp;
        }
        
        return a;
    }
    
    private int lcm(int a, int b){
        return (a*b) / gcd(a,b);
    }
    
    private Fraction reduce(Fraction f){
        int least = gcd(f.num, f.den);
        
        if(least <= 1){
            return f;
        }
        else{
            f.num /= least;
            f.den /= least;
            reduce(f);
        }
        
        return f;
    }
    
    
    //test section - main
    public static void main(String[] args) {
        Fraction f = new Fraction(5, 8);
        Fraction g = new Fraction(7, 16);
        Fraction result;
        
        result = f.addition(g);
        result.display();
        
        result = f.subtraction(g);
        result.display();
        
        
        result = f.division(g);
        result.display();
        
        result = f.multiplication(g);
        result.display();
        
        if(f.isEqual(f)) System.out.println("equal");
        if(f.isEqual(g)) System.out.println("equal");
        
        if(g.lessThan(f)) System.out.println("less");
        if(f.lessThan(g)) System.out.println("less");
        
        if(f.greaterThan(g)) System.out.println("greater");
        if(g.greaterThan(f)) System.out.println("greater");
    }
}

- masterjaso July 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are two operands?

- Jackie June 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. Two operands. And this is a design/implementation question. So you are free to make your assumptions for the implementation.

- AnOnYmOuS June 25, 2013 | Flag


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