Facebook Interview Question for Software Engineer / Developers






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

int flip_digit(int d)
{
  switch (d = val%10) {
    /* flippable numbers */
    case 0:
    case 1:
    case 5:
    case 8:
      return d;
    /* unflippable numbers */
    case 2:
    case 3:
    case 4:
    case 7:
       return -1;
    case 6:
       return 9;
    case 9:
       return 6;
    default:
       return -1;
  }
}
int flip (int val, int carry)
{
  int d;
  if (val == 0) return carry;
  d = flip_digit(val%10);
  if (d < 0) return d;
  return flip(val/10, carry*10+d);
}

int isFlipped(int val)
{
  return (val == flip(val, 0)); 
}

- Ron Park March 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

is it optimal code ??

#include<stdio.h>

int possibleValues(int val){
    if(val==1)
        return 1;
    else if(val==6)
        return 9;
    else if(val==8)
        return 8;
    else if(val==9)
        return 6;
    else if(val==0)
        return 0;
    else
        return -1;
}

int check_reversal(int a[],int size){
    int start=0,i;
    int end=size-1;
    for(i=0;i<size;i++){
        if(a[end]==possibleValues(a[start])){
        }
        else{
            return 0;
        }
        start++;
        end--;
    }
    return 1;
}

int main(){
    int a[3]={8,1,8};
    int b[4]={1,6,9,1};
    int c[2]={8,8};
    int d[4]={8,0,1,8};
    int e[2]={2,1,2};
    printf("%d",check_reversal(a,3));
    printf("%d",check_reversal(b,4));
    printf("%d",check_reversal(c,2));
    printf("%d",check_reversal(d,4));
    printf("%d",check_reversal(e,2));
    return 0;
}

- Nishant Pandey July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use

while(start <= end) {
   ...
}

otherwise great!

- Safi December 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I respectfully disagree that the function applied to 212 should return false. A 2 flips into a 2 in the same way a 5 flips into a 5 or a 1 into a 1.

Here's my approach:

CSPFlippingNumber.h:

@interface CSPFlippingNumber : NSObject

/**
 * This method returns YES if its input, when flipped over, results in the same
 * number. Example: flipping over 66 gives 99, which is different. But 69 flipped
 * over is still 69.
 * @param number an NSUInteger to check
 * @return YES if the input flips onto itself
 */
+ (BOOL)isFlippingNumber:(NSUInteger)number;

@end

CSPFlippingNumber.m:

#import "CSPFlippingNumber.h"
#include <math.h>

@implementation CSPFlippingNumber

+ (BOOL)isFlippingNumber:(NSUInteger)number
{
    // Iterarion of O(n/2) (or O(n) linear), where n is the number of digits of
    // the input. Each iteration will check if the most significant digit flips
    // onto the least one. Then removes both from the number before looping
    // again.
    for (int numberLength = (int)log10(number); numberLength >= 0; numberLength-= 2) {

        // Base case, when the number of digits in the input is an odd number.
        // In such case, this case evaluates
        if (numberLength == 0) {
            return (number == 0) || (number == 1) || (number == 2) || (number == 5) || (number == 8);
        } else {
            // Regular case. msd: most significant digit. lsd: least one.
            unsigned msd = number / pow(10, numberLength), lsd = number % 10;

            // If msd flips into lsd...
            if (((msd == 1) && (lsd == 1)) || ((msd == 2) && (lsd == 2)) ||
                ((msd == 5) && (lsd == 5)) || ((msd == 6) && (lsd == 9)) ||
                ((msd == 8) && (lsd == 8)) || ((msd == 9) && (lsd == 6))) {
                // Remove both from the input
                number-= (msd * pow(10, numberLength));
                number/= 10;
            } else {
                // Otherwise, the number isn't flippable
                return NO;
            }
        }
    }

    return YES;
}

@end

Test cases:

#import <XCTest/XCTest.h>
#import "CSPFlippingNumber.h"

@interface CSPFlippingNumberTests : XCTestCase

@end

@implementation CSPFlippingNumberTests

- (void)testTrue
{
    XCTAssert([CSPFlippingNumber isFlippingNumber:818]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:1691]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:88]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:68189]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:0]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:212]);
    XCTAssert([CSPFlippingNumber isFlippingNumber:5]);
}

- (void)testFalse
{
    XCTAssertFalse([CSPFlippingNumber isFlippingNumber:8018]);
    XCTAssertFalse([CSPFlippingNumber isFlippingNumber:215]);
    XCTAssertFalse([CSPFlippingNumber isFlippingNumber:7]);
}

@end

