Microsoft Interview Question for Software Engineer / Developers






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

Convolve string with reverse(string), the point at which the area is max under the curve is the longest palindrome.

- Anonymous December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u explain this a little more

- verma.tech December 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

prefix tree

- Anonymous December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pls give the algo..

- Anonymous December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let the given string be S.

create a new string R (R is the reverse of S)

now create a general suffix tree with these two strings S# and R$.

The longest path from the root to a node which has its children nodes $ and # is the longest palindrome.

- v December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution..

- Anonymous February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use KMP as well for in this method. use S$R word to find longest palindrom. wher R is reverse of string S.

- sushantmax88 December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

string: abc12345cba
reverse: abc54321cba

Ok, u`ll find 'abc' and 'cba' in both strings, now what?

- Anonymous December 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a java code which runs in O(n).
steps:
1. First tokenize the string.
2. For each token u r getting find the length and reverse the string and check palindrome or not
3. Finally compare the length is max or not and print the string.

public class LongPalin {

public int strPalin(String token)
{
int len = token.length();
String reverse = new StringBuffer(token).reverse().toString();
if(token.equalsIgnoreCase(reverse))
return len;
else
return -1;

}

public static void main(String[] args) {

String sent = " civic Dewed Malayalam rotavator redder";
String longpalin = null;
StringTokenizer Tok = new StringTokenizer(sent);
int n=0, maxlen =0;
LongPalin lp = new LongPalin();
while (Tok.hasMoreElements())
{
String token1 = Tok.nextToken();
int len = lp.strPalin(token1);
if(maxlen < len )
{
maxlen = len;
longpalin = token1;

}

}
System.out.println("longest palindrome ="+ longpalin);
}

}

- Bhargava December 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is not O(n)

- Anonymous December 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous
The whole sentence is processed in a single pass
so the time complexity is O(n). can u plz explain why the time complexity is not O(n)

- bhargava December 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

inside loop u have to reverse and compare - these operations are O(n), so total complexity is O(n^2)

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

don't use java.

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the interviewer was expecting me to use sufix trees to solve it..

- see December 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for O(n) we have to check every char only once.
we can maintain a stack and after we read every char peek the stack and if the char is same as stack it will be a middle of palindrome . keep track the size of stack.(A)
then change mode and ever new char pop the stack and if the trend breaks means new char is not same as the top in stack, palindrome ended here; A-StackCurrentSize multiple 2
is the length of it, now start pushing chars to stack again....
this need max O(n) extra space for stack and solves in O(n) time.
same we can do for odd palindrome too...

please review if you find some bug in it...

- verma.tech December 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. assume original string is S
2. reverse each word in the sentence and store it in string R
3. now compare each word in S, R and find longest matching word

this can be done in O(n)

- nirmalr January 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot to mention, step 2 can be done in O(n) time using some extra space.

Reverse the whole sentence, then split it and copy the words from last.

Ex :

Assume string is "I am aabaa"
reverse the string "aabaa ma I"
split it - "aabaa", "ma", "I"
now, copy this words (from last) to new string,

I ma aabaa

- nirmalr January 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

reverse and compare

- syrus January 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but I guess u are making an assumption here that the palindrome will be a complete word; while the question statement does not necessarily support this assumption.

- syrus January 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think you solution work correctly.

Assume the given string is 123aba56, then the correct answer should be aba.

The reverse string is 65aba321, then how will you compare?

I think an O(n) solution may require to build a suffix tree on S#1R#2 within O(n), and then find the latest common ancestor
of #1 and #2.

Another solution would be build the suffix Array on T=S#R@ and a RMQ to calculate the longest common prefix for the suffix Array with O(nlgn) time, then you can check the longest common prefix of T[i..2n] and T[2n-i] (if the length of palindrome is odd, similar in an even case) for all the i, the query can be done in O(1) with RMQ, and the time to find out i is O(n), which together gives you a O(nlnn) + O(n) = O(nlgn) algorithm

- Anonymous January 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The java code is O(n). because you go over the string once and you go over the string again. Which is O(2n) = O(n).

- Anonymous January 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question is not very clear.

if the input string is like "abc aabb abba kkk", the question is very easy.
if the input string is like "aabbaacc", then it is hard. it has no token.

- Anonymous March 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

post-suffix or manacher, O(n)

- wangxuyang October 08, 2012 | 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