I recently came across Gary Daniels and Evan Goldring - Mock whiteboard posting on Channel9 and I thought to give it a shot. In short, Microsoft created a reputation of asking job candidates to write a mock up solution to a predefined problems during the interview process. The posting makes mention of one past question that asked a candidate to write a code snippet to determine a palindrome string. A palindrome phrase can get complex, and can allow for adjustment of punctuations and spaces between words (Wikipedia).
From a business point of view, I have found two valuable outcomes from this approach. For one, it can act as an ice breaker allowing for a good discussion between the interviewer and a competent candidate. Second, you are testing communication, logic, analytical thinking, information gathering, and problem solving skills rather than code and syntax memorization. The answer to such problem does not have to be in code, it can simply be in pseudo code. The goal is to adequately assess a potential prospect.
The following code is my attempt at the problem. Download the code
using System;
using System.Collections.Generic;
using System.Text;
namespace PalindromeConsole
{
class Program
{
static void Main(string[] args)
{
string sLine;
// Wait for response to exit
do
{
Console.WriteLine("Press return to exit or enter a Palindrome word (case ignored). ");
if ((sLine = Console.ReadLine()) == string.Empty)
break;
try
{
string str1stHalf = string.Empty, str2ndHalf = string.Empty;
// Check Palindrome
bool isPalindrome = IsPalindrome(sLine, out str1stHalf, out str2ndHalf);
if (sLine.Length <= 1)
Console.Out.WriteLine("IsPalindrome result: {0} is not Palindrome.", str1stHalf);
else
Console.Out.WriteLine("IsPalindrome result: {0} {1} {2}.", str1stHalf, (isPalindrome ? "=" : "!="), str2ndHalf);
// Check Palindrome using method 2
isPalindrome = IsPalindrome2(sLine, out str1stHalf, out str2ndHalf);
if (sLine.Length <= 1)
Console.Out.WriteLine("IsPalindrome2 result: {0} is not Palindrome.", str1stHalf);
else
Console.Out.WriteLine("IsPalindrome2 result: {0} {1} {2}.", str1stHalf, (isPalindrome ? "=" : "!="), str2ndHalf);
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
}
finally
{
Console.Out.WriteLine();
}
} while (1 == 1);
}
private static bool IsPalindrome(string value, out string firstHalf, out string secondHalf)
{
if (value.Length <= 1)
throw new Exception(string.Format("Value given {0} is not a Palindrome.", value));
int iMid = (int)(value.Length / 2);
int iOdd = (value.Length % 2 == 0) ? 0 : 1;
bool isValuePalindrome = true;
// Set out two halves
firstHalf = value.Substring(0, iMid + iOdd);
secondHalf = value.Substring(iMid);
// Compare character by character
for (int item = 0; item <= iMid; item++)
if (value[item] != value[value.Length - (item + 1)])
{
isValuePalindrome = false;
break;
}
return isValuePalindrome;
}
private static bool IsPalindrome2(string value, out string firstHalf, out string secondHalf)
{
if (value.Length <= 1)
throw new Exception(string.Format("Value given {0} is not a Palindrome.", value));
int iMid = (int)(value.Length / 2);
int iOdd = (value.Length % 2 == 0) ? 0 : 1;
// Set out two halves
firstHalf = value.Substring(0, iMid + iOdd);
secondHalf = value.Substring(iMid);
// Use a collection and take advantage of the Array.Reverse method.
char[] aList = secondHalf.ToCharArray();
Array.Reverse(aList);
string secondHalfReverse = new string(aList);
// Compare two halves
return (firstHalf.Equals(secondHalfReverse, StringComparison.CurrentCultureIgnoreCase));
}
}
}
Currently rated 5.0 by 2 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5