Microsoft Palindrome Interview Question

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

Related posts

Comments

August 12. 2008 20:09

Gravatar

Wael, I like your code and for those who are allergic to too many comparisons in a loop, here’s another version of IsPalindrome() that compares only once:



private static bool IsPalindrome(string value, out string firstHalf, out string secondHalf)
……
……
……
string valueReverse= "";
for(int i = value.Length -1; i >= 0; i--)
valueReverse += str[i];
if(valueReverse.ToLower() != value.ToLower())
isValuePalindrome = false;
……
……

I thought of using Regex but it’s impossible to construct a single regular expression that would work with strings of arbitrary length.

Abe

July 4. 2009 03:40

Gravatar

Congratulation! you already solve this Microsoft Palindrome Interview Question

bolinaoph

November 3. 2009 10:13

Gravatar

Yea nice Work !:D

faxless payday loans

November 8. 2009 02:10

Gravatar

Do you have any more info on this?

payday loans

November 14. 2009 00:39

Gravatar

I reallyappreciate your straight forward approach and interesting thoughts.

buying generic cialis

November 15. 2009 00:37

Gravatar

I like thedesign of the blog, and thoughts are just great.

canadian pharmacy online

November 23. 2009 15:42

Gravatar

Yea nice Work !:D

cash loans

November 28. 2009 07:45

Gravatar

Nice info for Microsoft Palindrome Interview Question.

Bedding Duvet Covers

December 3. 2009 16:04

Gravatar

Do you have any more info on this?

payday loans

December 4. 2009 13:51

Gravatar

Interesting ... as always - is your blog making any cash advance ? ;)

payday loans

December 5. 2009 02:51

Gravatar

Interesting ... as always - thanks one more time

cash advance

December 18. 2009 02:55

Gravatar

Do you make money out of this blog? just curious

payday loans

December 20. 2009 11:33

Gravatar

Finally! A relevant, intelligent post regarding a subject that so much logic is missing. Many thanks for sharing this inventive and intelligent commentary with the world. We definitely need lot of sense like you've shown here. I appreciate it very much Smile.

Gadgets Article

December 25. 2009 09:38

Gravatar

Microsoft Palindrome Interview Question. Do you accept guest posts? I would love to write couple articles here..

Phone Pay As You Go

December 28. 2009 09:14

Gravatar

Nice post . keep up the good work

payday loans

December 31. 2009 15:07

Gravatar

Do you make money out of this blog? just curious

faxless payday loans

January 3. 2010 17:26

Gravatar

There is obviously a lot to know about this. I think you made some good points in Features also.

fast payday loans

January 16. 2010 15:23

Gravatar

Success usually comes to those who are too busy to be looking for it.

no fax loans

January 21. 2010 12:14

Gravatar

guau! los tenia a la mayoria en mis marcadores pero marcando este sitio los tengo a todos en uno..muy buena seleccion!

payday loans

January 21. 2010 19:25

Gravatar

Thanks for this - I'll make sure to check out this page more often. You've won my bookmark

payday loans

January 23. 2010 03:03

Gravatar

Nothing is so much fun as business. I do not expect to do anything but work as long as I can stand up.

Loans in AK

January 23. 2010 03:03

Gravatar

The beginning is the most important part of the work.

Loans in CA

January 23. 2010 16:25

Gravatar

Microsoft Palindrome Interview Question. This is actually really interesting regarding your fact article here, This article is very informative..

Bedding Duvet Covers

January 23. 2010 17:50

Gravatar

Microsoft Palindrome Interview Question. Do you accept guest posts? I would love to write couple articles here..

Pay As You Go Phone

January 25. 2010 01:09

Gravatar

Microsoft Palindrome Interview Question. Having read the post, I have bookmarked your blog..

Hair Color Ideas

January 25. 2010 20:28

Gravatar

Honestly I dont quite often to post on website, but this post really force me to do so!

grow 4 inches

