Microsoft Interview Question


Country: United States




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

isn't the idea same as palindrome but instead of checking the same characters you will have to check the below map
[6, 9]
[9, 6]
If the number encountered is 6 then at the other end it should have 9 and vice versa.

- aka November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
    long q, r, y=0,c=0;
    q=x;
    while(q>0){
        r=q%10;
        q=q/10;
        if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
        return 0;
        if(r==6)r=9;
        else if(r==9)r=6;
        y=r+y*(10);
    }
    printf("y is set to: '%lld'\n",y);
    return y;
}


int main()
{
    long n=99166;
    long r=rotate180(n);
    if(r!=0 && r==n)
    printf("They are equal after 180 degree rotation\n");
    else
    printf("Failed");
    return 0;
}

- pbodke November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Constraints and Method
1.Keep a map of all possible numbers that rotated and turn into another number like 1-1,0-0,6-9,9-6 and 8-8
and check the map for presence with each digit of the number
2.ith index and (n-i)th must be same

- Krishna November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int map[100] = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
int foo(char *s, int size)
{
    int i=0, ret = 1, found = 0;

    /* if size is odd */
    if (size & 1) {
        char c[2];
        c[0] = s[size/2];
        c[1] = '\0';
        if (map[atoi(&c)] == -1)
            return -1;
    }

    for (i=0;i<size/2;i++) {
        char c[2];
        char d[2];
        d[0] = s[size-1-i];
        c[0] = s[i];
        c[1] = d[1] = '\0';
        if ((map[atoi(c)] == -1 || map[atoi(d)] == -1)) {
            ret = 0;
            break;
        }

        if (atoi(c) == atoi(d) || (map[atoi(c)] == atoi(d))) {
            continue;
        }
        ret = 0;
        break;
    }
    return ret;
}

int main(void) {
    char *x = "9165910199916";
    printf("%s\n", foo(x, strlen(x))?"mirror":"not mirror");
    return 0;
}

- aka November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <stdio.h>

long rotate180(long x){
long q, r, y=0,c=0;
q=x;
while(q>0){
r=q%10;
q=q/10;
if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
return 0;
if(r==6)r=9;
else if(r==9)r=6;
y=r+y*(10);
}
printf("y is set to: '%lld'\n",y);
return y;
}


int main()
{
long n=99166;
long r=rotate180(n);
if(r!=0 && r==n)
printf("They are equal after 180 degree rotation\n");
else
printf("Failed");
return 0;
}

- pbodke November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

test

- pbodke November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

test

- pbodke November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
    long q, r, y=0,c=0;
    q=x;
    while(q>0){
        r=q%10;
        q=q/10;
        if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
        return 0;
        if(r==6)r=9;
        else if(r==9)r=6;
        y=r+y*(10);
    }
    printf("y is set to: '%lld'\n",y);
    return y;
}


int main()
{
    long n=99166;
    long r=rotate180(n);
    if(r!=0 && r==n)
    printf("They are equal after 180 degree rotation\n");
    else
    printf("Failed");
    return 0;
}

- Anonymous November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
    long q, r, y=0,c=0;
    q=x;
    while(q>0){
        r=q%10;
        q=q/10;
        if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
        return 0;
        if(r==6)r=9;
        else if(r==9)r=6;
        y=r+y*(10);
    }
    printf("y is set to: '%lld'\n",y);
    return y;
}


int main()
{
    long n=99166;
    long r=rotate180(n);
    if(r!=0 && r==n)
    printf("They are equal after 180 degree rotation\n");
    else
    printf("Failed");
    return 0;
}

- pbodke November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
long q, r, y=0,c=0;
q=x;
while(q>0){
r=q%10;
q=q/10;
if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
return 0;
if(r==6)r=9;
else if(r==9)r=6;
y=r+y*(10);
}
printf("y is set to: '%lld'\n",y);
return y;
}


int main()
{
long n=99166;
long r=rotate180(n);
if(r!=0 && r==n)
printf("They are equal after 180 degree rotation\n");
else
printf("Failed");
return 0;
}

- pbodke November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
long q, r, y=0,c=0;
q=x;
while(q>0){
r=q%10;
q=q/10;
if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
return 0;
if(r==6)r=9;
else if(r==9)r=6;
y=r+y*(10);
}
printf("y is set to: '%lld'\n",y);
return y;
}


int main()
{
long n=99166;
long r=rotate180(n);
if(r!=0 && r==n)
printf("They are equal after 180 degree rotation\n");
else
printf("Failed");
return 0;
}

- pbodke November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

long rotate180(long x){
long q, r, y=0,c=0;
q=x;
while(q>0){
r=q%10;
q=q/10;
if(r!=8 && r!=0 && r!=1 && r!=6 && r!=9)
return 0;
if(r==6)r=9;
else if(r==9)r=6;
y=r+y*(10);
}
printf("y is set to: '%lld'\n",y);
return y;
}


