Amazon Interview Question for Front-end Software Engineers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 6 vote

We can do this in place.

void RemoveChar(char* pStr, char ch)
{
           if(pStr == NULL)
              return;

            int len = strlen(pStr);
            int index = 0;
            int res = 0;
            while (index < len)
            {
                      if (pStr[index] != ch)
                      {
                            pStr[res++] = pStr[index];
                      }
                      index++;
             }
             pStr[res] = "\0";
}

- Vs September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is easy :S

If stored in array and the array is small, just shift parts of the array over
But this is O(n^2) in the worst cases like "sasasasasasasasa" (delete s)

If you want good big-O performance to handle long strings, walk down your array one character at a time and insert into a linked list (but skip the characters you want to delete). When you are done, the linked list will hold your answer (you can copy it it to an array if you want).
This is O(n).

You can do some tricks with just an array to improve on the brute force also.

- bigphatkdawg September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you know the upperbound on string size, you can use a vector/ArrayList too instead of LinkedList

Basically any mutable List ADT... keep inserting the characters you want to keep

- bigphatkdawg September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am just curious why we are thinking of complex algorthim when it can de done simply through java collections.

- Anonymous September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

We can make a pass through array and will keep adding each characters to another array.If we encounter character that we want to delete then we can use "continue" to skip it..It can be done in time-complexity O(n).

- vishal.2947 September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You no need of another array
simplay take two pinter
one is IP_pointer other is res_pointer
after that simplay run a loop

remove_char(chat t , char *str)
{
char * ip_ptr, res_ptr
ip_ptr= str;
res_ptr = str;
while(*str)
{
if(*str == t)
{
ip_ptr++;
}
else
{
ip_ptr++;
res_ptr++;
}
}
*res_ptr = NULL;
}

- Ashish September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

in java using List<Character> :Arraylist.

void RemoveChar(List<Character> a, char ch)
{
if(a.contains(ch))
{
a.remove(ch);
}
for(char x : a)
{
System.out.print(x);
}
}

- dark666 September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Fairly easy in Javascript

var text = 'YOUSUF';
console.log(text.replace(/F/gi, ''));

- Zach S October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function deleteChar(str, char){
    var newStr = str.replace(char, '');
    return newStr;
}

alert(deleteChar('YOUSUF','F'));

- shikolay December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// case insensitive
function deleteChar(inp, t){
	return inp.replace(/t/i, '');
}

- JB March 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var deleteCharacters = function(char, str){
  
  return str.split('').filter(function(val){
    return val !== char;
  }).join('');

- Anonymous August 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const deleteChar = (inputStr, char = '') => { 
  if (!char) {
    return false;
  }
  let outputArr = [], index = -1;
  let inputStrArr = inputStr.split('')
  while(++index < inputStrArr.length) {
     if (inputStrArr[index] !== char) {
        outputArr.push(inputStrArr[index]);
     }    
  }
  return outputArr.join('');
}

console.log(deleteChar('YOSUF', 'F'))

- Mohan Dere May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const deleteChar = (inputStr, char = '') => { 
  if (!char) {
    return false;
  }
  let outputArr = [], index = -1;
  let inputStrArr = inputStr.split('')
  while(++index < inputStrArr.length) {
     if (inputStrArr[index] !== char) {
        outputArr.push(inputStrArr[index]);
     }    
  }
  return outputArr.join('');
}

console.log(deleteChar('YOSUF', 'F'))

- Mohan Dere May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const deleteChar = (inputStr, char = '') => { 
  if (!char) {
    return false;
  }
  let outputArr = [], index = -1;
  let inputStrArr = inputStr.split('')
  while(++index < inputStrArr.length) {
     if (inputStrArr[index] !== char) {
        outputArr.push(inputStrArr[index]);
     }    
  }
  return outputArr.join('');
}

console.log(deleteChar('YOSUF', 'F'))

- Mohan Dere May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do it in-place and w/o using any additional storage. Actual one-pass implementation presented below does not use strlen, so allowing us to do it in a single pass:

void subst_all_chars(char* a)
{
    int fill_pos = -1;
    char subst = 'f';

    while (a[i++]){
        char chcur = a[i];
        if (chcur == subst){ // if the current is the sought char
            a[i] = 0;
            if (-1 == fill_pos)
                fill_pos = i;
        }
        else { // char that is OK to stay
            if (-1 == fill_pos)
                continue;
            else { // we have previous gap, so copy the current char to this position
                a[fill_pos] = chcur;
                a[i] = 0;
                ++fill_pos;
            }            
        }
    }
}

- ashot madatyan September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1
This is why we need to use our own brains. I read somewhere a while back that it's impossible in linear + in place (and that stuck with me).
+10 algo.

- bigphatkdawg September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I don't find any challenge here. I could do it as in-line alogorithm and that too in one iteration. Please let me know, if you have any input which breaks this program.

Is it really Amazon question? :)

Algorithm:
1. Find out input char position in given string
2. If char is not found then don't do anything. Just return
3. From that position (found in step1), keep copying all chars one position back(left) till end of the string.

void RemoveCharFromString(string& s, char c) {
    int i = 0;

    for(; (s[i] and toupper(s[i]) != toupper(c)); i++); // Iterate till we found the char or end of the string

    if(s[i] == '\0') // means input char is not found. Return input string as it is
        return;

    for(; s[i]; i++) // move rest of the string one char back
        s[i] = s[i+1];

    s = (s.substr(0, i-1)); // update input string by reducing last char. (other string size still be original size)

}


int main(int argc, char * argv[])
{
    string s("abcdef");
    cout << "After removing d from " << s; RemoveCharFromString(s, 'd'); cout << ", the result is: " << s << endl; // result: abcef
    s = "abcdef";
    cout << "After removing f from " << s; RemoveCharFromString(s, 'f'); cout << ", the result is: " << s << endl; // result: abcde
    s = "ABCDEF";
    cout << "After removing f from " << s; RemoveCharFromString(s, 'f'); cout << ", the result is: " << s << endl; // result: ABCDE
    s = "ABCDEF";
    cout << "After removing a from " << s; RemoveCharFromString(s, 'a'); cout << ", the result is: " << s << endl; // result: BCDE
    s = "ABCDEF";
    cout << "After removing h from " << s; RemoveCharFromString(s, 'h'); cout << ", the result is: " << s << endl; // result: ABCDEF
}

- Kallu September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if(!*s)
 return;
*q=s;
i=0;
while(*q)
{
  if(*q==Given_Char)
    q++;
  else
  {
     s[i++]=*q;
     q++;
  }
}
s[i]='\0';

- Anonymous September 24, 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