Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

you can do this using suffix Tree and Suffix arrays. O(nlgn) solution

1. For constant sized alphabet we can build suffix trees using Ukkonen's Algorithm in O(n).
2. Traverse the tree lexicographically and populate the suffix array.
3. Pass over suffix array once to get total number of palindromic substrings in the input string.

More Specifically:

This can be done in linear time using suffix trees:
1) For constant sized alphabet we can build suffix trees using Ukkonen's Algorithm in O(n).
2) For given string S, build a generalized suffix tree of S#S` where S' is reverse of string S and # is delimiting character.
3) Now in this suffix tree, for every suffix i in S, look for lowest common ancestor of (2n-i+1) suffix is S`.
4) count for all such suffixes in the tree to get total count of all palindromes.
5) You can even go further and get longest palindrome. Look for LCA which is deepest in the tree i.e. one whose distance from root is max.

- zahidbuet106 December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@zahidbuet106

Can you please explain step 3 in more detail?
How can the suffixes alone suffice? What do you do when you pass over them?

- visitor December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

1) For constant sized alphabet we can build suffix trees using Ukkonen's Algorithm in O(n).
2) For given string S, build a generalized suffix tree of S#S` where S' is reverse of string S and # is delimiting character.
3) Now in this suffix tree, for every suffix i in S, look for lowest common ancestor of (2n-i+1) suffix is S`.
4) count for all such suffixes in the tree to get total count of all palindromes.
5) You can even go further and get longest palindrome. Look for LCA which is deepest in the tree i.e. one whose distance from root is max.

I hope this helps. I am updating the main post.

- zahidbuet106 December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Special Note: Use Manacher's algorithm to find all the palindromic substring or the longest palindromic substring very simply in O(n) time!

public class ManacherLongestPalindromicSubstring{

	public static String void preprocess(String in){
		StringBuffer sb = new StringBuffer();
		sb.append’#’);
		for(int i=0; i<in.length(); i++){
			sb.append(in.charAt(i));
			sb.append(‘#’);
		}
		return sb.toString();
	}

	public static String LongestPalindrome(String in){
		/*Preprocess the string to insert special character  ‘#’ in the spaced between characters in input and the two outside edges. This is done merely to make the computation identical for both odd and even length input string. */

		String S = preprocess(in);
		int C = 0; //contains center of current palindrome
		int R = 0; //contains the right edge of current palindrome
		int[] P = new int[S.length()];

		// P[i] contains the length of longest palindrome (in S not original in) centered at i
		for(int i=0;i<P.length(); i++) 
			P[i] = 0;
		// for each position find longest palindrome centered at the position, save length in P
		for(int i=0; i<S.length; i++){
			int i_mirror = 2*C-i; // as C - i_mirror = i - C due to symmetric property 
		
			/*When R-i > P[i_mirror] then palindrome at center i_prime contained completely within palindrome centered at C.   Else R-i <= P[i_mirror] means that the palindrome at ceter i_mirror  doesn’t fully contained in the palindrome centered at C. Then palindrome at i is extendable past R*/

			P[i] = (R>i) ? min(P[i_mirror], R-i) : 0;

			// if palindrome centered at i is extendable past R then extend R to the right edge of newly extended palindrome
			while(S[i+P[i]+1] == S[i-P[i]-1])
				P[i]++;
			// If palindrome centered at i extend past R then update Center to i
			if(i+P[i] > R){
				C = i;
				R = i+P[i];
			}
		}
		return extractLongest(in, P);
	}

	public int extractLongest(String in, int[] P){
		int longestCenter = 0;
		int longestLength = 0;

		for(int i=0; i<P.length; i++){
			if(P[i] > longestLength){
				longestLongest = P[i];
				longestCenter = i;
			}
		}

		return in.substring((longestCenter-longestLength-1)/2, longestLemgth);
	}

	public Set<String> allPalindromicSubstring(String in, int[] P){
		HashSet<String> all = new HashSet<String>();

		for(int i=0; i<P.length;  i++)
			if(P[i] != 0)
				all.add(in.substring((i-P[i]-1)/2, P[i]));

		return all;
	}
}

- zahidbuet106 December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

without help of suffix array or a trie, i can think of a solution with O(n*n),where n is string len.

1) for palindromic str len %2 == 1, we can iterate through each index i of str, and check how many substring is palindromic if str[i] is the centre of the substr. the check process is as follow: if str[i-1]==str[i+1],then str[i-1,i,i+1] is a palindromic substr, we can continue to check if str[i-2]==str[i+2], if it is not, quit this iterator.
2) for palindromic str len%2 == 0, the process is similar except that str[i-1]=str[i],then check how many substr is palindromic.

- busycai December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@zahidbuet106

can you please explain step 3 in more detail?
How can the suffixes alone suffice? What do you do when you pass over them?

- visitor December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

public class UniqueSubstringsInAStringThatArePalindromes {

    public static void main(
            String[] args) {
        String str = args[0];
        Set<String> set = new HashSet<String>();
        for (int i = 0; i < str.length(); i++)
            for (int j = i+1; j <= str.length(); j++)
                if (isPalindrome(str.substring(i, j)))
                    set.add(str.substring(i, j));
        System.out.println(set);
    }

    public static boolean isPalindrome(
            String str) {
        if (str.length() > 0) {
            for (int i = 0, j = str.length() - 1; i <= j; i++, j--) {
                if (str.charAt(i) == str.charAt(j))
                    continue;
                else
                    return false;
            }
            return true;
        } else
            return false;
    }

}

- nirupam.astro December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. construct two suffix trees, one for original string and the other for reversed string. O(n)
2. for each letter in the string, get LCA of both trees. if string length is odd, get LCA(a[i], a' [n-i+1]), if even, LCA(a[i], a' [n-i+1]) and LCA(a[i+1], a' [n-i+1]). O(n)

- Anonymous December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Here is reasonable approach without suffix array and trie though.

stevekrenzel.com/articles/longest-palnidrome

You have to modify algorithm slightly to count all intermediate palindromes.

- thelineofcode December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code can generate only the longest contiguous substring..But I think that is not what is required.

- Sushrut Rathi December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

The question clearly states substring, not subsequence. As far as substrings are concerned this algo is correct. We have to count all of them.

- thelineofcode December 16, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More