Amazon Interview Question for Software Engineer in Tests






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

That is true anon! we can try this!! since spaces affect the result

how ever, we can consider a lot of special cases such as:
upper and lower case
dots, commas, and stuff like that
spaces
and so on:

Example:
A dog! A panic in a pagoda!
Ah, Satan sees Natasha.
Are we not drawn onward, we few, drawn onward to new era?
Do geese see God?
I prefer pi.
If I had a hi-fi.
Ma is as selfless as I am.
Mr. Owl ate my metal worm.
Never odd or even.
No devil lived on.
No lemon, no melon.
No, sir, away! A papaya war is on!
Red rum, sir, is murder.
Rise to vote, sir.
So many dynamos!


this code just check for spaces. I do not consider upper cases, and special characters. but I don't think it is hard to implement it mbut in an interview you have to let the interviewer know that you notice that.

int isPalindrome(char* string)
{
	char* p1 = string;
	char* p2 = string;
	while(*p2)
	{
		p2++;
	}
	p2--;
	while(p1<p2)
	{
		if(*p1 == ' '){p1++; continue;}
		if(*p2 == ' '){p2--;continue;}
		if(*p1++ != *p2--)
			return 0;
	}
	return 1;
	
}

axel_net@hotmail.com

- Axel David Velazquez Huerta. October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To explicitly satisfy such condition, you don't need to first go through whole string to find out end of string.

Following code is good enough:
===============================
bool isPalindrome(char * str)
{
assert(str != 0);
int i=0, j = strlen(str)-1;

while(i<j)
{
if(str[i] == " ")
{
i++;
}
else if(str[j] == " ")
{
j--;
}
else if(str[i] != str[j])
{
return false;
}
else
{
i++; j--;
}
}
return true;
}

- S ... November 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPalindrome(char * str)
{
   assert(str != 0);
   int len = strlen(str);
   if(len%2 == 1)
     return false;
   for(int i = 0 ; i < len/2 ; i++)
   {
      if(str[i] != str[len - i -1])
        return false;
   }
   return true;
}

- Ashutosh October 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(len%2 == 1)
return false;

This is incorrect, as strings with odd lengths can be palendromes - IE 'nun'.

- anon October 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wrong solution ..U dont have to check that string is palindrome or not. U have to find that is it possible to form a palindrome using characters of given string

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

manage count for all the letters present in the string..
count for all letters should be even ..with an exception of any one(or none) having odd count..
IF the above condition is satisfied.. the string can be made into a palindrome..

- Anonymous November 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "abcfba";
Boolean palindrome=true;
if(str.length()==0 | str.length()==1)
{
palindrome=true;
}
else
{
int i=0;
int j=str.length();
for(i=0;i<str.length();i++)
{
for(j=str.length()-1;j>0;j--)
{

if(str.charAt(i)==str.charAt(j))
{
palindrome = true;
}
else
{
palindrome = false;
}
}
}

I think the running time is O(n^2)..Is there anything better than this one?

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

I think the qtn is not to find a string is palindrome or not?
Algorithm to find that palindrome can be made from a string
eg:
damam >> madam


I would write an algorithm like this

scan each chars and store in a int array[256] their counts
check for the count of each chars.
if there are more than one odd count it cannot make a palindrome.

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

//Java
public boolean checkPalindrome(String str){
		
		str=str.toLowerCase().replace(" ","");
		char[] strArr=str.toCharArray();
		int length=strArr.length;
		for(int i=0;i<length/2;i++){
			if(strArr[i]!=strArr[length-1-i])
				return false;
		}		
		return true;
	}

- sl714 August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For a string to become palindrome there are two cases:
1) if the length of the string is Odd. In this case we will have all the charaters even numbers of time except for one character.
2) if the length of the String is Even. here all the characters should occur even numbers of time.

So, we maintain a HashMap<Character,Integer> of the String

we iterate throught the map

and every entry in the map we check the value

if the length is even
if at anytime we found any key with value which is odd then return false(No Palindrome possible)
else if every key has even value then return true(palindrome is possible)

if the length is odd
if key has even value; continue;
if key has odd value keep a counter and increment its value for every odd value of a key.
if the counter!=1 return false;
else return true;

- Ravi Ranjan March 08, 2015 | 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