Facebook Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

private static boolean check180(String[] arr) {
	    int length = arr.length;
	    Map<String,String> matcher = new HashMap<String, String>();
	    matcher.put("0","0");
	    matcher.put("1","1");
	    matcher.put("6","9");
	    matcher.put("8","8");
	    matcher.put("9","6");
	    
	    boolean oddLength = (length % 2 != 0);
	    if(oddLength && !arr[length/2].equals("8") && !arr[length/2].equals("0")) {
	        return false;
	    }
	    
	    int half = oddLength ? (length/2)+1 : length/2;
	    int i = 0;
	    int j=length-1;
	    for(;i<length/2;i++,j--) {
            if(!matcher.get(arr[i]).equals(arr[j])) {
                return false;
	        }
	    }
	    return true;
	}

- Anonymous April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Doesn't account for the wider edge cases.

function check180(array){
    var matches = {
        0: 0,
        1: 1,
        8: 8,
        6: 9,
        9: 6
    };

    var leftIdx, rightIdx;
    if(array.length % 2 === 0){
        leftIdx = Math.floor(array.length/2);
        rightIdx = leftIdx + 1;
    } else {
        var midPoint = Math.floor(array.length/2);
        leftIdx = midPoint - 1;
        rightIdx = midPoint + 1;
        var midValue = matches[array[midPoint]];
        if(!midValue || midValue === matches[6] || midValue === matches[9]){
            return false;
        }
    }

    while(leftIdx >= 0){
        var leftValue = array[leftIdx];
        var rightValue = array[rightIdx];

        if(leftValue !== matches[rightValue] && rightValue !== matches[leftValue]){
            return false;
        }

        leftIdx--;
        rightIdx++;
    }

    return true;
}

- mche1987 March 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

checkOneEighty :: [Int] -> Bool
checkOneEighty [] = True
checkOneEighty (x:[]) | x == 0 = True
                      | x == 1 = True
                      | x == 6 = True
                      | x == 8 = True
                      | x == 9 = True
                      | otherwise = False
                      
checkOneEighty (x:xs) =  (checkOneEighty . init $ xs) && areReverse x (last xs)
                        where areReverse x y = case x of
                                               0 -> y == 0
                                               1 -> y == 1
                                               6 -> y == 9
                                               8 -> y == 8
                                               9 -> y == 6

- Haskeller March 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Solution {
    std::vector<int> V;
    std::map<int, int> m;
    public:

        Solution(std::vector<int> v) : V(v) {
            m[1] = 1;
            m[6] = 9;
            m[8] = 8;
            m[9] = 9;
            m[0] = 0;
        }
        bool validateRotation() {
            int len = V.size();
            int start;
            int end;
            if(len %2 == 0) {
                end = len/2;
                start = end - 1;
            }else {
                start = end = len / 2;
            }
            while(start >=0 && end < len) {
                if(m.find(V[start]) == m.end() || m.find(V[end]) == m.end()) {
                    return false;
                }
                if(m[V[start]] == V[end] || V[start] == m[V[end]]) {
                    start--;
                    end++;
                    continue;
                } 
                return false;
            }
            return true;
        }
}

- vikalpveer April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scala

object Turned180 extends App {
  println(check180(List(9,6,1,8,1,9,6)))

  def check180(input: List[Int]): Boolean = input.map {
    case 1 => 1
    case 6 => 9
    case 9 => 6
    case 8 => 8
    case 0 => 0
    case _ => return false
  }.reverse == input
}

- jvmakine April 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const matches = {
0: 0,
1: 1,
8: 8,
6: 9,
9: 6
};

function eqaulTo180(str) {
let memo;

for (let i = 0; i < str.length; i++) {
if (!(str[i] in matches)) {
return false;
}

const newIndex = str.length - 1 - i;

memo = str[newIndex];

if (str[i] !== matches[memo]) {
return false;
}
}

return true;
}

