Google

  www.google.com
  www.google.com

Interview Question

Software Engineer Interview Los Angeles, CA

Given a set top box: a, b, c, d, e, f, g, h, i, j, k, l, m

  , n, o p, q, r, s, t u, v, w, x, y z Write code to give the character sequence given a word, For example, if the word is "CON", the function will print this: Right//now we're at B Right//now we're at C OK//to select C Down DOwn Right Right OK//to select O Left//now at N OK//to select N note: Be careful when you're at Z. if you go to the right, you will get stuck. Afterwards, the interviewer adds a space to the right of 'Z' to test the code.
Answer

Interview Answer

2 Answers

0

a=[['a','b','c','d','e'],
    ['f','g','h','i','j'],
    ['k','l','m','n','o'],
    ['p','q','r','s','t'],
    ['u','v','w','x','y'],
    ['z','~','~','~','~']]

def trace_word(s):
    x = 0
    y = 0
    c = a[y][x]
    print('At ' + c)
    for c in s:
        p = a[y][x]
        while p!=c:
            # find min and max character in the row y
            low = a[y][0]
            high = a[y][len(a[y])-1]
            # in row y?
            if low <= c and c <= high:
                if p < c: # move right
                    x+=1
                    print("Right//now we're at " + a[y][x])
                else: # move left
                    x-=1
                    print("Left//now we're at " + a[y][x])
                p = a[y][x]
                continue
            if c < p: # in upper row
                y-=1
                if y < 0:
                    print('Impossible'); return
                print("Up//now we're at " + a[y][x])
            else: # in lower row
                y+=1
                if y >= len(a):
                    print('Impossible'); return
                print("Down//now we're at " + a[y][x])
            p = a[y][x]
        print('OK//to select ' + c)

if __name__ == '__main__':
    trace_word('con')
    #trace_word('cozmo')

idispatch on Apr 17, 2013
0

public static void printString(String s)
{
    int cx = 0;
    int cy = 0;
    for (int i = 0; i < s.length(); i++)
    {
        int desiredI = s.charAt(i) - 'a';
        int dx = desiredI % 5;
        int dy = desiredI / 5;
        while (cy > dy) {
            System.out.print("UP ");
            cy--;
        }
        while (cx > dx) {
            System.out.print("LEFT ");
            cx--;
        }
        while (cy < dy) {
            System.out.print("DOWN ");
            cy++;
        }
        while (cx < dx) {
            System.out.print("RIGHT ");
            cx++;
        }
        System.out.println("PRESS");
    }
}

Rahul on May 4, 2013

Add Answers or Comments

To comment on this Question, Sign In with Facebook or Sign Up