Facebook Interview Question
SDE1sCountry: United States
Interview Type: Phone Interview
// 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;
}
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
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;
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
- Anonymous April 01, 2018