Amazon Interview Question for Quality Assurance Engineers


Country: India
Interview Type: In-Person




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

public void replaceCharacter(String s1, String s2, char c) {

StringBuilder builder = new StringBuilder( "", s1.length);

foreach(char toCheck in s1) {
	if( (int) c == (int) toCheck) {
		builder.append(s2)
	} else {
		builder.append(toCheck);
	}
}

- Nuruddin January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you cannot even use .length then I would do it inside a while loop and not a for loop. To improve the big O notation you can use recursing while dividing the array I guess. Not too sure about that.

- Nuruddin January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can do char1 == char2.

- Appy January 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

couldn't find any other way of reading char from console..

public class StringReplacement {

    public static void main(String[] args){

        Scanner scanner=new Scanner(System.in);

        char c=scanner.next().charAt(0);
        String str1= scanner.next();
        String str2= scanner.next();
        String newstr=new String();

        for(char ch:str1.toCharArray()){

            if(ch==c){
              newstr=newstr+str2;
            }

            else{
                newstr=newstr+ch;
            }
        }

        System.out.println(newstr);
    }
}

- geekgirl January 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void find_and_replace(char **S1, const char ch, const char* S2)
{
	char* str1 = strdup(*S1);
	int m=0;
	while(str1[m] != '\0')
	{
		if(str1[m] != ch)
		{
			m++;
		}
		else
		{
			str1 = realloc(str1, strlen(str1) + strlen(S2));
			memcpy(str1 + m + strlen(S2), str1 + m + 1, strlen(str1 + m + 1));
			strncpy(str1 + m, S2, strlen(S2));
			m += strlen(S2);
		}
	}
	*S1 = str1;
}

int main()
{
	char* S1 = "this is a test";
	char* S2 = "blah";
	char ch = 't';
	printf("%s\n", S1);
	find_and_replace(&S1, ch, S2);
	printf("%s\n", S1);
	return 0;
}

- Sagar January 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Represent the Strings as char[], scan the first string once, to count the number of times that char c is present, now calculate the length of the final string, and fill it from backwards O(2n) = O(n)

Example: abcdcdef and xyz are 2 strings, represent them as a char[], given char c, now scan first string, we find c twice, so the final string length is 8 - 1 + 3 - 1 + 3 = 12, create a new char[] and fill it from backwards with the first string, but when u hit c, fill it with the second string xyz instead of the c.

- captain_haddock February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReplaceCharFromString{

     public static void main(String []args){
         String str1 = "stauvwamna";
         char[] arr1 = str1.toCharArray();
         char str2 = 'X';
         char chr = 'a';
         for(int i=0;i<arr1.length;i++){
             if(arr1[i]==chr){
                  arr1[i]=str2;
                  System.out.println(arr1[i]);
             }else
             System.out.println(arr1[i]);
             
         }
     }
}

- naps April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Findandreplace {
public static void main(String args[]){
String name = "napendra";
String ch = "a";
String name1 = "SINGH";
String newName = name.replaceAll(ch, name1);
System.out.println(newName);
}

}

- napendra@canbrand.in July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String insertString(String subject, String inserter, char c1) {
String result = subject;
char[] array = result.toCharArray();
for (int i = 0; i < array.length; i++ ) {
if (array[i] == c1) {
result = new StringBuilder(result).replace(i, i+1, inserter).toString();
i+= (inserter.length()-1);
array = result.toCharArray();
}
}
return result;
}

- rigatoni November 23, 2017 | 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