Interview Question


Country: United States




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

I think your solution it's almost complete.. but not quite.
For example if a give you string = "caaca", I should remove the last a, but your algorithm removes the c.. resulting in a non-palindrome

- gastonbm June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right, I felt I was missing something. Great catch.

- Blahfoo June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code for removal of character to make palindrome

#include <iostream>
#include <string>
using namespace std;
int checkForPalindrome(string str,int i,int j)
{
	if(i==j)
		return 1;
	if(str[i]==str[j])
		return 1;
	if(str[i]!=str[j])
		return 0;
	return checkForPalindrome(str,i,j-1)&&checkForPalindrome(str,i+1,j);			
}
string removeChar(string str,int i,int j)
{
	if(i==j)
		return str;
	if(str[i]==str[j]&&i+1==j)
		return str;
	if(str[i]!=str[j]&&i+1==j)
	{
		str.erase(i,1);
		return str;
	}
	if(str[i]==str[j])
		return removeChar(str,i+1,j-1);
	if(str[i]!=str[j])
	{
		if(checkForPalindrome(str,i,j-1))
		{
			str.erase(j,1);
			return str;
		}
		else if(checkForPalindrome(str,i+1,j))
		{
			str.erase(i,1);
			return str;
		}
	}				
}
int main()
{
	string str="fbbfe";
         if(checkForPalindrome(str,0,str.length()-1)!=1)
	         str=removeChar(str,0,str.length()-1);
	cout<<str;
}

- vgeek June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code would be incorrect if we must remove a character. e.g. str = "aaaa"

- Miguel Oliveira June 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this code is only for the cases where we have to move a character. Of course for other cases we can call for checkPalindrome first and if it is already then we will not proceed. It can be included easily

- vgeek June 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

All right i have included that check in code..:)

- vgeek June 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming we must delete a character, we can use 2 pointers: from the start and from the end of the string and check if they're equal. If not, we must remove one of them and check if the remaining string is palindrome.
We only do at most 2 checks for palindrome, so this code runs in O(n), n = string.length

bool IsPalindrome(const string&s, int from, int to) {
	for (; from < to; from++, to--)
		if (s[from] != s[to])
			return false;
	return true;
}
int DeleteCharToPalindrome(const string& s, int from, int to) {
	if (from >= to) 	// s is palindrome. if its size is even, we can
		return to;		// just remove one of the middle elements if the size
						// is even or the middle element if size is odd
	if (s[from] == s[to])
		return DeleteCharToPalindrome(s, from+1, to-1);
	else if (IsPalindrome(s, from, to-1)) 
		return to;
	else if (IsPalindrome(s, from+1, to)) 
		return from;
	return -1;
}
// returns -1 if impossible, 0..len-1 otherwise representing the index to delete
int DeleteCharToPalindrome(const string& s) {
	return s == "" ? -1 : DeleteCharToPalindrome(s, 0, s.size()-1);
}

- Miguel Oliveira June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Assuming there exists one character in the string the removal of which turns string into palindrome, we can do as follows:

1. Keep two pointers front=0 and back=n-1.
2. while string[front++] == string[back--] continue;
3. if string[front+1, back] is palindrome then return string without character[front]
else return string without character[back]

Edit: based on gastonbm's reply, changed last step to check for palindrome.

- Blahfoo June 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think your solution it's almost complete.. but not quite.
For example if a give you string = "caaca", I should remove the last a, but your algorithm removes the c.. resulting in a non-palindrome

- gastonbm June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In step 3, instead of checking for just one character, better choice is to check for the palindrom. If the substring between front and back pointer(skipping front) is palindrom then front is the odd char or else back is the odd char

{{if(isPalin(str.substring(front, back+2)))
return charArray[front-1];
else if(isPalin(str.substring(front-1, back+1)))
return charArray[back+1];}}

- amish.cusat June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)

{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
temp[i]=temp[strlen(temp)-i];
temp[i]='\0';
if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}

- coder June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)

{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
temp[i]=temp[strlen(temp)-i];
temp[i]='\0';
if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}

- coder June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<string.h>
int main()
{
char ch[30],temp[30],i,j=0,ch1[30];
scanf("%s",ch);
do
{
for(i=0;i<strlen(ch);i++)

{
if(i>=j)
{
ch1[j]=ch[j+1];
if(i==strlen(ch)-1)
break;
}
else
ch1[i]=ch[i];
strrev(ch1);

if(!strcmp(temp,ch1))
{
printf("%s",temp);
}j++;
}
while(j<strlen(ch));
return 0;
}

- student June 05, 2014 | 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