Amazon Interview Question for Software Engineer / Developers






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

#include <stdio.h>
#include <string.h>

void printArr(char s[], int i, int j) {
    while(i<=j)
        printf("%c   ", s[i++]);
    printf("\n");
}

void palindromes(char s[]) {
    int i, j, L, H;
    L = 0;
    H = strlen(s)-1;
    for(i=L; i<=H; i++) {
        // odd palindromes with i at center
        for(j=1; i-j>=L && i+j<=H && s[i-j]==s[i+j]; j++)
            ;
        if(j>1) {
            printArr(s, i-j+1, i+j-1);
        }
        // even palindromes, no-center
        for(j=0; i-j>=L && i+j+1<=H && s[i-j]==s[i+j+1]; j++)
            ;
        if(j>0)
            printArr(s, i-j+1, i+j);
    }
}

int main() {
    char *s = "malayalam is funny language";
    
    palindromes(s);
    
    return 0;
}

Input: malayalam is funny language
Output:
a l a
m a l a y a l a m
a l a
n n

- Anil January 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

in this string "malayalam is funny language"
list of all palindromes are
+ malayalam
+ ala (m_ _ _ y _ _ _ m)
+ alayala
+ layal
+ aya
+ nn

for out of all { malayalam, alayala, layal, aya } share the same center so
only
ala and nn are non cocentric

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

Just to point out to everyone, this is a nice solution and it works perfectly fine.

- coder1212 March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is non concentric palindrome? for that matter.. what is concentric palindrome?

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

I think concentric palindromes are palindromes with the same center.
acasdabadso: sdabads and aba are concentric and aca is not concentric with the rest.

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

even ex: In string "dabacabae", aba aba and abacaba are three non concentric palindromes.

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

No. aba and aba are the only non-concentric palindromes. because abacaba shares same center with aca and bacab.

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

Is this same as determining if a string is a paliandrome whose length is an even number?

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

in that case thats fairly easy :)

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

My first idea is, treat each character as the center and starting at it, find the longest radius of the palindrome with this character as the center. the time complexity is O(n*h) where h is the longest palindrome.
Comparing with the suffix method: 1 suffix tree requires O(n) which is much faster. 2. But, what if the hr wanted you to write the code??

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

void nonConcentricPalindrome (char *str)
{
    if (str == NULL)
        return;
    for (int i = 1; i < strlen (str) - 1; i++)
    {
        if (str[i - 1] == str[i + 1])
        {
            int p = i - 1;
            int q = i + 1;
            while (p >= 0 && q < strlen - 1)
            {
                if (str[p] != str[q])
                    break;
                p++;
                q--;
            }
            printf ("The starting index is %d the ending index is %d and center is %d\n", p, q, i);
        }
    }
}
In the worst case the runtime will be O(n^2). Any better solution would be apprciated

- anonymouS January 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{ void nonConcentricPalindrome (char *str) { if (str == NULL) return; for (int i = 1; i < strlen (str) - 1; i++) { if (str[i - 1] == str[i + 1]) { int p = i - 1; int q = i + 1; while (p >= 0 && q < strlen - 1) { if (str[p] != str[q]) break; p--; q++; } printf ("The starting index is %d the ending index is %d and center is %d\n", p+1, q-1, i); } } } Sorry for some corrections in the above code - anonymouS January 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution:

some optimization suggested in your code
int p = i - 1;
int q = i + 1;
replaced by
int p = i - 2;
int q = i + 2;

- Roshan Mangal January 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe you are trying to avoid some equality check but we need to add some extra boundary check and also if characters doesn't match, check the previous characters.

- Anurag Singh January 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above code is a good solution. But here we are making an assumption that the string is going to be always ODD length.
eg. It will work for the string like 'abcba', but it will not work for the string 'abccba', which is also an EVEN length pallindrome string.
So to do for the even length string,we should ALSO consider two characters(which are same eg. 'cc') at a time and try to find the biggest radius, as we are doing above. This way we will be able to cover all the cases.

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

Hi Ricky, I had this thought in my mind when I was working on it. But the question says "non concentric palindrome" which i guess is palindromes which do not have a common center. So i thought all the palindromes had to b around some or the other center but not same, hence developed only for odd length strings

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

Both, Anil and anonymouS, good one !!!

- Anurag Singh January 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code I am pasting below is based on the observation that,

for odd length non concentric palindrome, u only need to check if the palindrome of length 3 extends to 5.

in case of 'layal' - in order to invalidate 'aya' u shud go one more char on either side and establish it is a palindrome. If you cant establish that as a palindrome then u can print out 'aya'

for even length palindromes u only need to look if the palindrome of length 2 extends to 4.

in case of 'anna' - u can discard 'nn' because when u extend it by one more character on either side it will form a palindrome. but in case of 'funny' - when u extend one character on either side of 'nn', it will become 'unny' which is not a palindrome. So u can print out 'nn' as a non-concentric palindrome.

Please let me know if this logic works fine. I have tested it and it seems to be correct. comments are welcome

- slimboy July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey787" class="run-this">

#include <iostream>
using namespace std;

void calculateNonConcPalindrome(string& str);
void isPalindrome(string& str, int begin, int size);

int main(int argc, char* argv[]){

cout<<"Calculating non concentric palindromes"<<endl;
string word;
cout<<"Enter word: ";
cin>>word;

calculateNonConcPalindrome(word);
void isPalindrome(string& str, int begin, int size);

return 0;
}

void calculateNonConcPalindrome(string& str){

for(int i=0; i <= str.size()-4; i++){

	//checking for even lengths
	isPalindrome(str, i, 4);
	if(i == str.size()-4)
		return;
		
	isPalindrome(str, i, 5);
}	


}

void isPalindrome(string& str, int begin, int size){

	if((str[begin] == str[begin+size-1]) && (str[begin+1] == str[begin+size-2]))
		return;
		
	if(str[begin+1] == str[begin+size-2]){
		if(size%2 == 0)
		cout<<str[begin+1]<<str[begin+size-2]<<endl;
		
		else
		cout<<str[begin+1]<<str[begin+2]<<str[begin+3]<<endl;
	}
		
		
		
	return;	

}

</pre><pre title="CodeMonkey787" input="yes">
</pre>

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

Marhaba,

Great piece on Amazon Interview Question for Software Engineer / Developers, I’m a fan of the ‘flowery’ style Looking forward to more long form articles ??

I have AWS ec2 six instance for 3 year subscription and type is c4.xlarge for purpose of web-server. due to high usage of RAM/CPU/IO, we wanted extend capacity means resizes resources for existing c4.xlarge to c42xlarge type. may I know the how much extra cost we need to pay for each instance. if we want resize ec2 instances.

Super likes !!! for this amazing post. I thinks everyone should bookmark this.

Thanks and Regards
Radhey

- Radhey June 09, 2018 | 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