Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Reverse the string, and then find the longest common subsequence using a suffix tree. That's N^2 but looks very straightforward.

The most efficient way I think is to loop through 1 to 2n-1 (n being string len), and check on each location possible palindromes. 1 to n is not right as it will miss mirrored palins like AAABBB. This is essentially O(N) with worst case having all same chars.

- Xiang October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is aaabbb considered a palindrome? I thought it was restricted to things in the form of aabbbaa

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

Can you elaborate on the O(N) solution?
1. loop through 1 to 2n-1 ? 2n-1? it will be out of range
2. check on each location possible palindromes. what do you mean?

- Vincent October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 to 2n-1 is just imaginary (2n-3 is better). Take "AABB" for example, imagine "A1A3B5B" looping from 1 to 5. at loop counter 1, get palindrome "AA"; at 2, get nothing as AAB is not palindrome; at 3, get "AABB"; at 4, get nothing due to ABB; at 5, get palindrome "BB".

Here's working C# code:

public static int FindLongestPalindromeSubsequence_X(string s)
        {
            int maxLen = 0;

            for (int i = 1; i < 2 * s.Length - 2; i++)
            {
                int low;
                int high;

                if (i % 2 != 0)
                {
                    low = i / 2;
                    high = low + 1;
                }
                else
                {
                    low = i / 2 - 1;
                    high = low + 2;
                }

                int curLen = 0;
                while (low >= 0 && high < s.Length)
                {
                    if (s[low] == s[high])
                    {
                        string currentPalindrome = s.Substring(low, high - low + 1);
                        Console.WriteLine("FindLongestPalindromeSubsequence_X: " + currentPalindrome);

                        curLen = high - low + 1;
                        if (curLen > maxLen)
                        {
                            maxLen = curLen;
                        }

                        low--;
                        high++;
                        continue; // Not really necessary;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return maxLen;
        }

- Xiang October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't 12345gfg/gfg54321 should be the longest palindrome?

- nikhilgupta0901 November 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Xiang,

How could AAABBB be a palindrome? Only AABBAA is a palindrome. Please correct me if I'm wrong.

- code_mystery February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great buddy... Nice job...

- Vineet Yadav March 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Xiang,
Can you please tell me the output of your program. I am not getting correct output.
if the question is related to max subSequence palindrome then output would be
12345gfg/gfg54321 of length 16

Well my code just prints maxlength

private static int FindLongestPalindrom(String string) {
return FindLongestPalindrom(string.toCharArray(),0,string.length()-1);

}

private static int FindLongestPalindrom(char[] s, int i, int j) {
if(i==j)
return 1;
else if(i==j+1){
if(s[i] == s[j]){
return 2;
}else{
return 1;
}
}else if(s[j] == s[i+1])
return FindLongestPalindrom(s, i+1, j-1)+2;
else
return Math.max(FindLongestPalindrom(s, i+1, j), FindLongestPalindrom(s, i, j-1));
}


We might still improve by using memoization.

- anand March 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is an O(n) algorithm to find the longest palindrome
Named Manacher Algorithm.

Reference:( you should add "." in the address)
zhuhongcheng wordpress com/2009/08/02/a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/
If you can read chinese then you can go to:
www cnblogs com/a180285/archive/2012/03/03/Manacher_Algorithm.html

- Meowhuang October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Substring != sub sequence.

- Anonymous October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The array is unsorted therefore it will take O(n) to search for duplicated elements, once you find one call a function to check if the elements next to them form a palindrome, if so save it. The biggest palindrome possible should be n/2 so the worst case is quadratic, but not likely in practice.

After save it just check which one of your saved palindromes is the biggest and return it.

- Anonymous October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dynamic programming
dp[start][end] -> number of letters
dp[start][end] =
case start>end -> 0
case start =end -> 1
case ch[start]==ch[end] -> 1+dp[start+1][end-1]
case ch[start]!=ch[end] -> max(dp[start+1][end],dp[start][end-1])

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

Based on your algorithm, the longest sequence in the above example has length 16, which gives "12345gfg/gfg54321".

Revise: the last sentence change to
case ch[start]!=char[end] -> 0

Please comment.

- Richard October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then I think I got the problem statement wrong...

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

Yes, if you say, that both have to be consecutive sequences , then you just have to change the last definition to zero.

- Anonymous October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>

using namespace std;
int main()
{ int n,max=0;
char s[30];
cout<<"enter the string\n";
for(int i=0;i<20;i++)
cin>>s[i];
for(int i=0;i<20;i++)
{ int count=0,max1=0;
for( int j=i,m=19;j<=m;)
{ if(s[j]==s[m])
{count++;j++;m--;
if(max1<count)
max1=count;
}

else
{ break;}

}
if(max<max1)
max=max1;
}
cout<<"max of palindrome length is"<<max<<"\n";
system("pause");
return 0;
}

- Mukesh Kumar Dhaker October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define bool int
#define true 1
#define false 0

typedef struct maxSegment{
int a;
int b;
}MAXSEGMENT;

int findReverseStr(char a[], int s1, int s2, int d1, int d2)
{

if(s2<s1 || d2<d1 || a==NULL)
return false;
if((s2-s1) > (d2-d1))
return false;

int n = s2-s1+1;
int i,j=0;

for(i=d2; i>=d1; i--)
{
if(j>=n)
return 1;
if(a[i] == a[j])
{
j++;
}else
{
j=0;
}
}

return 0;
}

main()
{
char a[]="12345gsfggd541fhgs54321fgsdh";
int n = strlen(a);
int i, j, k;
MAXSEGMENT maxseg;
maxseg.a=0;
maxseg.b=0;


for(k=0; k<n; k++)
{
for(i=k; i<n/2+1; i++)
{
//find reverse string of a[k..i] from a[i+1..n]
if(findReverseStr(a, k, i, i+1,n-1))
{
if((i-k)>(maxseg.b-maxseg.a))
{
maxseg.a=k;
maxseg.b=i;
}
}

}

}

for(i=maxseg.a; i<=maxseg.b; i++)
printf("a[%d]=%d ", i, a[i]);
printf("\n\r maxseg.a=%d, maxseg.b=%d", maxseg.a, maxseg.b);
/*
if(findReverseStr(a, 0, 4, 5,27))
printf("yeah");*/
}

- implemented in C, but with bad performance I guess, can anyone help optimize it? October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given an integer array A[1..N],write a programme for finding max value of A[i]^A[j] , where ^ is bitwise xor.

- mukul.s2003 October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using two pointers ??

Algo :
1. Keep one pointer at start and other at the end.
2. Move first pointer and move second pointer until it finds element pointer by first.
3. If first and second points to same element then exit
4. If it does find a hit then keep track of indexes which are matched.

This is similar to reversing a string, but with out using extra string object.

- princeelectric October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its pretty simple reverse the string and find longest common substring in O(n)

- sukusuku October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of my friends was asked this question;
The interviewer wanted a O(n2) solution with no extra space.

Algorithm that he told me that I think works goes something like this:

for each character in the array
{
start checking from the end of the array towards the start to see if there is a substring present while keepinf track of the largest substring found so far.
}

- Illusion October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't it the longest palindrome should be 12345gfg/gfg54321 ?
Sorry If I miss anything

- nikhilgupta0901 November 23, 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