Facebook Interview Question
Software Engineer / DevelopersCountry: United States
private static void printKnight(int i, int j)
{
if (i < 0 || i > 7 || j < 0 || j > 7)
{
Console.WriteLine("Error");
}
for (int x = -2; x < 3; x++)
{
if(x==0)
{
continue;
}
//check for overflow
if ((i + x) < 0 || (i + x) > 7)
{
continue;
}
if (x % 2 == 0)
{
//check for overflow
if ((j + 1) < 8)
{
Console.WriteLine(string.Format("({0},{1})", x + i, j + 1));
}
if ((j - 1) > -1)
{
Console.WriteLine(string.Format("({0},{1})", x + i, j - 1));
}
}
else
{
//check for overflow
if ((j + 2) < 8)
{
Console.WriteLine(string.Format("({0},{1})", x + i, j + 2));
}
if ((j - 2) > -1)
{
Console.WriteLine(string.Format("({0},{1})", x + i, j - 2));
}
}
}
}
package main
// Find target in chess board with given start position of knight
import (
"fmt"
)
// kight goes as L shape
//
// Up/Down first
// x,y x+1,y
// x,y-1 x+1,y-1
// x,y-2 x+1,y-2
// Left/Right first
// x,y x+1,y x+2,y
// x,y-1 x+1,y-1 x+2,y-1
// chess board is 64 * 64 square (0,0) (0,7) (7, 0) (7,7)
const (
MinX = 0
MinY = 0
MaxX = 7
MaxY = 7
)
type Point struct {
X int
Y int
}
func main() {
moveFuncs := []MoveFunc{
moveUpLeft,
moveUpRight,
moveDownLeft,
moveDownRight,
moveLeftUp,
moveLeftDown,
moveRightUp,
moveRightDown,
}
src := Point{7, 7}
possibleDests := []Point{}
for _, moveFunc := range moveFuncs {
moveFn := moveFunc
if dest, canMove := moveFn(src); canMove {
possibleDests = append(possibleDests, dest)
}
}
fmt.Println(possibleDests)
}
type MoveFunc func(src Point) (dest Point, canMove bool)
func validPoint(p Point) bool {
return p.X >= MinX && p.X <= MaxX && p.Y >= MinY && p.Y <= MaxY
}
// x+1,y-2 to x,y
func moveUpLeft(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 1,
Y: src.Y + 2,
}
return dest, validPoint(dest)
}
// x,y-2 to x+1,y
func moveUpRight(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 1,
Y: src.Y + 2,
}
return dest, validPoint(dest)
}
// x+1,y to x,y-2
func moveDownLeft(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 1,
Y: src.Y - 2,
}
return dest, validPoint(dest)
}
// x,y to x+1,y-2
func moveDownRight(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 1,
Y: src.Y - 2,
}
return dest, validPoint(dest)
}
// x+2,y-1 to x,y
func moveLeftUp(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 2,
Y: src.Y + 1,
}
return dest, validPoint(dest)
}
// x+2,y to x,y-1
func moveLeftDown(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 2,
Y: src.Y - 1,
}
return dest, validPoint(dest)
}
// x,y-1 to x+2, y
func moveRightUp(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 2,
Y: src.Y + 1,
}
return dest, validPoint(dest)
}
// x,y to x+2,y-1
func moveRightDown(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 2,
Y: src.Y - 1,
}
return dest, validPoint(dest)
}
static Set<Location> existingValues = new HashSet<Location>();
public static void findLocations(int startX, int startY) {
final Location toAdd = new Location(startX, startY);
Set<Location> available = existingValues.stream().filter(loc-> loc.x == toAdd.x && loc.y == toAdd.y).collect(Collectors.toSet());
if (available.isEmpty()) {
existingValues.add(toAdd);
} else {
return;
}
System.out.println("X" + Integer.toString(startX)+ " " + Integer.toString(startY));
if (startX+1 < 8 && startY+2 < 8) {
findLocations(startX+1, startY+2);
}
if (startX+1 < 8 && startY-2 > 0) {
findLocations(startX+1, startY-2);
}
if (startX-1 > 0 && startY-2 > 0) {
findLocations(startX-1, startY-2);
}
if (startX-1 > 0 && startY+2 < 8) {
findLocations(startX-1, startY+2);
}
if (startY+1 < 8 && startX+2 < 8) {
findLocations(startX+2, startY+1);
}
if (startY+1 < 8 && startX-2 > 0) {
findLocations(startX-2, startY+1);
}
if (startY-1 > 0 && startX-2 > 0) {
findLocations(startX-2, startY-1);
}
if (startY-1 > 0 && startX+2 < 8) {
findLocations(startX+2, startY-1);
}
}
static class Location {
int x;
int y;
Location(int x, int y) {
this.x = x;
this.y = y;
}
}
def knight_target():
def check_boundary(d1, d2):
for d in [d1, d2]:
if d <= 0:
return True
if d > 64:
return True
return False
def check_free(d1, d2):
return False # # needs implementation
def check_opponent(d1, d2):
return False # # needs implementation
def check_target(d1, d2):
if check_boundary(d1, d2):
return (d1, d2, 'X')
if check_free(d1, d2):
return (d1, d2, 'F')
if check_opponent(d1, d2):
return (d1, d2, 'O')
return (d1, d2, 'S')
start = [63, 15]
direction = [(2, 1), (1, 2)]
sign = [1, -1]
for d1, d2 in direction:
for s1 in sign:
for s2 in sign:
print(check_target(start[0] + d1*s1, start[1] + d2*s2))
- Amit Kumar November 14, 2020