- diegum May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do something like a palindrome check, but instead of checking if chars are equal, check if they are part of a 'flip pair' (like 6.9 or 8,8 or 1,1 etc).

- Anonymous March 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const int decDict[] = {0, 1, 5, 3, -1, 2, 9, -1, 8, 6};

bool chk_rev_same(unsigned int val);

int main(void) {

  unsigned int testVec[5] = {818, 1691, 88, 8018, 212};
  unsigned int i = 0;

  for(i = 0; i < 5; i++) {
    if(chk_rev_same(testVec[i])) {
      printf("%d:true\n", testVec[i]);
    } else {
      printf("%d:false\n", testVec[i]);
    }
  }
  
  return 0;
}

bool chk_rev_same(unsigned int inVal)
{
  unsigned int tmpVal = 0, outVal = 0, remVal = 0;

  for(tmpVal = inVal; tmpVal != 0; tmpVal /= 10) {
    remVal = tmpVal % 10;

    if(decDict[remVal] < 0) return false;

    outVal *= 10;
    outVal += decDict[remVal];
  }

  if(outVal == inVal) return true;
  else return false;
}

- freeaion March 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey can you give some more info about your interview,was this a phone or onsite,and what else did they ask?

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

public class Mirror {
public Mirror() {

}

public boolean isCharMirror(char c1, char c2) {
if(c1 == '0' && c2 == '0')
return true;
if(c1 == '1' && c2 == '1')
return true;
if(c1 == '8' && c2 == '8')
return true;
if((c1 == '6' && c2 == '9') || (c1 == '9' && c2 == '6'))
return true;
return false;
}

public boolean isMirror(int value) {
String str = Integer.toString(value);
for(int i = 0, j = str.length()-1; i < j; i++, j--) {
if(! isCharMirror(str.charAt(i), str.charAt(j)))
return false;
}
return true;
}

public static void main(String[] args) {
Mirror mirror = new Mirror();
System.out.println(mirror.isMirror(818));
System.out.println(mirror.isMirror(1691));
System.out.println(mirror.isMirror(88));
System.out.println(mirror.isMirror(8018));
System.out.println(mirror.isMirror(212));

}
}

- hero October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code would not work for numbers like 99 or 66. You need to modify the if condition as :

if((c1 == '6' && c2 == '9') || (c1 == '9' && c2 == '6') ||
(c1 == '6' && c2 == '6') || (c1 == '9' && c2 == '9'))

- Anonymous March 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Original Solution is correct. It should not work for 66 or 99. Because they are not mirror numbers. 69 and 96 are mirrors.

- Naman March 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good point. However, it fails for single digit numbers like 2 which is not a mirror of itself. It should pass for 0, 1, 8 only. You can change the for loop termination condition to (...; i <= j; ...) so that every character is checked.

- Anonymous November 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should do the job

def ifReverse(number):
	reverseMap = {'0':'0','1':'1','6':'9','8':'8','9':'6'}
        numberStringed = str(number)
	reversedNumber = ''
	for digit in numberStringed:
		if digit not in reverseMap:
			return False
		reversedNumber = reverseMap[digit]+reversedNumber
	if reversedNumber == numberStringed:
		return True
	else:
		return False

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

<pre lang="java" line="1" title="CodeMonkey83347" class="run-this">def isRev(n):
size = len(n)
loop = int(math.ceil(size/2))
dic = {1:1,6:9,8:8,9:6}
for i in range(loop):
if n[i] in dic:
if n[size-1-i]!=dic[n[i]]:
return False
else:
return False
return True

</pre><pre title="CodeMonkey83347" input="yes">
</pre>

- Anonymous December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about reverse the number and comparing with the original

public static boolean palindrome(int n) {
		//reverse n;
		if (n<0) return false;
		int m=n;
		int revn=0;
		while (m>0) {
			revn = revn * 10;
			revn = revn + (m%10);
			m = m/10;
		}
		if (revn==n) return true;
		return false;
	}

- blakdogg March 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This may be simpler, but this code didn't tackle the 6 and 9 when transferring the number. Below is the C++ code:

bool flippedNumber(int num)
{
	int flipped = 0;
	int original = num;
	while(num > 0)
	{
		int tmp = num%10;
		if(tmp == 6)
			tmp = 9;
		else if(tmp == 9)
			tmp = 6;
		
		flipped = flipped*10 + tmp;
		num /= 10;
	}
	if(original == flipped)
		return true;
	else 
		return false;
}

- nriver March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

reverse the string and then flip each char. Eg: 1691 reverse will be 1961. Then flip each char it will be 1691.

- Anonymous January 06, 2013 | 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