Amazon Interview Question for Quality Assurance Engineers


Country: United States




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

Java solution, complexity is O(n):

private String countDuplicate(String s) {
    StringBuilder result = new StringBuilder();

    // count letters
    int[] counter = new int[300];
    for (char c : s.toCharArray()) {
        counter[c]++;
    }

    // build answer
    boolean[] isProcessed = new boolean[300];
    for (char c : s.toCharArray()) {
        if (isProcessed[c]) {
            continue;
        }

        isProcessed[c] = true;

        result.append(c).append(counter[c]);
    }

    return result.toString();
}

- techinterviewquestion.com May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

private static void calculate(char[] charArray) {
		LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
		for (char c : charArray) {
			if (!map.containsKey(c)) {
				map.put(c, 1);
			} else {
				map.put(c, map.get(c) + 1);
			}
		}
		StringBuilder sb = new StringBuilder();
		for (Entry<Character, Integer> entry : map.entrySet()) {
			sb.append(entry.getKey());
			sb.append(entry.getValue());
		}
		System.out.println(sb.toString());
	}

- Isaac Pérez Estrada May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void calculate(char[] charArray) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
StringBuilder sb = new StringBuilder();
for (Entry<Character, Integer> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append(entry.getValue());
}
System.out.println(sb.toString());
}

- Isaac Pérez Estrada May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* The idea is to store characters against their frequencies in a Hash Table, and then to pull them out. Complexity: Space O(N), Time O(N). */

public static String writeRepetitions(String str){
int len = str.length();
if (len == 0) return str;

HashMap<Character,Integer> hm = new HashMap<>();
for (int i = 0; i<len; i++){
char ch = str.charAt(i);
if (hm.containsKey(ch)){
hm.put(ch, hm.get(ch)+1);
} else hm.put(ch, 1);
}
int j=0;
StringBuilder sb = new StringBuilder();
while (!hm.isEmpty()){
char c = str.charAt(j);
if (hm.containsKey(c)){
sb.append(c);
sb.append(hm.get(c));
hm.remove(c);
}
j++;
}
return sb.toString();

}

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

public class Solution {
	public static void main(String[] args) {
		String s = "abbaccdbac";
		
		printOccurencesChars(s);
	}

	// assuming only ASCII chars in string.
	private static void printOccurencesChars(String s) {

		int[] count = new int[256];
		for (int i = 0; i < count.length; i++) {
			count[i] = 0;
		}
		char[] c = s.toCharArray();
		int charVal = -1;
		for (int i = 0; i < c.length; i++) {
			charVal = (int) c[i];
			count[charVal]++;
		}
		
		StringBuilder sb = new StringBuilder();
		int charCount = -1;
		for (int i = 0; i < count.length; i++) {
			charCount = count[i];
			if (charCount > 0) {
				sb.append((char)i + "" + charCount);
			}			
		}
		
		System.out.println(sb.toString());
	}
}

- guilhebl May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Objects;

public class StringUtil {
	// Assumed only ASCII character string
	private static final int ASCII = 255;

	public static String countChars(String str) {
		if (Objects.isNull(str)) {
			return str;
		}

		char[] chars = str.toCharArray();

		int[] counter = new int[ASCII];
		boolean[] isVisited = new boolean[ASCII];

		for (char c : chars) {
			counter[c]++;
		}

		StringBuilder builder = new StringBuilder();

		for (char c : chars) {
			if (isVisited[c]) {
				continue;
			}

			isVisited[c] = true;
			builder.append(c).append(counter[c]);
		}
		return builder.toString();

	}
}

- harikrishna553 May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CountDuplicate {

	public static void main(String[] args) {
		String str = "abcdbababac";
		int l =str.length();
		int i=0;
		String output="";
		int count[] = new int[26];
		 
		if(l==0)
		{
			System.out.println("The string is empty");
			System.exit(0);
		}
		
		for(i=0;i<l;i++)
		{
			count[str.charAt(i)-'a']++;
		}
		
		for(i=0;i<26;i++)
		{
			if(count[i]!=0)
			output = output + (char)(i+'a') + count[i];
		}
		
		System.out.println(output);

	}

}

