Interview Question


Country: United States




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

Divide until you either have no remainder or the remainder is one you've seen before. Then print and insert "(" at the place where you saw the remainder before and ")" at the end, if a repeating fraction

public static void main(String[] args) {

        int x = 10044;
        int y = 70;
        // Remember all remainders seen
        List<Integer> remainders = new ArrayList<>();
        int r;
        List<Integer> result = new ArrayList<>();
        boolean added = false;
        do {
            result.add(x / y);
            r = x % y;
            x = r * 10;
            if (!remainders.contains(r)) {
                remainders.add(r);
                added = true;
            } else {
                added = false;
            }
        // loop until no remainder or we've seen the remainder before.
        // Also make sure we do at least one after 0 for cases like 10/3
        } while (r != 0 && (added || result.size() == 1));

        boolean repeating = false;
        for (int i = 0; i < result.size(); i++) {
            if (i == 1) {
                System.out.print(".");
            }
            if (!repeating && remainders.indexOf(r) == i - 1) {
                System.out.print("(");
                repeating = true;
            }
            System.out.print(result.get(i));
        }
        if (repeating) {
            System.out.print(")");
        }
    }

- Anonymous July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So for a number like 1.12222(34) you will give the wrong answer?

- Anonymous July 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This looks right. Notice that he is looking at repeating remainder.

So for example of 1/7

Remainder is 1.
Mutiply by 10.

Now 7 goes 1 time in 10, and remainder is 3.

0.1

Multiply 3 by 10 = 30

7 goes 4 times and remainder is 2.

0.14

2*10 = 7*2 + 6

0.142

6*10 = 7*8 + 4

0.1428

4*10 = 7*5 + 5

0.14285

5*10 = 7*7 + 1

0.142857

remainder 1 has come again, so we are going to repeat.

Thus 1/7 = 0.(142857)

- Anonymous July 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

x=2777503, y=2475000 correctly gives 1.12222(34)

- Anonymous July 04, 2014 | 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