int main()
{
long n=99166;
long r=rotate180(n);
if(r!=0 && r==n)
printf("They are equal after 180 degree rotation\n");
else
printf("Failed");
return 0;
}

- Anonymous November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

def sameAfterRot(n):
digitList=list(str(n))
rotatedDigitList=[]
for d in digitList:
if int(d) not in digitMap:
return False
rotatedDigitList.append(str(digitMap[int(d)]))
rotatedDigitList.reverse()

return rotatedDigitList==digitList

- dr dull November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

def sameAfterRot(n):
   digitList=list(str(n))
   rotatedDigitList=[]
   for d in digitList:
      if int(d) not in digitMap:
         return False
      rotatedDigitList.append(str(digitMap[int(d)]))
   rotatedDigitList.reverse()

   return rotatedDigitList==digitList

- dr dull November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why did you put 1, 8, 0 in your map?

- Mabooka November 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++, implementation

bool isRotatable(int n) {
    vector<int> arr;
    bool rotatables[10] = {1, 1, 0, 0, 0, 0, 1, 0, 1, 1};
    bool selfRotatables[10] = {1, 1, 0, 0, 0, 0, 0, 0, 1, 0};
    int rotateNumbers[10] = {0, 1, 0, 0, 0, 0, 9, 0, 8, 6};
    int i, j;
    
    if (n == 0) return true;
    
    while (n) {
        i = n % 10;
        n /= 10;
        if (rotatables[i] == false) return false;
        arr.push_back(i);
    }
    
    i = 0;
    j = (int)arr.size() - 1;
    while (i < j) {
        if (rotateNumbers[ arr[i] ] != arr[j]) return false;
        i++;
        j--;
    }
    if (i == j) return selfRotatables[ arr[i] ];
    
    return true;
}

- kyduke November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static boolean isItHas180(String input) {
		boolean result = false;
		if (input != null) {
			int stringLength = input.length();
			if (stringLength > 0 && input.matches("[01689]+")) {
				char[] inputArray = input.toCharArray();
				result = stringLength == 1 ? true : false;
				for (int index = 0; index < stringLength && stringLength > 1; index++) {					
					if(index > stringLength / 2){
						break;
					}
					if (String.valueOf(Character.getNumericValue(inputArray[index])
											+ Character.getNumericValue(inputArray[stringLength-1-index]))
												.matches("(0|2|15|16)")) {
						result = true;
					} else {
						result = false;
					}
				}
			}
		}

		return result;
	}

- saravanancomsc November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

build up the reverse number as follows:
-replace 0 with 0
-replace 1 with 1
-replace 6 with 9
-replace 8 with 8
-replace 9 with 6
For all other digits, return FALSE

At the end if the reverse number is equal to the original number return true; otherwise return false

here's the code:

bool IsReversible( int n )
{
int reverse = 0;

int n_copy = n;

while ( n > 0 )
{
reverse *= 10;

switch ( n%10 )
{
case 0:
reverse += 0;
break;

case 1:
reverse += 1;
break;

case 6:
reverse += 9;
break;

case 8:
reverse += 8;
break;

case 9:
reverse += 6;
break;

default:
return false;

}

n /= 10;
}

return ( n_copy == reverse );
}

Here's the sample output:

69 is reversible
916 is reversible
123 is not reversible
6009 is reversible
6119 is reversible
8698 is reversible
8668 is not reversible
66899 is reversible

- koosha.nejad November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here's the code with indentations

bool IsReversible( int n )
{
   int reverse = 0;

   int n_copy = n;

   while ( n > 0 )
   {
      reverse *= 10;

      switch ( n%10 )
      {
      case 0:
         reverse += 0;
         break;

      case 1:
         reverse += 1;
         break;

      case 6:
         reverse += 9;
         break;

      case 8:
         reverse += 8;
         break;

      case 9:
         reverse += 6;
         break;

      default:
         return false;

      }

      n /= 10;
   }

   return ( n_copy == reverse );
}

- koosha.nejad November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(p());
}

public static boolean p() {
HashMap<Character, Character> h = new HashMap<Character,Character>();
h.put('0', '0');
h.put('1', '1');
h.put('5', '5');
h.put('8', '8');
h.put('6', '9');
h.put('9', '6');

String ip = "1691";
int len = ip.length();

for(int i=0; i < len; i++ ) {
if (i>(len/2)+1)
break;

char x,y;
Character z;
x = ip.charAt(i);
if (len == 1)
y = ip.charAt(0);
else
y = ip.charAt(len-1-i);
z = h.get(x);

if (z!=null && y == z)
continue;
else
return false;

}
return true;
}
}

