## 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<>();
do {
r = x % y;
x = r * 10;
if (!remainders.contains(r)) {
} else {
}
// 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(")");
}
}``````

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

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

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

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)

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

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

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.

### 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.