Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

I will post the code for this shortly, but here is the explanation in English:

1. You will need a suffix array (you can use a suffix tree, but suffix array takes up less space and is easier to build, especially on a board during the interview).

2. You will need to build two suffix arrays - one for your string: S and one for the reverse of S'. O(n) time

3. Sort the suffixes in the suffix array O(n lg n) time

4. Now run a simple loop comparing the first, second, third.... n elements of your suffix array. Remember, you are not comparing each element to every element. You are just incrementing a single index - 'i' and comparing the element at 'i' in the first suffix array, with that of the second suffix array - O(n) time

5. When you compare, you are actually trying to find the Longest Common Prefix. Suffix arrays naturally lets you perform this operation - O(k) time, where k is the length of the longest common prefix. Since you run this for every compare (in Step 4), total time is O(nk)

6. If the length of the LCP at each compare is greater than 2, then you have a palindrome :)
{{

8. Go back to Step 4 and repeat till you have scanned all elements of both the suffix arrays.

9. Simply output whatever you stored in Step 7 and you have a list of palindromes :)

I am not going to explain how or why suffix arrays/trees are able to do this. I suggest you go through a good suffix array article online (especially once Wikipedia stops protesting against SOPA :P)

I will post the code for this shortly - maybe tomorrow.

- Anonymous January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just to add to this, we wouldn't need a suffix array at all, if the interviewer didn't add the extension to the question.

If its simply checking if a word is a palindrome, you can put two pointers in the middle of the word (special case, if word length is even) and then check characters on left and right side of the pointer. While doing this, increment one pointer till pointer>=word.length and decrement other pointer, till pointer < 0.

If characters are different, word is not a palindrome.

- Anonymous January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brilliant! I think in Step 5, you will have to scan each element with every element, making this O(k^2). But this is an acceptable solution nonetheless. It will find all palindromes!

- Anonymous January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent!

- Rahul February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach fails in below case:
abcdecba

Both the original strings suffix array, and inverted one'ss will have a entry that has a prefix of 'abc'...but this does not mean that they form a palindrome

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

anagram or palimdron? you confused me

- Anonymous January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry it will be palandrome

- MangoPeople January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry it will be palandrome

- MangoPeople January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess it should be palandrome

- Raman January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Use a stack

- Learner January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why there's no one has suggested stack ?

- CaseyYu2 November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"dadana" may be question is not cleared to me but what u say palindrome then dadana string will have 6 palindromes withint pallindromes are
1. a
2. d
3. n
4. ana
5.dad
6.ada
if ia am wrong please tell me what is correct question confussing me.

- geeks January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n^2) simple solution for all palindromes. Take matrix( (len(s)+1)*(len(s1)+1)) arrange s on the first row, reverse s(call it rs) arrange it in the first column. Populate matrix with 0 or 1 accordingly. Count all diagonals whose length is more than 1 ,this gives all possible palindromes.

- Dr.Sai January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If its just about checking for palindromes then the following code works for me:

public class anagram {
  public static void main(String[] args) {
    String s1 = "nitin";
    String s2 = "MALAYALAM";
    String s3 = "abba";
    String s4 = "aa";
    String s5 = "haha";    
    System.out.println("Result for s1 is: " +anagramtest(s1));
    System.out.println("Result for s2 is: " +anagramtest(s2));
    System.out.println("Result for s3 is: " +anagramtest(s3));
    System.out.println("Result for s4 is: " +anagramtest(s4));
    System.out.println("Result for s5 is: " +anagramtest(s5));
  }
  
  public static boolean anagramtest(String s) {
    boolean flag = false;
    char[] c = s.toCharArray();
    if(s.length() == 2) {
    	if(c[0] == c[1]) { flag = true; }
    }
    else if(s.length() > 2) {
    	for (int start = 0, finish = s.length()-1; start < finish; start++,finish--) {
    		if(c[start] == c[finish]) {
    			flag = true;
    		}    		
    	}
    }
    return flag;
  }
}

But i think the question is asking for palindromes within palindromes essentially asking us to count the total palindromes within a given word. Please clarify.

- renegade January 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

From above comments looks like O(n^2) is the best approach. If so wont it be simpler to check each subarray, of the given string, for palindrome.

for(int i=0; i<str.length(); i++)
  for(int j=i; j<str.length(); j++)
    if (isPalindrome(str.substring(i,j)) cout << str.substring(i,j)

- IntwPrep December 05, 2013 | Flag Reply


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