public static bool IsPalindrome(long number) { bool isPalin = true; // inefficient solution - convert number to string string numAsStr = number.ToString(); // more inefficiency - looping through the entire string // could have achieved the same by traversing just half the string for (int i = 0, j = numAsStr.Length - 1; i < numAsStr.Length - 1; i++, j--) { if (numAsStr[i] != numAsStr[j]) { // the corresponding locations do not match isPalin = false; // some people forget to break out of the loop // extremely bad - no need to do any further comparisons break; } } return isPalin; }
The above solution was an in-efficient solution. It works, but does not get you any points. So, now let's see what improvements can be made to the above program.
public static bool IsPalindrome2(long number) { bool isPalin = true; // inefficient solution - convert number to string string numAsStr = number.ToString(); // we can find if a string is palindrome or not by just traversing half the string for (int i = 0, j = numAsStr.Length - 1; i < (numAsStr.Length) / 2; i++, j--) { if (numAsStr[i] != numAsStr[j]) { // the corresponding locations do not match isPalin = false; // some people forget to break out of the loop // extremely bad - no need to do any further comparisons break; } } return isPalin; }
In the above solution, we cut our execution time by almost half. Better, but not good enough. At this point, the interviewer should have a pretty good idea of your skills on reviewing code and making changes to improve it. Let's assume that he still wants you to keep digging.
public static bool IsPalindrome3(long number) { // since the input is a number, one very fast way way would be // to reverse the number and compare to original long revNum = ReverseNumber(number); return (number == revNum); } private static long ReverseNumber(long number) { long retVal = 0; do { // get the last digit by using the modulo (remainder) operator retVal = (retVal * 10) + (number % 10); // drop the last digit from the number number = number / 10; } while (number != 0); return retVal; }
If you run some performance tests with larges number sets on the above solutions, you will find that the 3rd solution is the most performant. I hope this example has shown you one way to attempt to solve a coding question. Remember, it is not just about the answer. The interviewer is trying to find your ability to think, debug and troubleshoot a problem.
Great example!
ReplyDeleteMore examples of programming elegance please :)
class Palindrome
ReplyDelete{
static void Main()
{
string checkStr, sampleStr;
char[] temp;
System.Console.Write("Enter your String: ");
sampleStr = System.Console.ReadLine();
checkStr = sampleStr.Replace(" ", "");
checkStr = checkStr.ToLower();
temp = checkStr.ToCharArray();
System.Array.Reverse(temp);
if(checkStr== new string(temp))
{
System.Console.WriteLine(""{0}" is a palindrome.",sampleStr);
}
else
{
System.Console.WriteLine(""{0}" is NOT a palindrome.",sampleStr);
}
}
}
Great collection of questions in one plate!
ReplyDelete