Given a set top box: a, b, c, | CareerCup
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.
public class GoogleMovieLookup {
public static void main(String[] args) {
lookUpMovie("up", 5);
}
public static void lookUpMovie(String str, int width) {
char[] arr = str.toCharArray();
int prevPosition = 0;
int prevRow = 0;
for(char c : arr) {
// Tricks I learned from a solution on CareerCup.
// Get the horizontal distance moved.
int position = (c - 'a') % width;
// Get the vertical distance moved.
int row = (c - 'a') / width;
// Print horizontal movement.
int deltaHorizontal = position - prevPosition;
if(deltaHorizontal != 0) {
if(deltaHorizontal > 0)
printCommand("r", deltaHorizontal);
else if(deltaHorizontal < 0)
printCommand("l", deltaHorizontal*-1);
prevPosition = position;
}
// Print vertical movement.
int deltaVertical = row - prevRow;
if(deltaVertical != 0) {
if(row - prevRow > 0)
printCommand("d", deltaVertical);
else if(row - prevRow < 0)
printCommand("u", deltaVertical*-1);
prevRow = row;
}
// Select after making the necessary movement.
printCommand("!", 1);
}
}
// Helper method to print a command n times.
public static void printCommand(String command, int n) {
for(int i=0; i < n; i++)
System.out.print(command);
}
}
Read full article from Given a set top box: a, b, c, | CareerCup
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.
public static String getPath(int w, char[] str) {
int i = 0;
StringBuilder sb = new StringBuilder();
int curR = 0;
int curC = 0;
while (i < str.length) {
int destR = (str[i] - 'a') / w;
int destC = (str[i] - 'a') % w;
while (curC > destC) {
sb.append('l');
curC--;
}
while (curR > destR) {
sb.append('u');
curR--;
}
while (curC < destC) {
sb.append('r');
curC++;
}
while (curR < destR) {
sb.append('d');
curR++;
}
sb.append('!');
i++;
}
return sb.toString();
}
https://github.com/Simiopolis/careercup-java/blob/master/src/google/GoogleMovieLookup.javapublic class GoogleMovieLookup {
public static void main(String[] args) {
lookUpMovie("up", 5);
}
public static void lookUpMovie(String str, int width) {
char[] arr = str.toCharArray();
int prevPosition = 0;
int prevRow = 0;
for(char c : arr) {
// Tricks I learned from a solution on CareerCup.
// Get the horizontal distance moved.
int position = (c - 'a') % width;
// Get the vertical distance moved.
int row = (c - 'a') / width;
// Print horizontal movement.
int deltaHorizontal = position - prevPosition;
if(deltaHorizontal != 0) {
if(deltaHorizontal > 0)
printCommand("r", deltaHorizontal);
else if(deltaHorizontal < 0)
printCommand("l", deltaHorizontal*-1);
prevPosition = position;
}
// Print vertical movement.
int deltaVertical = row - prevRow;
if(deltaVertical != 0) {
if(row - prevRow > 0)
printCommand("d", deltaVertical);
else if(row - prevRow < 0)
printCommand("u", deltaVertical*-1);
prevRow = row;
}
// Select after making the necessary movement.
printCommand("!", 1);
}
}
// Helper method to print a command n times.
public static void printCommand(String command, int n) {
for(int i=0; i < n; i++)
System.out.print(command);
}
}
Read full article from Given a set top box: a, b, c, | CareerCup