Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer - New Grad at Google:
Given a base 10 number, print the hexidecimal (base 16) representation of that number.
See more for this Google Software Engineer - New Grad Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
public static String intToStr(int val, int radix) {
char[] digits = new char[36];
for (int i = 0; i < 10; i++) {
digits[i] = (char)('0' + i);
}
for (int i = 10; i < 36; i++) {
digits[i] = (char)('A' + i);
}
String result = "";
boolean negative = false;
if (val < 0) {
negative = true;
}
val = Math.abs(val);
do {
result = digits[val%radix] + result;
val = val/radix;
} while (val != 0);
if (negative) {
result = "-" + result;
}
return result;
}
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
while (n > 0) {
int base = n% 16;
buffer.append((base<10 ? base : (Char)('a'+base-10));
n /= 16;
}
String result = buffer.toString().reverse();
Helpful Answer?
Yes |
No
Inappropriate?
printf ("%x",n); //n being the base 10 number
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 0 people found this helpful
by b:
string s = "";
int m = 0;
int next = d;
int a[16];
a[10] = "a";
a[11] = "b";
a[12] = "c";
a[13] = "d";
a[14] = "e";
a[15] = "f";
while (next > 15) {
m = next % 16;
if (m > 9 && m < 16)
s = a[m] + s;
else
s = m + s;
next = next / 16;
}
system.out.println(s);
}