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"

- minya March 02, 2018 | Flag Reply
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')

- saishg March 12, 2018 | Flag Reply


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