Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

An efficient O(n) solution without using any additional data structure or buffer.

public static String removeDuplicate(String in)
{
	StringBuilder sb = new StringBuilder();
	int flag = 0;
	
	in = in.toLower();
	for(int i=0; i<in.length(); i++)
	{
		int mask = 1<<(int)in.charAt(i);
		
		if(! flag&mask) //char not seen so far
			sb.append(in.charAt(i));
		
		flag |= mask;
	}
    return sb.toString();
}

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

Did you mean.. Suppose 'fool' is given as input string, you have to convert this to 'fol'

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

yes

- AVK September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

AVK, you had an interview on Saturday with Amazon, with 6 of these "easy" questions?

- bigphatkdawg September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

This can be solved by o(n) by taking an extra array of size 256 to count the frequency of the character.
Please find the working function below.
Input : string, length,
Output : string

char* removeDupFromString(char input[], int len)
{
        char output[len];
        int ASCII[256] = {0};
        int i, j;
        for(i=0, j=0; i<len; i++)
        {
                if(ASCII[input[i]] == 0)
                {
                        output[j++] = input[i];
                        ASCII[input[i]] += 1;
                }
                else
                        ASCII[input[i]] += 1;
        }
        output[j] = '\0';
        return output;
}

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

1) Why are you counting "+=1" when it is a boolean problem (yes/no have we seen the character before)?
2) What do you have the same "ASCII[ ]+=1" part inside if and else part? It is common to both, so move it ouside.

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

char* removeDupFromString(char input[], int len)
{
        char output[len]; <== Created on stack
        int ASCII[256] = {0};
        int i, j;
        for(i=0, j=0; i<len; i++)
        {
                if(ASCII[input[i]] == 0)
                {
                        output[j++] = input[i];
                        ASCII[input[i]] += 1;  <== same
                }
                else
                        ASCII[input[i]] += 1;  <== same
        }
        output[j] = '\0'; <=Will this fit if string has no duplicates?
        return output; <== Pointer to stack returned
}

What if the string has no duplicates?

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

@bigphatkdawg
I m counting the frequency of each word is which is not required for this problem. so we can use as this array as boolean but in 'c' we don't have this datatype so i used int.
Yest "ASCII[ ]+=1" can be put outside the if statement. Like

if(ASCII[input[i]] == 0)
                        output[j++] = input[i];
 ASCII[input[i]] += 1;

And if string has no duplicate char then same string will be returned back.

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

Fixing your solution (what you meant):

if C, "typedef enum {false=0,true} bool;" before this code

char* removeDupFromString(char input[], int len)
{
       char *output=malloc(len+1); <=n+1 space from HEAP
	if(!output) exit(1);

        bool ASCII[256] = {0};
        int i, j;
        for(i=0, j=0; i<len; i++)
        {
                if( !ASCII[input[i]] )
                {
                        output[j++] = input[i];
                        ASCII[input[i]]=true;
                }
        }
        output[j] = '\0'; 
        return output;  //better if you strcpy back to original array
}

Find the remaining bugs...

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

a caller will call your funciton like this in C
removeDup(string, strlen(string));

then it will seg fault here (or corrupt memory):
output[j] = '\0';

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

SIMPLE CONCEPT:

for(i=0;i<str[i].length();i++)
{
if(str[i]!=str[i+1])//##AVOIDING DUPLICATES INPLACE IN str[i]##
{
str[i++]=str1[i++]//copying the unique elements of str[i] into new array str1[i]
}
printf("the new array is %s",str1[i])
}

- Goli gundu September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

An efficient O(n) solution without using any additional data structure or buffer.

public static String removeDuplicate(String in){
	StringBuilder sb = new StringBuilder();
	int flag = 0;
	
	in = in.toLower();
	for(int i=0; i<in.length(); i++){
		int mask = 1<<(int)in.charAt(i);
		
		if(! flag&mask) //char not seen so far
			sb.append(in.charAt(i));
		
		flag |= mask;
	}
      return sb.toString();
}

- zahidbuet106 December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String replaceDuplicates(String s_) {
		char[] c = s_.toCharArray();
		Map<Character, Boolean> v = new HashMap<>();
		for (int i = 0; i < c.length; i++) {
			Character a = Character.valueOf(c[i]);
			if (v.containsKey(a))
				v.put(a, true);
			else
				v.put(a, false);
		}
		String noDups = s_;
		Character empty = '\0';
		for (Character a : v.keySet()) {
			if (v.get(a) == false)
				continue;
			noDups = noDups.replace(a.toString(), "");
		}
		return noDups;
	}

- Gravometer September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/// O(n), without extra memory
#include <iostream>

using namespace std;

int main()
{
std::string str = "aaaabbbbcdddeee";
std::string::iterator itr1=str.begin();
std::string::iterator itr2=str.begin();
for(; itr2 != str.end(); ++itr2)
{
if(*itr1 != *itr2)
{
*(++itr1) = *itr2;
}
}
str.resize(itr1 +1 - str.begin());
cout << str;;
}

- Pankaj September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string removeDuplicate(string s)
{
    string returnS = "";
    bool ASCII[256] = {0};
    for(int i=0; i<s.length(); i++)
    {
        if(!ASCII[s[i]])
        {
            returnS += s[i];
            ASCII[s[i]] = 1;
        }
    }
    return returnS;
}

- evoamac September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveDuplicate(char *s)
{
if(!*s)
  return;
int *hash=new int[256]//assuming ANSI
memset(hash,0,sizeof(int)*256);
char *q=s;
while(*q)
{
 if(hash[*q]==0)
 {
   hash[*q]++;
   s[i++]=*q;
   q++;
 }
 else
  q++;
}
s[i]='\0';
delete [] hash;
}
for()

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

public static void removeDublicate() {

		String str = "aaabbddffrrg88gcc8c";
		String result = "";
		int index;
		boolean[] visitedIndexs = new boolean[str.length()];
		result = "";
		char current;
		for (int i = 0; i < str.length(); i++) {
			current = str.charAt(i);
			index = i;
			if (visitedIndexs[i] == true)
				continue;

			while (true) {
				visitedIndexs[index] = true;
				index = str.indexOf(current, index + 1);
				if (index == -1)
					break;
			}
			result = result + current;

		}
		System.out.println(str);
		System.out.println(result);

	}

- Bannie September 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution using pointers. Start from the end of the array to copy the optimized array.

#include<iostream>
using namespace std;


void copy_rest(char* index1, char* index2) {

        char* temp = index1+1;
        int i = 2;
        while(*temp != '\0') {
                *(index2 +i) = *temp++;
                i++;
        }
        *(index2+i) = '\0';
}

int main (int argc, const char* argv[]) {
        char input[] = "abbbccd";

        char* index1 = input+sizeof(input)-1;
        char* index2 = index1-1;

        while(index2 >= input) {
                if(*index2 != *index1) {
                        copy_rest(index1,index2);
                        index1 = index2--;
                } else {
                        index2--;
                }
        }
        if(index1 != index2) {
                copy_rest(index1,index2);
        }
        cout<<input;

}

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

***** the poster ...if i catch u i ll **** u..

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