## Google Interview Question for SDETs

Country: United States
Interview Type: Phone Interview

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

``````import java.util.*;
import org.junit.*;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;

/**
*  |1,2,3|
*  |4,5,6|
*  |7,8,9|
*  |0|
*/

public class CalculatorExample extends TestCase{
int[][] calculator= {{1,2,3},{4,5,6},{7,8,9},{0}};
int[][] LMove= {{-1,-2},
{-1,2},
{1,2},
{1,-2},
{-2,-1},
{-2,1},
{2,-1},
{2,1}};

public int[] getXYByNumber(int number) {
int[] returnArray = new int[2];
if (number==0) {
returnArray[0]=3;
returnArray[1]=0;
return returnArray;
}
for (int i=0; i <3; i++) {
for (int j=0; j<3; j++) {
if (calculator[i][j]==number) {

returnArray[0]=i;
returnArray[1]=j;
break;
}
}
}
return returnArray;
}

public int getNumberByXY(int x, int y) {
try {
if ((x==3)&&(y<0)&&(y>3))
return 0;
if ((x<0)&&(x>4))
return -1;
if ((y<0)&&(y>3))
return -1;
return calculator[x][y];
}catch(Exception e) {
return -1;
}

}

public Set getAllLMoveNumber(int start) {
Set<Integer> set = new HashSet();
int[] xy=getXYByNumber(start);
for (int[] move : LMove) {
int x=xy[0]+move[0];
int y=xy[1]+move[1];
if (getNumberByXY(x,y)!=-1){
//System.out.println("x:"+x+",y:"+y);
//System.out.println("x:"+x+",y:"+y+",number:"+getNumberByXY(x,y));
}
}
return set;
}

public void inputNumber(int input) {
//System.out.println("input:"+input);
Iterator itr = getAllLMoveNumber(input).iterator();
while(itr.hasNext())
System.out.println("Input ["+input+"]"+itr.next());
}

public void test_allPossibleNumber() {
inputNumber(1);
inputNumber(2);
inputNumber(3);
inputNumber(4);
inputNumber(5);
inputNumber(6);
inputNumber(7);
inputNumber(8);
inputNumber(9);
inputNumber(0);
}

}

Output:
Input [1]6
Input [1]8
Input [2]7
Input [2]9
Input [3]4
Input [3]8
Input [4]3
Input [4]9
Input [5]0
Input [6]1
Input [6]7
Input [7]2
Input [7]6
Input [8]1
Input [8]3
Input [9]0
Input [9]2
Input [9]4
Input [0]5
Input [0]9``````

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

5 doesn't have any @wingchuihk.

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

``````import java.util.*;
public class HelloWorld{

public static void main(String []args){

generate(6);
}

public static void generate(int number){

HashMap<Integer, HashMap<String, Integer>> cache = new HashMap<Integer, HashMap<String, Integer>>();

cache.put(0, new HashMap<String, Integer>());
cache.get(0).put("x", 3);
cache.get(0).put("y", 2);

cache.put(1, new HashMap<String, Integer>());
cache.get(1).put("x", 2);
cache.get(1).put("y", 1);

cache.put(2, new HashMap<String, Integer>());
cache.get(2).put("x", 2);
cache.get(2).put("y", 2);

cache.put(3, new HashMap<String, Integer>());
cache.get(3).put("x", 2);
cache.get(3).put("y", 3);

cache.put(4, new HashMap<String, Integer>());
cache.get(4).put("x", 1);
cache.get(4).put("y", 1);

cache.put(5, new HashMap<String, Integer>());
cache.get(5).put("x", 1);
cache.get(5).put("y", 2);

cache.put(6, new HashMap<String, Integer>());
cache.get(6).put("x", 1);
cache.get(6).put("y", 3);

cache.put(7, new HashMap<String, Integer>());
cache.get(7).put("x", 0);
cache.get(7).put("y", 1);

cache.put(8, new HashMap<String, Integer>());
cache.get(8).put("x", 0);
cache.get(8).put("y", 2);

cache.put(9, new HashMap<String, Integer>());
cache.get(9).put("x", 0);
cache.get(9).put("y", 3);

int[][] LMove= {{-1,-2},
{-1,2},
{1,2},
{1,-2},
{-2,-1},
{-2,1},
{2,-1},
{2,1}};

int currentX = cache.get(number).get("x");
int currentY = cache.get(number).get("y");

for (int[] move : LMove) {

int destX = currentX + move[0];
int destY = currentY + move[1];

HashMap<String, Integer> point= new HashMap<String, Integer>();

point.put("x", destX);
point.put("y", destY);
int destNum = getKeyFromValue(cache, point);
if(destNum !=10){

System.out.println(destNum);

}

}

}

public static int getKeyFromValue(HashMap hm, Object value) {
for (Object o : hm.keySet()) {
if (hm.get(o).equals(value)) {

int i = (int) o;
return i;
}
}
return 10;
}
}``````

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

