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

- Amit Kumar November 14, 2020 | Flag Reply
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));
}
}
}

}

- tal December 28, 2020 | Flag Reply
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)
}

- xuannhat25 March 17, 2021 | Flag Reply
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);

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

- dhanushka May 27, 2021 | Flag Reply
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))

- Anonymous September 18, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recently my friend took a Facebook interview for a software engineer he asked these types of questions:
Create a function that reverses a singly linked list.
Identify a cycle in a linked list:
Calculate the value of an equation in reverse Polish language.
Given two sorted arrays, merge them into a single sorted array.

These questions cover the multiple topics for software engineer at Facebook

- jesicasharma150 April 25, 2024 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More