Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

char hashtable[256] ={0}
void strRemove(char *p , char *remove);
void hashInit(char *remove);
bool hashfind(char value);

//set the remove string in hashtable.
void hashInit()
{
    while('\0' != *remove)
    {
         hashtable[*remove] = *remove;
         remove++;
    }
}

//If char present then retuen true else return false.
bool hashfine(char value)
{
    if('\0' != hashtable[tolower(value)])
    {
         return true;
    }
    return false;
}

void strRemove(char *p , char *remove)
{
    hashinit(remove);
    int stringlen = 0, j= 0;
    char *source = p;
    while('\0' != *source)
    {
         if(!hashfine(*source))
         {
              p[j++] = *source;
          }
         source++;
         stringlen++;
    } 
    memset(p+j, 0, stringlen -(j+1));
}

- Biswaranjan January 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your program wont compile . See the definition of your hashinit function. If you are writing proper code ( not pseudocode) , make sure you write compiled code.

- vivekh February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This line

memset(p+j, 0, stringlen -(j+1));

is not required.
Just add

p[j] = '\0';

instead of this.

- Aleksey.M January 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;

public class RemoveDuplicates {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(removeDuplicates("amazon development center", "aenr"));
}

public static char[] removeDuplicates(char[] source, char[] remove){
HashSet<Character> set = new HashSet<Character>();

for(int i=0 ; i < remove.length ; i++)
set.add(remove[i]);


int copyIndex =0;

for(int i=0 ; i < source.length ; i++){
if(!set.contains(source[i]))
source[copyIndex++] = source[i];
}

return new String(source).substring(0, copyIndex).toCharArray();
}


public static char[] removeDuplicates(String source, String remove){

if(source != null && remove != null)
return removeDuplicates(source.toCharArray(), remove.toCharArray());

if(source != null)
return source.toCharArray();

return null;
}

}

- Navneel January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash set is not needed , it's needs a big data structure in memory. As qn is to support only 256 int, we can simply create an array of 256.
int remove[256] = {0};
iterate through second parameter and make each char to 1;
like if "A" is found,
then remove['a' ] = 1 ;

while doing the search we can do a simple search -- remove['a'] is 0 or 1, this will give the most efficient search.

- Tushar K Gupta January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My logic is correct but my code is not running properly...pls help me
#include<stdio.h>
#include<conio.h>
void strRemove(char *source, char *remove )
{ int Remov[256];
int i;
for(i=0;i<256;i++)
Remov[i]=-1;
for(i=0;remove[i]!='\0';i++)
{
if(Remov[remove[i]]==-1)
{
Remov[remove[i]]=1;
}
}

for(int j=0;((Remov[source[j]]==-1) && (source[j]!='\0'));j++)
{
printf("%c",source[j]);
}
}
int main()
{

strRemove("amazon development center","aenr");
getch();
}

- saurabh saxena(vit) January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Saurabh saxena your for loop for printing the deleted string terminates on the very first time as Remov['a'] is 1.

so try

for(j=0; source[j]!='\0' ; j++)
{ 
  if(Remov[source[j]]==1)
      continue;
  printf("%c",source[j]);
}

- cp February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int hashtable[256] ={0};
void strRemove(char *p , char *remove);
void hashInit(char *remove);
bool hashfind(char value);



bool hashfind(char value)
{
if(0 != hashtable[tolower(value)])
{
hashtable[tolower(value)]++;
return 1;
}
else
{
hashtable[tolower(value)]++;
return 0;
}
}


void strRemove(char *p , char *remove)
{
int newlen =0, tempIndex = 0;
char *tp = p,*tp2 = p;
printf("\n The Input String is : %s \n",p);
while('\0' != *tp)
{
if(0 == hashfind(*tp))
{
*tp2 = *tp;
tempIndex++;
tp2++;
}
tp++;
}
p[tempIndex] = '\0';
printf("\n The String is : %s and len : %d\n",p,tempIndex);
}

- Vidyasagar Grandhi January 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;


public class RemoveDupli {

public static void main(String[]args){

System.out.println(removeDuplicates("amazon development center", "aenr"));
}

public static String removeDuplicates(String source, String remove){
int copyIndex = 0;
if(source!=null && remove!=null)
{
char[]source1 = source.toCharArray();
char[]remove1 = remove.toCharArray();

HashSet<Character> set = new HashSet<Character>();

for(int i= 0; i<remove1.length; i++)
set.add(remove1[i]);



for(int i=0; i<source1.length; i++) {

if(!set.contains(source1[i]))
source1[copyIndex++] = source1[i];}

return new String(source1).substring(0, copyIndex);
}

return null;
}
}

- puneet rana February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can also use a boolean array of size 256 which is true at all positions of the characters that are to be removed and false otherwise.
(see nobrainer .co .cc)