- Anonymous November 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(p());
}

public static boolean p() {
HashMap<Character, Character> h = new HashMap<Character,Character>();
h.put('0', '0');
h.put('1', '1');
h.put('5', '5');
h.put('8', '8');
h.put('6', '9');
h.put('9', '6');

String ip = "1691";
int len = ip.length();

for(int i=0; i < len; i++ ) {
if (i>(len/2)+1)
break;

char x,y;
Character z;
x = ip.charAt(i);
if (len == 1)
y = ip.charAt(0);
else
y = ip.charAt(len-1-i);
z = h.get(x);

if (z!=null && y == z)
continue;
else
return false;

}
return true;
}
}

- Somisetty November 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RotateUtility {
	private Map<Character, Character> map;
	private String str;

	RotateUtility(int num) {
		map = new HashMap<Character, Character>();
		map.put('0', '0');
		map.put('1', '1');
		map.put('6', '9');
		map.put('8', '8');
		map.put('9', '6');
	}
	
	public boolean isSameAfter180Rotation() {
		int start = 0;
		int end = str.length() - 1;

		while (start <= end) {
			char c1 = str.charAt(start);
			char c2 = str.charAt(end);

			if (map.contains(c1)) {
				if (c1 != map.get(c2)) {
					return false;
				} else {
					start += 1;
					end -= 1;
				}
			} else {
				return false;
			}
		}

		return true;
	}
}

- Vishnu Valleru November 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

int rnum(int n) {
	int res = 0;
	while(n) {
		res *= 10;
		res += n%10;
		n /= 10;
	}
	return res;
}

int derot(int n) {
	int r = rnum(n);
	while(r) {
		int r1 = r%10, n1 = n%10;
		if(r1 != n1 && r1 != 6 && r1 != 9) return 0;
		r/=10;
		n/=10;
	}
	return 1;
}

int main() {
	int n = 6669;
	printf("reversed num = %d\n", rnum(n));
	derot(n) ? printf("True\n") : printf("False\n");
	return 0;
}

- chiranjeevi K November 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>

int rnum(int n) {
	int res = 0;
	while(n) {
		res *= 10;
		res += n%10;
		n /= 10;
	}
	return res;
}

int derot(int n) {
	int r = rnum(n);
	while(r) {
		int r1 = r%10, n1 = n%10;
		if(r1 != n1 && r1 != 6 && r1 != 9) return 0;
		r/=10;
		n/=10;
	}
	return 1;
}

int main() {
	int n = 6669;
	printf("reversed num = %d\n", rnum(n));
	derot(n) ? printf("True\n") : printf("False\n");
	return 0;
}

- chiranjeevi K November 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace reverse180
{
    class Program
    {
        static void Main(string[] args)
        {
            // File contains any number. For example: 916 
            string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { '\r'}, StringSplitOptions.RemoveEmptyEntries);
            // Checking whether a number is strange :)
            var is_strange_number = Turn180(int.Parse(input_data[0]));
            Console.WriteLine(is_strange_number);
            Console.ReadKey();
        }
        static bool Turn180(int strange_number)
        {
            int x=0; // it is result of replace and reverse
            int x0 = strange_number; // store input number
            while (strange_number > 0)  // in each step strange_number is decremented in 10 times
            {
                x = x * 10;             
                var temp = strange_number % 10; // Get the youngest position of number
                if (temp != 0)
                {
                    if (temp == 6) temp = 9;    // reverse
                    else if (temp == 9) temp = 6; // reverse
                    else if (temp != 1 && temp != 8 && temp != 0) return false; // if number contains un-reverseable item, breake method.
                    x =x + temp;            
                    strange_number = strange_number / 10;   
                }
                else {
                    strange_number = strange_number / 10;
                }
            }
            if (x != x0) return false;
            return true;
        }
    }
}

- Trushnikov.Ivan December 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a dictionary of rotate values of each integer.

public static void CheckRotationNumber(int myInt)
        {
            bool flag=true;
            Dictionary<int, int> rotateDict = new Dictionary<int, int>();
            rotateDict.Add(1, 1); rotateDict.Add(2, 2); rotateDict.Add(3, 3); rotateDict.Add(4, 4);
            rotateDict.Add(5, 5); rotateDict.Add(6, 9); rotateDict.Add(7, 7); rotateDict.Add(8, 8); rotateDict.Add(9, 6); rotateDict.Add(0, 0);

            char[] myChar = myInt.ToString().ToArray();
            int length = myChar.Length-1;

            for (int i = 0; i <= length/2; i++)
            {
                if(rotateDict[myChar[i] - '0'] != myChar[length-i] - '0')
                {
                 flag=false;
                    break;
                }
            }
           Console.Write(flag.ToString());
        }

- Neelavan April 06, 2017 | 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