## Google Interview Question for Java Developers

Country: United States

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

``````def nearestDouble(str):
if str[0] == str[1]:
return str[0]
num = int(str, 16)
doubles = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]
min_diff = 16
argmin = -1
# We could reduce the number of comparisons here.
# I'm searching for argument minimum in a dumb way to simplify the idea
for i in doubles:
diff = abs(i - num)
if diff < min_diff:
min_diff = diff
argmin = i
# We take last digit here because it would be something like 0x0d and I'm to lazy to read how to format hex
return hex(argmin % 16)[-1]

def shortenColor(rrggbb):
# We are using here additivity of loss function: we can minimize each component of rgb independently
r = nearestDouble(rrggbb[1:3])
g = nearestDouble(rrggbb[3:5])
b = nearestDouble(rrggbb[5:7])
return "#{}{}{}".format(r, g, b)

assert shortenColor("#09f166") == "#1e6"``````

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

``````def shorten(color):
color = int(color, 16)
lower = (color / 0x11) * 0x11
higher = ((color / 0x11) + 1) * 0x11

if (color - lower) < (higher - color):
return hex(lower)[3]
else:
return hex(higher)[3]

def nearest_short_color(long_color):
red = shorten(long_color[1:3])
green = shorten(long_color[3:5])
blue = shorten(long_color[5:7])

return '#{}{}{}'.format(red, green, blue)

if __name__ == '__main__':
print nearest_short_color('#09f166')``````

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.