Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

step1 : This problem can solved just by finding permutations of a,e,i,o,u in 5 places ( repetition allowed )

step2: divide 10 place into two parts. find permutations for first 5 places, and fill remaining 5 places in reverse order.

Step3: print 10 places at the end of each permutation . total palindromes 5*5*5*5*5

code ::

//calling convention permutations( input, permute, 5, 0);

void permutations(char Input[], char permute[], int n, int index )
{
int i=0;
// printing palindrome
if( index == n )
{
for( i =0 ;i < n; i++ )
printf("%d, ", Permute[i] );
for( i =n-1 ;i >= 0; i--)
printf("%d, ", Permute[i] );
Printf("\n");

return ;
}

for (i = 0 ; i < n; i++)
{
Permute[index] = Input[i];

permutations( Input, permute, n, index +1 );
}

}

- siva.sai.2020 April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was trying to trace through this function, and I don't see how this is supposed to work correctly.

I just ran this in Visual C++

#include <iostream>

using namespace std;

void permutations(char input[], char permute[], int n, int index )
{
	int i=0;
	// printing palindrome
	if( index == n )	{
		for( i =0 ;i < n; i++ )
			printf("%c, ", permute[i] );
		for( i =n-1 ;i >= 0; i--)
			printf("%c, ", permute[i] );
			
		printf("\n");

		return ;
	}

	for (i = 0 ; i < n; i++)	{
		permute[index] = input[i];
		permutations( input, permute, n, index +1 );
	}

}

int main()
{

	char input[] = {'a','b','c'};
	char permute[2] = {};

	permutations(input, permute, 2, 0);

	return 0;
}

and the output is 

a, a, a, a,
a, b, b, a,
b, a, a, b,
b, b, b, b,

There seems to be an issue with this solution, where it only gives correct output if n = input.size() if it doesn't then it skips outputting anything pertaining to the indices in input past n. Therefore it will only work if we are given a length k that is double that of the size of the alphabet given.

Can any one help explain how you would create all permutations regardless of the size of the palindrome?

- confused April 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correction

int main()
{

char input[] = {'a','b','c'};
char permute[3] = {};

permutations(input, permute, 3, 0);

return 0;
}

- siva.sai.2020 February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good questions. A recursion approach would probably be easiest to implement it. The complexity of the algorithm depends on two variables, size of the alphabet set n and size of resulting palindromes k. If n=5 and k=3 then we would be expected to come up with results such as:

aaa
aea
aia
aoa
aua
eae
...

so three postions, half them will have k choices each, the other half will depend on choices made in the first half

for n=5, k=5, the structure could look like:

n n n 1 1 -> total number of palindromes with be O(n^3) or to generalize O(n^(k/2))

public static void palindromes(char[] letters, int k, int position, char[] str, Set<String> result) {
        for(char c : letters) {
            char[] str_copy = str.clone();
            str_copy[position] = c;
            str_copy[(k-1)-position] = c;
            if(position == k/2) {
                // finished
                result.add(new String(str_copy));
            } else {
                palindromes(letters, k, position+1, str_copy, result);
            }
        }
    }

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

nice

- Anonymous April 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

great insight siva sai
code in python

def permutations(alp,n):
if n==0: return [""]
lower=permutations(alp,n-1)
res=[]
for a in alp:
for low in lower:
res.append(a+low)
return res

def palindrome(alp,n):
num=0
if n%2==0:
num=n/2
else:
num=n/2+1

complete=permutations(alp,num)
if n%2==0:
for perm in complete:
print perm+perm[::-1]
else:
for perm in complete:
print perm+perm[:-1][::-1]

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

@light, thanks

- siva.sai.2020 April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

great insight siva sai
code in python

def permutations(alp,n):
if n==0: return [""]
lower=permutations(alp,n-1)
res=[]
for a in alp:
for low in lower:
res.append(a+low)
return res

def palindrome(alp,n):
num=0
if n%2==0:
num=n/2
else:
num=n/2+1

complete=permutations(alp,num)
if n%2==0:
for perm in complete:
print perm+perm[::-1]
else:
for perm in complete:
print perm+perm[:-1][::-1]

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

Two cases
odd size and even size.
odd size --> center is a single letter e.g. size = 5 , palindrome = madam .. center = d
even size --> center is a double letter e.g. size = 6, palindrome = maddam.. center = dd
(Counter Examples welcome)

So if odd, add the first letter in the char set once and expand around the center.
e.g. Input Set (a,b) , size = 3 . Start with a --> aaa --> bab
Then b --> aba --> bbb
Similarly, for even sized Palindrome
e.g. Input Set (a,b) size = 4, Start with aa instead of a.
aa --> aaaa --> baab
come back to next char i.e. b .. start with bb --> abba --> bbbb

here is the java code for this implementation

public static ArrayList<String> sizeKPalindromes(ArrayList<Character> charSet, int size) {
		Boolean odd = false;
		if ( (size & 1) > 0 ) {/* odd */
			odd = true;
		}
		return sizeKPalindromes("", charSet, size,odd);
		
		
	}

	private static ArrayList<String> sizeKPalindromes(String string, ArrayList<Character> charSet, int size, Boolean odd) {
		if (string == null) {
			return null;
		}
		
		if (string.length() == size) {
			ArrayList<String> currPalindrome = new ArrayList<String>();
			currPalindrome.add(string);
			return currPalindrome;
		}
		
		ArrayList<String> allPalindromeStrings = new ArrayList<String>();
		
		for (int i = 0; i < charSet.size(); i++) {
			
			String tempString = new String();
			
			if (odd && string.isEmpty()) {
				tempString = string + charSet.get(i);
			}
			
			else {
				tempString = charSet.get(i) + string + charSet.get(i);
			}
			
			ArrayList<String> tempPalindromes = sizeKPalindromes(tempString, charSet, size, odd);
			allPalindromeStrings.addAll(tempPalindromes);
		}
		
		
		
		
		return allPalindromeStrings;
		
	}

- dilip.rangarajan April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great solution.

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

@dilip.rangarajan
Can you please explain your code? I am unable to understand how it works according to your above stated algo

- Ragavenderan April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The point is if the string is "ABCD" and k =5,
then we need to fix the middle position for each character..
1.palindrome[2]='A',
now we should find all possible 2 character sets to fill palindrome[0] and palindrome[1] and reverse it in 3 and 4 indices..

- Ragavenderan April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Or expand around center by prefixing and suffixing the same character.
E.g. a --> aaa or a-->bab

- Dilip April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Or expand around center by prefixing and suffixing the same character.
E.g. a --> aaa or a-->bab

- Dilip April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *s = {a,b,c,d}; // the set
int cs = 4; // the size of the set

void f(int n)
{
   char *a = new char[n+1]; // buf for storing the chars
   a[n] = '\0';
  
   f1(a,floor(n/2),n);
}

void f1(char* a, int x, int n)
{
    for(int i=0;i<cs;++i)
    {
         a[x]=a[n-1-x]=s[i];

         if(x==0)
         {
            printf("%s\n", a); 
         }
         else
         {
            f1(a, x-1, n);
         }
    }   
}

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

ok.

- jiangok2006 July 17, 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