- Artem Oborevich April 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean check180(String[] input){
		
		String[] inputInverse = new String[input.length];
		
		Map<String, String> equivalents = new HashMap<String, String>();
		equivalents.put("0", "0");
		equivalents.put("1", "1");
		equivalents.put("6", "9");
		equivalents.put("8", "8");
		equivalents.put("9", "6");
		
		
		int y = 0;
		for(int x = input.length - 1; x >= 0; x-- ){
			if(equivalents.containsKey(input[x])){
				inputInverse[y] = equivalents.get(input[x]);
			}else{
				System.out.println("No existe" + input[x]);
				inputInverse[y] = "";
			}
			y++;
		}
		
		boolean isEqual = false;
		if(Arrays.equals(input, inputInverse)){
			isEqual = true;
		}
		
		return isEqual;
		
	}

- Sergio Flores May 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean check180(String[] input){
		
		String[] inputInverse = new String[input.length];
		
		Map<String, String> equivalents = new HashMap<String, String>();
		equivalents.put("0", "0");
		equivalents.put("1", "1");
		equivalents.put("6", "9");
		equivalents.put("8", "8");
		equivalents.put("9", "6");
		
		
		int y = 0;
		for(int x = input.length - 1; x >= 0; x-- ){
			if(equivalents.containsKey(input[x])){
				inputInverse[y] = equivalents.get(input[x]);
			}else{
				
				inputInverse[y] = "";
			}
			y++;
		}
		
		boolean isEqual = false;
		if(Arrays.equals(input, inputInverse)){
			isEqual = true;
		}
		
		return isEqual;
		
	}

- saul.floresfo May 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def check180(astring):
    match={0:0,1:1,6:9,8:8,9:6}  
    gnirtsa = astring[::-1]
    count = 0
    
    for i in range(len(astring)):
        try:
            if match[gnirtsa[i]] == astring[i]:
                count+=1
        except:
            pass
            
    if count == len(astring):
        return "true"
    else:
        return "false"

- bugmeister June 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def check180(astring):

match={0:0,1:1,6:9,8:8,9:6}

gnirtsa = astring[::-1]

count = 0

for i in range(len(astring)):

try:

if match[gnirtsa[i]] == astring[i]:

count+=1

except:

pass

if count == len(astring):

return "true"

else:

return "false"

- bugmeister June 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function c(arr) {
const matches = {
0: 0,
1: 1,
8: 8,
6: 9,
9: 6
};
const med = arr.length/2;
let l = 0;
let r = arr.length - 1;
let res = false;

while(l <= med) {
const lc = arr[l++];
const rc = arr[r--];

res = matches[lc] === rc &&
matches[rc] === lc;

if (!res) { break; }
}

return res;
}

- Alberto August 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function c(arr) {
	const matches = {
        0: 0,
        1: 1,
        8: 8,
        6: 9,
        9: 6
    };
	const med = arr.length/2;
	let l = 0;
	let r = arr.length - 1;
	let res = false;

	while(l <= med) {
		const lc = arr[l++];
		const rc = arr[r--];

		res = matches[lc] === rc &&
			matches[rc] === lc;
		
		if (!res) { break; }
    }

	return res;
}

- Alberto August 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool canRotate180(int* arr, int size, const std::unordered_map<int, int>& matches) {
    int l = 0, r = size-1;
    while( l < r) {
        auto it = matches.find(arr[l]);
        if( it == matches.end()) {
            return false;
        } else if (it->second != arr[r]) {
            return false;
        } else {
            l++;
            r--;
        }
    }
    return true;
}

int main() {
    
    std::unordered_map<int, int> matches({
        {0, 0},
        {1, 1},
        {8, 8},
        {6, 9},
        {9, 6}
    });
    
    int arr1[] = {1,2,4,7,1};
    int size = (sizeof arr1/sizeof arr1[0]);
    std::cout << canRotate180(arr1, size, matches) << std::endl;
    
    int arr2[] = {1,6,8,9,1};
     size = (sizeof arr2/sizeof arr2[0]);
    std::cout << canRotate180(arr2, size, matches) << std::endl;
    
    return 0;
}

- Ak13 October 23, 2018 | 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