Google Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
public class Coefficients {
public a;
public b;
}
public static Coefficients simplifyEq(String equation) {
if (equation == null || equation.length == 0) {
throw new IllegalArguementException();
}
int yPrefix;
int a;
int b;
int multiplier = 1;
int current;
for(int index; index < equation.length; index ++) {
char c = equation.charAt(index);
if (c == '+') {
b += multiplier * current;
current = 0;
multiplier = 1;
continue;
} else if (c == '-') {
b += multiplier * current;
current = 0;
multipiler = -1;
continue;
}
if (c == 'y') {
yPrefix += multiplier * current;
current = 0;
continue;
} else if (c == 'x') {
a += multiplier * current;
current = 0;
continue;
}
// if c is int
switch (c) {
case '0':
case '1':
case '9':
current = current * 10 + convertCharToInt(c);
break;
default:
throw new IllegalArguementException();
}
}
Coefficients coef = new Coefficients();
coef.a = a / yPrefix;
coef.b = b / yPrefix;
return coef;
}
- yura.gunko April 17, 2017