Facebook

www.facebook.com
Employer Engaged

Interview Question

Software Engineer Interview(Student Candidate)

Check if two strings are Palindrome

Answer

Interview Answer

3 Answers

0

non-recursive:
bool pali(string a, string b){
 if(a.size() != b.size())
     return false;

 for (int i = 0; i<a.size();i++){
     if(a.charAt(i) != b.charAt(a.size() - i){
         return false;
    }
}
return true;
}

recursive

bool is_pali(string a, string b){
      if (a.size() != b.size() or a.size() == 0 or a.charAt(0) != b.charAt(b.size()-1)){
          return false;
     }
      is_pali(a.substring(1),b.substring(0,b.size - 1);
}

Adi on Feb 12, 2013
0

int palindrome(string a)
{
  assert(a.length());//Abort is string empty

  int val = 1;//default value is true
  for(int i = 0; i < a.length(); i++)//Can actually restrict i:0-length/2
        val *= (a[i]==a[a.length()-1-i]);
  return val;
}

S on Feb 17, 2013
0

Python implimentation

string==string[::-1]

Of course your interviewer will ask you to elaborate.

Dan on Aug 3, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.