Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
7
of 9 vote

i=0, j=s.length-1;
do {
    while( i < s.length && s[i]==' ') i++;
    while( 0 <= j       && s[j]==' ') j--;
  
    if( i>=j ) return true;        
    if(s[i++] != s[j--]) return false;
    
} while(true);

Is the question a joke? Amazon? Memory constrained environment? :S

- Urik's twin Cookie Monster (dead now) October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks like ...

- Ajeet October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Probably intended form mobile...

- Leo October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
using namespace std;

int isPalindrome(char *s){
	int len = strlen(s);
	int start = 0, end = len-1;
	bool checkpalindrome = true;
    while(start<end){
		if(*(s+start) == ' '){
			start++;		
		}else if(*(s+end) == ' '){
			end--;
		}else{
			if(*(s+start) != *(s+end)){
				checkpalindrome = false;
				break;		
			}
			start++;
			end--;	
		}
	}
    return checkpalindrome;
}

int main(){
	string str = "ABCB DA";
	cout<<isPalindrome((char*)str.c_str());
}

Please let me know if any mistake I have done

- Rudra October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you need to change this part of the code :
while(*(s+start) == ' ')
start++;
while(*(s+end) == ' ')
end--;
because you may have more than one spaces at the time

- Mani October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isPalinSpaceAgnostic(String x){
                int start,end;
                start = 0;
                end = x.length()-1;
                while(start<end){
                        if(x.charAt(start)==x.charAt(end)){
                                start++;
                                end--;
                        }else if(x.charAt(start)==' '){
                                start++;
                        }else if(x.charAt(end)==' '){
                                end--;
                        }else{
                                return false;
                        }
                }
                return true;
        }

TestCases:

Positive:
"a"
"aa"
"aba"
"  a"
"a  "
"aabbaa"
"a a baa"
"a                  b    a   a  b a       "

Negative:
"ab"
"aab"
"        ab"
"ab      "
"   a a  ba     a a "

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

int IsPalindrome(string str)
{
if(str.empty())
  return 0;
remove_copy(str.begin();str.end(),str.begin(),' ');
int n=str.size();
for(int i=0;i<n/2;i++)
{
   if(str[i]!=str[n-i-1])
     return 0;
}
return 1;
}

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

yaa thats quite simple...

k=n-1;

for(i=0;i<n/2;i++)
{
flag=0;

if(a[i]==a[k])
{
flag=1;
}

if((a[i]==' ')
i++;
if(a[k]==' ')
k--;

if(flag==0)
{
printf("not pallindrome\n");
break;
}

}

- rvndr October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This algo will fail at <space><space>ab<space>a. This is because we have skipped spaces until we reach a and we found that a matches exactly with k's value which is a and the flag becomes one. Now since the i index reaches n/2 it will break the loop and the function returns true. So doing it till n/2 will make it fail. We have to iterate full.

- rengasami21 November 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static boolean isPalin(String s){
		int l=s.length();
		char [] ch=s.toCharArray();
		int i=0, j=l-1;
		while(i<j){
			if(ch[i]!=' ' && ch[j]!=' '){
				if(ch[i++]!=ch[j--]){
					return false;
				}
			}
			if(ch[i]==' '){
				i++;
			}
			if(ch[j]==' '){
				j--;
			}
		}
		return true;
	}

- shashi_kr October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code will fail in case of white spaces " " (string containing only spaces)

so takecare of edge case correctly.

- niraj.nijju October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool palindrome(string str){
        for(int i = 0, j=str.length() -1; i<str.length()/2; i++, j--){
                if(str[i] == ' ') i++;
                if(str[j] == ' ') j--;
                if(str[i] != str[j]){
                        cout<<str[i]<<" "<<str[j];
                        return false;}
        }
        return true;

}

- ambu.sreedharan October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no

- anon October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//go
func isPalindrome(s string) bool {
	s = strings.Replace(s, " ", "", -1)
	b := []byte(s)
	mi := len(b) - 1
	hi := mi / 2
	for i, c := range b {
		if c != b[mi-i] {
			return false
		}
		if i > hi {
			return true
		}
	}
	return false
}

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

private static boolean checkPalindrome(String str, int start, int end) {
		if (start <= end && end - start <= 1) {
			return true;
		}
		return checkPalindrome(str, ++start, --end);
	}

- Qing October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why oh why recursion

- ambu.sreedharan October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this one is genius algorithm

- anon October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Last posted recursion is wrong. Use this one.

private static boolean checkPalindrome(String str) {
		if (str.length() <= 1) {
			return true;
		}
		int i = 0, j = str.length() - 1;
		
		while (i < j) {
			if (str.charAt(i) == ' ') {
				i++;
				continue;
			}
			if (str.charAt(j) == ' ') {
				j--;
				continue;
			}
			
			if (str.charAt(i++) != str.charAt(j--)) {
				return false;
			}
		}
		return true;
	}

- Qing October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works?

- anon October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following recursion is working:

private static boolean checkPalindrome(String str, int start, int end) {
		if (start <= end && end - start <= 1) {
			return true;
		}
		if (str.charAt(start) != ' ' && str.charAt(end) != ' ') {
			if (str.charAt(start++) != str.charAt(end--)) {
				return false;
			}
		} else {
			if (str.charAt(start) == ' ') {
				start++;
			}
			if (str.charAt(end) == ' ') {
				end--;
			}
		}
		return checkPalindrome(str, start, end);
	}

- Qing October 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works? promise?

- anon October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPalindrome(const char* src)
{
int tail = strlen(src);
int head = 0;
while(head < tail ){
    while(head< tail&&*head==' ') head++;
    while(tail>head&&*tail==' ')tail--;
    if(*head!=*tail)return false;
}
return true;
}

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

Is this question for a developer?
Anyways, what is the meaning of working optimally under constrained memory conditions?

- Victor October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPalindromeIgnoreSpace(String s) {
		if(s==null) {
			return false;
		} else if(s.isEmpty()) {
			return true;
		} else {
			int p1=0;
			int p2=s.length()-1;
			while(p1<p2) {
				while(s.charAt(p1) == ' ') {
					p1++;
				}
				while(s.charAt(p2) == ' ') {
					p2--;
				}
				if(s.charAt(p1) != s.charAt(p2)) {
					return false;
				}
				p1++;
				p2--;
			}
			return true;
		}
	}

- ksjeyabarani November 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String s1=s.nextLine();
if(rev(s1))
System.out.println("String is palindrome");
else
System.out.println("string is not palindrome");
}
public static boolean rev(String s1)
{
int n=s1.length();
if(n==1 || n==0)
return true;
if(s1.charAt(n-1)==' ' || s1.charAt(0)==' ' || s1.charAt(0)==s1.charAt(n-1) )
return rev(s1.substring(1,n-1));
return false;

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

bool palindrome_check(const string& str) {
int start = 0;
int end = str.size() - 1;

while (start <= end) {
while (start <= end && str[start] == ' ') {
++start;
}

while (start <= end && str[end] == ' ') {
--end;
}

if (start >= end) {
break;
}

if (str[start] != str[end]) {
return false;
} else {
++start;
--end;
}
}

return true;
}

- Anonymous November 05, 2013 | 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