Amazon Interview Question

Find a longest Palindrome in a given string.

Interview Answers

Anonymous

Apr 2, 2011

Read the letters into a stack. Make a stack copy and pop the top (1 or 2, depends) letter(s) out when there's a potential palindrome.

Anonymous

May 10, 2011

Reverse the string and perform longest common substring algorithm

Anonymous

Sep 14, 2011

Struggling through some of these problems so I can prepare myself. I kind of worked something out. It seems to do it in O(n), but the code turned out so simple that I am skeptical I actually got it. Syntax of some things may not even be right. Anyways, here's my solution. public static String longestP (String mystring) { HashMap myhash = new HashMap(); int temp1, temp2; int i; int palindromeLength = 0; String longestPalindrome; for (i=0;i palindromeLength)) { palindromeLength = substring.length(); longestPalindrome = substring; } } } return longestPalindrome; }