Interview Question






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

Lets call given input string, T and string that contains characters to be removed as S. 1. Create auxiliary array of 26 size representing the character set (alphabets). Set 1 in chars location if char occurs in S else 0
2. Read string T, if index[char]==1 in auxiliary array, then remove the char from string T.

This will be done in O(n) + O(m) = O(n) time complexity with constant space complexity (26 sized array).

- cuppanomics May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is exactly O(n).
What is m here ?

- gevorgk May 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats right. Sorry, its just O(n).. Searching for the character in the auxiliary array is O(1)....

- cuppanomics May 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

does not look like a generalized solution....
"This" is different than "this" , what if the substring changed to "ti". then will you have new array of 26? How will you cover any special characters like "@"

- Anonymous May 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a hash table instead of auxiliary array. Hence, the lookup would be O(1)

- karanjain2910 July 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. create a hash table with the character to be removed, here i & s
2. 0 - length(str)
if(hash.get(i)!= str.charAt(i)
newstring[destination++]=string[source]

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

1. create a hash table with the character to be removed, here i & s
2. source = 0 - length(str)
if(hash.get(str.charAt(i))!= str.charAt(i)
newstring[destination++]=string[source]
else
source++

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

Can someone make the question clearer.
Is it that only when 'i' and 's' appear together, they shud be removed or any occurrence of 'i' and 's' in source string shud be removed.

- vsp August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are we going to remove given chars or a given string?

- erm October 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rem_char(char *word,char to_rem)
{
int next_ins = 0,trav = 0;
while(word[trav])
{
  if(word[trav] != to_rem)
    word[next_ins++] = word[trav++];
  else
    trav++;
}
word[next_ins] = '\0';
}

- Sangeeth Kumar July 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fast string search algo' should work here. Refer wikipedia

- KaLee November 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a solution in O(n)

public String removeString(String input, String remove) {
		char[] inputArray = input.toCharArray();
		char[] removeArray = remove.toCharArray();
		String newString = "";
		String tmp = "";
		int i, j = 0;
		int removelength = removeArray.length;
		for (i = 0; i < inputArray.length; i++) {
			if (inputArray[i] == removeArray[j]) {
				tmp = tmp + removeArray[j];
				j++;
				if (j == removelength) {
					j = 0;
					tmp = "";
				}
				continue;
			} else if (j > 0) {
				newString = newString + tmp + inputArray[i];
				j = 0;
				tmp = "";
			} else {
				newString = newString + inputArray[i];
			}
		}
		if (j > 0)
			newString = newString + tmp;

		return newString;
	}

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

Here we parse the 1st string("This is a boy") character by character, and everytime we check with the 1st char of the 2nd String("is" which is 'i' here). Now if we match with it then all we need to do is to compare substring till the size of 2nd string from the current index if the two string are equal if yes then we remove all the character from the 1st string from the current index to the size of the 2nd String.

if at any moment we find that the substring and the 2nd string are not equal the we move our cursor to the next character in the 1st striing and repeate the above steps.

And we can improve this by using finite automata.

- 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