Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
#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
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 "
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;
}
}
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.
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;
}
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;
}
private static boolean checkPalindrome(String str, int start, int end) {
if (start <= end && end - start <= 1) {
return true;
}
return checkPalindrome(str, ++start, --end);
}
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;
}
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);
}
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;
}
}
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;
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;
}
Is the question a joke? Amazon? Memory constrained environment? :S
- Urik's twin Cookie Monster (dead now) October 30, 2013