The phone number actually have 0 under the 8 like below. And 5 does not fulfilled the rule. Therefore it has none.
|1,2,3|
|4,5,6|
|7,8,9|
| ,0, |

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

Its a simple co-ordinate geometry problem. You can use mod 3 and solve it easily

``````/**
*  |1,2,3|
*  |4,5,6|
*  |7,8,9|
*  |0|
*/
public class KnightMoveInCalc{

private static void find(int num) {
if(num<0) return;
int x = num%3-1;
int y = num/3;
if(num == 0) {
x = 4;
y = 0;
}

int[] dxs = {1,2,-1,-2};
int[] dys = {1,2,-1,-2};
for(int dx:dxs) {
for(int dy:dys) {
if(Math.abs(dx)!=Math.abs(dy)) {
if((x+dx)>=0 && (x+dx)<3 &&
(y+dy)>=0 && (y+dy)<3) {
System.out.println((y+dy)*3+(x+dx)+1);
} else if((y+dy)==3 && (x+dx)==0) {
System.out.println("0");
}
}
}
}
}

public static void main(String[] args) {
find(5);
}
}``````

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

Its a simple geometry problem, use mod3 and solve it.

``````/**
*  |1,2,3|
*  |4,5,6|
*  |7,8,9|
*  |0|
*/
public class KnightMoveInCalc{

private static void find(int num) {
if(num<0) return;
int x = num%3-1;
int y = num/3;
if(num == 0) {
x = 4;
y = 0;
}

int[] dxs = {1,2,-1,-2};
int[] dys = {1,2,-1,-2};
for(int dx:dxs) {
for(int dy:dys) {
if(Math.abs(dx)!=Math.abs(dy)) {
if((x+dx)>=0 && (x+dx)<3 &&
(y+dy)>=0 && (y+dy)<3) {
System.out.println((y+dy)*3+(x+dx)+1);
} else if((y+dy)==3 && (x+dx)==0) {
System.out.println("0");
}
}
}
}
}

public static void main(String[] args) {
find(5);
}
}``````

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

``````package sk.radobenc.exercises;

import java.util.HashMap;
import java.util.Map;

/*
1 2 3
4 5 6
7 8 9
0
To adjust 0 position, fix the respective element in map m below.
*/

static class Pos {
private final int row;
private final int column;

Pos(int row, int column) {
this.row = row;
this.column = column;
}

@Override
public boolean equals(Object obj) {
Pos p = (Pos) obj;
return column == p.column && row == p.row;
}

@Override
public int hashCode() {
return 31 * (31 + column) + row;
}

@Override
public String toString() {
return new StringBuilder().append('[').append(row).append(',').append(column).append(']').toString();
}
}

public static void main(String[] args) {
Map<Pos, Integer> m = new HashMap<Pos, Integer>();
m.put(new Pos(0, 0), 1);
m.put(new Pos(0, 1), 2);
m.put(new Pos(0, 2), 3);
m.put(new Pos(1, 0), 4);
m.put(new Pos(1, 1), 5);
m.put(new Pos(1, 2), 6);
m.put(new Pos(2, 0), 7);
m.put(new Pos(2, 1), 8);
m.put(new Pos(2, 2), 9);
m.put(new Pos(3, 0), 0);
Map<Integer, Pos> revM = new HashMap<Integer, Pos>();
for (Map.Entry<Pos, Integer> e : m.entrySet()) {
revM.put(e.getValue(), e.getKey());
}
int[][] moves = { { 2, 1 }, { 2, -1 }, { 1, -2 }, { 1, 2 }, { -1, -2 }, { -1, 2 }, { -2, -1 }, { -2, 1 } };
for (int i = 0; i < 10; i++) {
for (int j = 0; j < moves.length; j++) {
Pos source = revM.get(i);
Pos target = new Pos(source.row + moves[j][0], source.column + moves[j][1]);
if (null == m.get(target)) {
continue;
}
System.out.println("input=" + i + " target=" + target + " targetSign=" + m.get(target));
}
}
}

}``````

output:

``````input=0 target=[2,2] targetSign=9
input=0 target=[1,1] targetSign=5
input=1 target=[2,1] targetSign=8
input=1 target=[1,2] targetSign=6
input=2 target=[2,2] targetSign=9
input=2 target=[2,0] targetSign=7
input=3 target=[2,1] targetSign=8
input=3 target=[1,0] targetSign=4
input=4 target=[2,2] targetSign=9
input=4 target=[0,2] targetSign=3
input=5 target=[3,0] targetSign=0
input=6 target=[2,0] targetSign=7
input=6 target=[0,0] targetSign=1
input=7 target=[1,2] targetSign=6
input=7 target=[0,1] targetSign=2
input=8 target=[0,0] targetSign=1
input=8 target=[0,2] targetSign=3
input=9 target=[3,0] targetSign=0
input=9 target=[1,0] targetSign=4
input=9 target=[0,1] targetSign=2``````

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.