Facebook Interview Question
Software Engineer / Developersis 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;
}
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
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;
}
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));
}
}
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'))
Original Solution is correct. It should not work for 66 or 99. Because they are not mirror numbers. 69 and 96 are mirrors.
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
<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>
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;
}
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;
}
- Ron Park March 09, 2009