Interview Question


Country: -




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

Which company?

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

Have a pointer at the start of the string and one at the end of the string.

Check if chars at the start and end pointer are same, increment start pointer, decrement end pointer, if they are not same, insert char which is at start pointer at (end pointer + 1), and increment start pointer

Repeat the above, till both pointers meet.

- Anonymous September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not give the minimum number of characters to be added to make a string pallindrome. Check it out.

- mohit September 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

find_min_insert(string)
   if (length of string is zero or one)
      return 0;
   if (first char==last char)
      return find_min_insert(string after removing first and last character)
   return 1+min(find_min_insert(string after removing first character),find_min_insert(string after removing last character))

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

Here's a hint to compute min # of insertions to convert string S to palindrome:

find LCS of S and Reverse(S)

- lol September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suggest you revisit your hint again.

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

S = c a b c b a y c 
T = Rev(S) = c y a b c b a c
LCS (S,T) = c a b c b a c

min # of insertions  = |S| - LCS

- lol September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL at LOL.

Theorem: All numbers are prime.

Proof: Consider 2.
QED.

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

Well, I was bit hectic last time. Hence, the example is kinda misleading. My solution finds longest palindromic subsequence, which is indeed min # of char deletion that make a string palindrome.

As the given problem is to insert, it'd be finding edit distance of S and reverse(S), considering only insertion is allowed.

I'm not here as mentor to guide some smart ass. So, I don't pay heed to someone who even doesn't have courage to have a nick... lurking here to seeking for READY-MADE solution, rather giving counter-example and challenging someone in constructive manner. Stop.

- lol September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL. It was for your benefit (and mine, I was hoping to learn something from you). You might well be right, but it is not obvious that an LCS of S and Reverse(S) must be palindromic.

Here is one which seems to be a counter-example:

1 5 2 4 3 3 2 4 5 1
1 5 4 2 3 3 4 2 5 1

LCS = 1 5 2 3 3 4 5 1

Of course it might so happen that there is at least one subsequence with the same length as LCS which is palindromic, but a burden of proof of that is on you (and this is what I was hoping you will convince me of, but alas...).

BTW, LCS algorithm is Theta(n^2) I believe. There are O(n^2) algorithms to find the largest palindromic subsequence (quite similar to LCS, I grant you that), so we could use that instead of LCS which has this potential gotcha.

If all I wanted was the solution, I would not start with "You might want to revisit that hint"...

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

As for the ridiculous nick comment. Here goes. Does that help, Mr. lol?

- LOLSUCKSNICK. September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It might be a bit late, but, apologies to lol for any distress I might have caused you. You caught me in a bad mood, which was only made worse by your unwarranted comment about spoon feeding etc and the accusation of hiding behind anonymity was quite ridiculous.

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

Also "c a b c b a c" is not the answer because no removals are allowed (from the original string) and "y" is missing.

- akasaka December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (a[i] == a[j]) // a is the string array.
dp[i][j] = dp[i+1, j-1]
else
dp[i][j] = 1 + min(dp[i][j-1] , dp[i+1][j]);

- Anonymous September 21, 2011 | 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