- Heba May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DuplicateCharsInString {
	public static void main(String[] args) {
		String str = "aabbacklccl";
		int[] count = new int[26];
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			int reminder = ((int) ch) % 97;
			count[reminder] = count[reminder] + 1;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < count.length; i++) {
			if (count[i] > 0) {
				int ch =  97 + i;
				sb.append(Character.toString((char) ch)+count[i]);
			}
		}
		System.out.println(sb.toString());
	}
}

- anand May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello

Thanks for the solution provided but while going through the code i am not able to understand the belo statement.

int reminder = ((int) ch) % 97;

it would be great if you can explain this?

- Prabhat January 08, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello

Thanks for the solution provided. while going through the code i am not able to understand the below statement
int reminder = ((int) ch) % 97;
It would be great if you can explain

- prabhat961 January 08, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

String t="abbaccdbac";
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		for(int i=0;i<t.length();i++)
		{
			String tmp = t.substring(i,i+1);
			if(!map.containsKey(tmp))
			{
				map.put(tmp,1);
			}
			else
			{
				map.put(tmp, map.get(tmp)+1);
			}
		}
		
		for(Entry<String, Integer> tm:  map.entrySet())
		{
			System.out.println(tm.getKey()+"---"+tm.getValue());
		}

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

String t="abbaccdbac";
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		for(int i=0;i<t.length();i++)
		{
			String tmp = t.substring(i,i+1);
			if(!map.containsKey(tmp))
			{
				map.put(tmp,1);
			}
			else
			{
				map.put(tmp, map.get(tmp)+1);
			}
		}
		
		for(Entry<String, Integer> tm:  map.entrySet())
		{
			System.out.println(tm.getKey()+"---"+tm.getValue());
		}

- Harsh May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String reversedUnique(String input)
	{
		StringBuilder sb = new StringBuilder();
		
		for(int i=input.length()-1;i>0;i--)
		{
			if(!sb.toString().contains(input.substring(i, i+1))){
				
				sb.append(input.substring(i, i+1));
			}
		}
		return sb.toString();
	}

- harshmighlani May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CountDuplicateChars {
	public static void main(String[] args) {
		String str = "aabbacklccl";
		int[] count = new int[26];
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			int reminder = ((int) ch) % 97;
			count[reminder] = count[reminder] + 1;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < count.length; i++) {
			if (count[i] > 0) {
				int ch =  97 + i;
				sb.append(Character.toString((char) ch)+count[i]);
			}
		}
		System.out.println(sb.toString());
	}
}

- anand June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python 3:

def orderly_count(let_string):
	mapping = {}

	for item in let_string:
		mapping[item] = mapping.get(item,0)+1

	result = ''

	for item in let_string:
		if item not in result:
			result += item + str(mapping[item])

	return result

print(orderly_count('abbaccdbac')) #a3b3c3d1

print(orderly_count('bbddeeeaaccbbdef')) #b4d3e4a2c2f1

- Enigma8ic June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Solution using HashMap:

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

String s="abbcaccbbaa";

char []charArray=s.toCharArray();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (char t : charArray) {
if (!map.containsKey(t)) {
map.put(t, 1);
} else {
map.put(t, map.get(t) + 1);
}
}

for (Map.Entry obj:map.entrySet()) {
System.out.print(obj.getKey() +""+obj.getValue());
}

}

- sahilsoni960 June 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why do you have to make the solution so complicated?

public static String compressString (String str) {
        if(str == null || str.length() <= 1) {
            return str;
        }


        String compStr = "";
        int num = 1;
        char cc = str.charAt(0);

        for(int cntr = 1; cntr < str.length(); cntr++) {

            if(cc == str.charAt(cntr)) {
                num ++;
            } else {
                compStr += cc + String.valueOf(num);
                num = 1;
                cc = str.charAt(cntr);
            }
        }

        return compStr + cc + String.valueOf(num);
    }

- redssoft July 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#--Count Dup values Ruby
a = String.new("aabccdd")
b = ""
l = a.length
for i in 0...l
	chr = a[i]
	if !(b.include? chr)
		b << chr
	end	
end
puts b
bl = b.length
for i in 0...bl
	cnt = 0
	for j in 0...l
		if b[i]==a[j]
			cnt = cnt +1
		end
	end
	puts "#{b[i]} #{cnt}"
end

- sonyjames9 July 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets assume if input string is "aaa333b9999bbbbbccc44" than ?