- Anony February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution can be optimised for space by having only 27 char array , instead of 256. here is the compiled and tested code

/* this function initializes the hash table */

void init_hash( char *remove, char *p){
    int i;
    memset(p,'0',26);
    p[26] = '\0';
    while(*remove){
        p[*remove - 97] = *remove;
        remove++;
    }

}


/* this function checks if hash search is successful or not */
int is_hash(char c, char* p){
    if(p[c-97] == c)
        return 1;
    else
        return 0;
}


/*this functions remove all the characters in remove string from 
 * the  source string 
 * eg : source : aabbcc
 *      remove : ab
 *      output : cc */

void strRemove(char *source , char *remove){

    char *p = source;
    char s[27];

    int i;

    init_hash(remove,s);
    for( i=0;i<strlen(p);i++){

        if(is_hash(p[i],s))
            p[i]=' ';
        else
            printf("%c",p[i]);

    }

printf("\n");

}

now from main


char source[] = "amazon development centre";
char remove[] ="aenr";

strRemove(source,remove);

- vivekh February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <bitset>
using namespace std;

void strRemove(char *source, char *remove)
{
	if ( !source || !remove )
		return;

	bitset<256> hash;

	while(*remove)
		hash.set(*remove++);

	int i = 0, j = 0;
	while(source[j])
	{
		if ( !hash.test(source[j]) )
			source[i++] = source[j];
		j++;
	}
	source[i] = '\0';
}

int main()
{
	char  str[] = "amazon development centre";
	char  remove[] = "aenr";
	strRemove(str,remove);
	cout<<str;
	return 0;

}

- Sunny_Boy February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void strRemove(char * source,char * remove)
{
 char *p,*temp,*last;
 last=&source[strlen(source)];
 p=source;
 temp=remove;
 while(*p)
 {
  while(*temp)
  {
   if(*p==*temp)
   {
   p=memcpy(p,p+1,last-p)-1; 
   break;            
   } 
   temp++;           
  }
  temp=remove;
  p++;
 }      
}

- Anonymous March 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

heyy can u ealborate
p=memcpy(p,p+1,last-p)-1;
line am not able co-relate.
plz help with example. :)

- hk588853 September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package stringss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * write a function strRemove(char *source, char *remove )
This function will delete all the chars that exist in string remove from array source, 
number of iteration should be only 1. Make the searching efficient.
Example 
("amazon development center", "aenr")
"mzo dvlpmt ct".
Criteria - First parameter should be modified , no need to create an extra string.

 * @author Musi
 *
 */
public class DeletionStr {
	
		String myStr;
		String myRemover;
		List   removers;
	
	DeletionStr(String s, String r){
	
		myStr =s;
		myRemover=r;
		removers = new ArrayList();
		
	}
	
	/**
	 * 
	 */
	public void deleteString() {
		
		char remover[] = myRemover.toCharArray();
		int l = remover.length;
		
		for(int i=0;i<l;i++) {
			removers.add(remover[i]);
		}
		
		int len= myStr.length();
		char source[] = myStr.toCharArray();
		System.out.println(source);
		int j=0;
		for(int i=0;i<len;i++) {
		if( search(source[i]))
			source[j] = source[i];	
		else 
			source[j++] =source[i];
		
		//  System.out.print(source[j]);
		
		}
		
		
		for(int i=j;i<len;i++)
			source[i]=' ';
		
					System.out.println();
			//		System.out.println(myStr);
					System.out.println(source);
	}
	
	/**
	 * 
	 * @param c
	 * @return
	 */
	
	public boolean search(char c) {
	
		if(removers.contains(c))
		 return true;
		else 
			return false;
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
	
		DeletionStr str = new DeletionStr("amazon development center", "aenr");
		str.deleteString();
	}
}

- Musi Ali May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void strrem(char *str,char *rem)
{
int a[128]={0};
char *p=str;
char *q=str;
int j=0;
while(*rem!=0)
{
a[*rem]=1;
rem++;
}

q=p;
while(*p!=0)
{
if(a[*p-0])
{
if(q==str)
q=p;


}
else
{
*q=*p;
q++;
j++;
}
p++;
}
memset(str+j, 0, strlen(str) -(j+1));


}

int main(int argc, _TCHAR* argv[])
{
char str[]="amazon development center";
char *p="aenr";
strrem(str,p);
printf("reversed string=%s\n",str);

}

- Anuradha July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void strrem(char* str, char* rem)
{
	if(str == NULL || rem == NULL) return;

	int count = 0;
	for(int = 0; str[i] != '\0'; ++i)
	{
		bool found = false;
		for(int j = 0; rem[j] != '\0'; ++j)
		{
			if(str[i] == rem[j])
			{
				++count;	
				found = true;
			}
		}

		if(!found)
		{
			str[i - count] = str[i];
		}
	}
}

- Anonymous July 26, 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