January 27. 2010 20:13

Gravatar

Microsoft Palindrome Interview Question. Thank you for the insight, i really appreciate your posts..

Hair Color Pictures

January 31. 2010 04:17

Gravatar

Microsoft Palindrome Interview Question. Do you accept guest posts? I would love to write couple articles here..

Hair Coloring

January 31. 2010 05:45

Gravatar

Microsoft Palindrome Interview Question. Thanks! very helpful post!! like the template btw ;).

Hair Coloring

February 8. 2010 13:25

Gravatar

Hi there, I found your blog via Google while searching for first aid for a heart attack and your post looks very interesting for me.

cash loans

February 9. 2010 01:42

Gravatar

Microsoft Palindrome Interview Question. Having read the post, I have bookmarked your blog..

Hair Color Pictures

February 10. 2010 22:33

Gravatar

Microsoft Palindrome Interview Question. Hmmm interesting stuff.

asvab

February 11. 2010 00:25

Gravatar

Microsoft Palindrome Interview Question. Thank you for your help!.

herpes

February 11. 2010 01:29

Gravatar

Microsoft Palindrome Interview Question. Very interesting post..Iam very much enjoyed by reading your site..this article has provided a useful info..Thanks for the info given...

herpes

February 11. 2010 06:56

Gravatar

Microsoft Palindrome Interview Question. Some interesting information, thank you! I'll refer my blog readers to this site .

herpes photos

February 11. 2010 09:03

Gravatar

Microsoft Palindrome Interview Question. Having read the post, I have bookmarked your blog..

toy dog

February 12. 2010 05:06

Gravatar

Microsoft Palindrome Interview Question. I never thought of that this way .. good writing. .

hair

February 16. 2010 07:56

Gravatar

There are certainly a lot of details like that to take into consideration.

teeth whitening

February 21. 2010 16:05

Gravatar

Please, can you PM me and tell me few more thinks about this, I am really fan of your blog...

Herbal colon cleanse

February 23. 2010 12:47

Gravatar

Keep working ,great job!

Acne scar treatment

February 24. 2010 16:29

Gravatar

This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

payday loans

February 24. 2010 18:57

Gravatar

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

pay day loans

February 26. 2010 09:43

Gravatar

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It?s the old what goes around comes around routine.

cash advance

February 26. 2010 11:39

Gravatar

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up!

personal loans

March 1. 2010 07:03

Gravatar

Microsoft Palindrome Interview Question. .

cats

March 1. 2010 11:22

Gravatar

Microsoft Palindrome Interview Question. Do you have any more info on this?.

flea

March 2. 2010 18:20

Gravatar

I am quite interesting in your topic of writing hope you will elaborate more on it in future posts.

instant cash loan till payday

March 3. 2010 07:09

Gravatar

Hi Webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do! Big thanks for the useful info i found on Microsoft Palindrome Interview Question.

cash payday loan

March 3. 2010 23:08

Gravatar

Wow and wow! Thank you! I always wished to write in my site something like that. Big thanks for the useful info i found on Microsoft Palindrome Interview Question.

instant loan

March 4. 2010 15:37

Gravatar

Microsoft Palindrome Interview Question. Very interesting post I have seen here.Thanks for posting it..

scabies pictures

March 7. 2010 00:35

Gravatar

Microsoft Palindrome Interview Question. Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also....

physical therapy

March 8. 2010 06:18

Gravatar

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info i found on Microsoft Palindrome Interview Question.

payday cash loans

March 8. 2010 13:59

Gravatar

I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I've added you guys to my blogroll.

Stretch marks

March 8. 2010 17:52

Gravatar

I have downloaded the code,thanks for it hope it will work out :)

Carpet Cleaner Perth

March 8. 2010 18:08

Gravatar

Microsoft Palindrome Interview Question. There is obviously a lot to know about this. I think you made some good points in Features also..

weight loss

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

March 10. 2010 04:05

Gravatar