Interview Question

Anonymous Interview(Student Candidate) Menlo Park, CA

check palindrome

, print subsets, multiply numbers in string.
Answer

Interview Answer

1 Answer

0

palindrome:

#include <string>
#include <cassert>

bool checkPalyndrome(const std::string& word) {
  if (word.size() <= 1) {
    return true;
  }
  if (word[0] != word[word.size() - 1]) {
    return false;
  }
  return checkPalyndrome(word.substr(1, word.size() - 2));
}

int main() {
  assert(checkPalyndrome(""));
  assert(checkPalyndrome("aba"));
  assert(!checkPalyndrome("abc"));
  assert(!checkPalyndrome("ab"));
  assert(checkPalyndrome("aa"));
}

Anonymous on Dec 12, 2013

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up