- rathor.rajeev August 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class count_of_duplicateChar {

	public static void main(String[] args) {
		// Print the count of duplicate char in a given string in same order.
		// Ex: Input- 'abbaccdbac', Output- 'a3b3c3d1'

		String str = "abbaccdbac";

		String[] myChar = str.split("");
		int i = 0;

		while (str.length()>0) {

			int count = 0;
			String repChar = myChar[i];
			String finalChar="";
			for (int j = 0; j <= myChar.length - 1; j++) {

				if (repChar.equalsIgnoreCase(myChar[j])) {
					count++;
				}
			}
			str=str.replaceAll(repChar,"");
			myChar =(str.split(""));
			finalChar = repChar+count;
			System.out.print(finalChar);
		}

	}

}

- chaitu95test August 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String val = "abbacca";
String a="a";String b="b";String c="c";
int acount=0;
int bcount=0;
int ccount=0;
for (int i=0; i<val.length(); i++)
{
if(val.charAt(i)=='a')
{
acount++;
System.out.println("Acount: "+acount);
}else if(val.charAt(i)=='b')
{
bcount++;
System.out.println("Bcount: "+bcount);
}else
{
ccount++;
System.out.println("Ccount: "+ccount);
}
System.out.println(a+acount+b+bcount+c+ccount);
}

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

String s="abbacca";
	    int count=0;
	    int len=s.length();
	    HashMap<Character,Integer> map=new HashMap<Character,Integer>(); 
	    
	    for(int i=0;i<len;i++)
	    {
	    	if(map.containsKey(s.charAt(i)))
	    		
	    	{
	    	 count=	map.get(s.charAt(i));
	    	 count++;
	    	 map.put(s.charAt(i), count);
	    		
	    	}
	    	else
	    	{
	    		map.put(s.charAt(i), 1);
	    	}
	    }
	    for(Entry e:map.entrySet())
	    {
	    	System.out.println(e.getKey()+":"+e.getValue());
	    }

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

public void getCount(String s){
		char[] carr = s.toCharArray();
		LinkedHashMap<Character,Integer> lmap = new LinkedHashMap<>();
		for(Character c : carr){
			if(lmap.containsKey(c)){
				lmap.put(c, lmap.get(c)+1);
			}
			else{
				lmap.put(c, 1);
			}
		}
		for(Entry<Character, Integer> entry : lmap.entrySet()){
			System.out.print(entry.getKey()+""+entry.getValue());
		}
	}

- anonymousNg November 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python Code using Hash Map==>

mainString = 'aaabbbbzyyzba'
dictMap={}
temp=' '
for i in mainArray:
dictMap[i]=dictMap.get(i,0) + 1

for key, value in dictMap.items():
temp += str(key) + str(value)
print(temp)

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

mainArray = 'aaabbbbzyyzba'
    dictMap={}
    temp=''
    for i in mainArray:
        dictMap[i]=dictMap.get(i,0) + 1 # Inserting each element & count in dict. Time=O(1)
    for key, value in dictMap.items():
        temp+=str(key)+str(value)
    print(temp)

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

mainArray = 'aaabbbbzyyzba'
    dictMap={}
    temp=''
    for i in mainArray:
        dictMap[i]=dictMap.get(i,0) + 1 # Inserting each element & count in dict. Time=O(1)
    for key, value in dictMap.items():
        temp+=str(key)+str(value)
    print(temp)

- shanky20889 May 16, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

def charDup(string):

    char = []
    charCount = []

    for i in range(len(string)):
        if string[i] not in char:
            char.append(string[i])
            counter = 0
            for j in range(len(string)):
                if string[i] == string[j]:
                    counter = counter +1
            counter = str(counter)
            charCount.append(counter)
    outputStr = ''
    for k in range(len(char)):
        outputStr = outputStr + char[k]+ charCount[k]
    return (outputStr)


a = 'aabbddbbccaad'
print (charDup(a))

- Prashant May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void countDuplicates(char[] charArray) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : charArray) {
  if (!map.containsKey(c)) {
     map.put(c, 1);
  } else {
     map.put(c, map.get(c) + 1);
 }
}

for (char c:charArray) {	
    System.out.print(c+map.get(c));
}

}
Time Complexity :- O(n), Space Complexity: O(n)

- yashraithathaCE May 23, 2016 | 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