## Facebook Interview Question for Software Engineer / Developers

Country: United States

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

``````uint8_t u8Rows=8, u8Cols=8;
vector<vector<int>> vecChessBoard(u8Rows, vector<int>(u8Cols,0));
std::stringstream oSS;

std::cout<<"Enter knight position row, col";
std::cin>>i>>j;				//i=row, j=col

//There can be 8 knight positions
int8_t i=0,j=0;
if((i+1 < u8Rows) and (j+1 < u8Cols))
oSS << "(" << i+1 << "," << j+2 << ")" << std::endl;
if((i-1 > -1) and (j+1 < u8Cols))
oSS << "(" << i-1 << "," << j+1 << ")" << std::endl;
if((i+2 < u8Rows) and (j+1 < u8Cols))
oSS << "(" << i+2 << "," << j+1 << ")" << std::endl;
if((i+2 < u8Rows) and (j-1 > -1))
oSS << "(" << i+2 << "," << j-1 << ")" << std::endl;
if((i+1 < u8Rows) and (j-2 > -1))
oSS << "(" << i+1 << "," << j-2 << ")" << std::endl;
if((i-1 > -1) and (j-1 > -1))
oSS << "(" << i-1 << "," << j-1 << ")" << std::endl;
if((i-2 > -1) and (j-1 > -1))
oSS << "(" << i-2 << "," << j-1 << ")" << std::endl;
if((i-2 > -1) and (j+1 < u8Cols))
oSS << "(" << i-2 << "," << j+1 << ")" << std::endl;

std::cout << oSS.str();``````

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

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));
}
}
}

}

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

``````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)
}``````

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

``````static Set<Location> existingValues = new HashSet<Location>();
public static void findLocations(int startX, int startY) {
final Location toAdd = new Location(startX, startY);

if (available.isEmpty()) {
} 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;
}
}``````

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

``````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))``````

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.