Palindrome Puzzle interview Question

 Palindrome Puzzle interview Question

Palindromes

Problem: this year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards).
10/02/2001
when was the last date that this occurred on? (see if you can do it in your head!)

Solution

Solution: we know the year has to be less than 2001 since we already have the palindrome for 10/02. it can’t be any year in 1900 because that would result in a day of 91. same for 1800 down to 1400. it could be a year in 1300 because that would be the 31st day. so whats the latest year in 1300 that would make a month? at first i thought it would be 1321, since that would give us the 12th month, but we have to remember that we want the maximum year in the 1300 century with a valid month, which would actually be 1390, since 09/31 is a valid date.
but of course, a question like this wouldn’t be complete without an aha factor. and of course, there are not 31 days in september, only 30. so we have to go back to august 08 which means the correct date would be 08/31/1380.
palindromes also offer another great string question.
write a function that tests for palindromes
bool isPalindrome( char* pStr )
if you start a pointer at the beginning and the end of the string and keep comparing characters while moving the pointers closer together, you can test if the string is the same forwards and backwards. notice that the pointers only have to travel to the middle, not all the way to the other end (to reduce redundancy).
bool isPalindrome( char* pStr )
{
  if ( pStr == NULL )
   return false;
 
  char* pEnd = pStr;
  while ( *pEnd != '\0' )
    pEnd++;
 
  pEnd--;
 
  while(pEnd > pStr)
  {
    if ( *pEnd != *pStr )
      return false;
 
    pEnd--;
    pStr++;
  }
 
  return true;
}
thanks to tom for sending me this one! congrats on the wedding…

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community
 
Top Blogs

Best Links to Visit

Flipkartcouponsoffers Cinterviews YOUTUBESHORTFILMS YADAGIRIGUTTA WOMENSPAGE VODAFONE TSRTCBUS TRAININGINSTITUTES TRAINEDNAUKRI TIRUMALATIRUPATIDEVASTHANAMS TIRUMALATIRUPATIDEVASTHANAM TELANGANASTATEPUBLICSERVICECOMMISSION SELFIESTICKONLINE SBIONLINE.INFO SACHINAUTOBIOGRAPHY RELIANCENETCONNECTCO REDBUSIN RED-BUS RCOMCO PINCODEOF PFRDAORG PAYELECTRICITYBILLONLINE OLXIN NSDLCO NHTIS MONSTERINDIA MOBILITYLABS MOBILITYLABS MOBILITILABS MEESEVAGOV MANABADICO LICINDIAIN JURASSICWORLD JOBINIT JNTUKEDU JAVASCJP IRCTCAVAILABILITY IPHONEINTERVIEWQUESTIONS INTERVIEWHOME HDFCBANKPERSONALLOANS HDFCBANKLOANS GREATTELANGAANA GMAIL CREATE GHMCGOV FLIPKARTCOUPONSOFFERS FIREFOXOPERATINGSYSTEM FIBER-GOOGLE FACEBOOKTIPS FACEBOOKSIGNUP ESEVAONLINE EPAPERAP EAMCET2016 EAMCET2015 CUSTOMER-CARE-NUMBER COUPONSCOUPONS CINTERVIEWS BUYMOBILEONLINE BTECHGIRLS BTECHFRIENDS BOOKKART APSRTCONLINEIN APSRTCONLINEE APSRTCONLINE APSRTCGOV APSPSCGOV APEAMCETNIC ANDROIDTUTORIAL ANDROIDINTERVIEWQUESTIONS AMAZON-IN AADHAARCARDS