Apple Interview Question: Write a function that determi... | Glassdoor

Interview Question

IOS UI Automation Engineer Interview Cupertino, CA

Write a function that determines which function is a mirror

  number. i.e. 12321 would return YES
Answer

Interview Answer

6 Answers

1

function mirror(num){
  len = num.length - 1;
  for(i=0; i<=len; i++){
  if(num[i] == num[len - i]){
   return YES;
  }else{
   return NO;
 }
}
}

Anonymous on Oct 28, 2013
4

function mirror(number){
        number=number.toString();
        length=number.length-1;
        for(i=0;i

Stanislaus Ju on Jan 15, 2014
0

def mirror(num):
   li=list(num)
   length=len(li)-1
   for i in range(length/2):
     if li[i]!=li[length-i]:
       print 'NO'
       return
   else: print 'YES'

mirror('12321')
mirror('12322')

output -
>>>
YES
NO
>>>

Neeraj Gupta on Apr 16, 2014
1

Using Python:

def mirror(num):
    li = list(str(num))
    length = len(li)-1
    for i in range(length/2):
        if li[i] != li[length-i]:
            print 'NO'
            return
    print 'YES'

mirror(12321)
mirror(12341)

Output
>>>
YES
NO
>>>

Bilal Ahamad on Jul 19, 2014
0

JAVASCRIPT

function mirror(num){
    var a = [];
    var n = num.toString().split("");
    for (var i= n.length - 1; i>=0; i--){
        a.push(n[i]);
    }
    a = a.join("");
    n = n.join("");
    if (n == a){
        return "YES";
    }
    else{
        return "NO";
    }
}

mirror(12321);

Anonymous on Jun 15, 2016
0

1. Convert to string
2. Check string for palindrome

Anonymous on Feb 18, 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.