## Amazon Interview Question for Quality Assurance Engineers

Country: United States
Interview Type: Phone Interview

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

public class Frequency {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};
//Array fr will store frequencies of element
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}

//Displays the frequency of each element present in array
System.out.println("---------------------------------------");
System.out.println(" Element | Frequency");
System.out.println("---------------------------------------");
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(" " + arr[i] + " | " + fr[i]);
}
System.out.println("----------------------------------------");
}}

Output :
----------------------------------------
Element | Frequency
----------------------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------------------

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

I think there is more optimum approach to this question and in the question your said string but in the solution you used array on numbers .
More optimum approach would be using a hashmap and make the character of string as key and its no of occurance as value.Hence you can find the count of each character.
Time complexity wound be o(n) and space complexity as o(1)

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

It will take o(n) space according to your approach because you are using hashmap.

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

# Python 3
from collections import Counter
def solve(arr):
arr1=Counter(arr)
print(arr1)

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

``````from collections import Counter
def frequency(array):
print(Counter(array))``````

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

public class Frequency{
public static void main(String[] args){
List<String> list = Arrays.asList("A", "B", "C", "A", "A", "B", "C", "A");

//create a SET to identify unique values
Set<String> unique = new HashSet<String>(list);

Map<String, Integer> result = new HashMap<String, Integer>();

for(String s : unique){
//result set is optional if you like to find out which is highest or lowest count
result.put(s, Collections.frequency(list, s));
System.out.println(s+" : "+Collections.frequency(list, s));
}

//if need to display for the specific item
System.out.println(result.get("A"));

}

}

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

Gave this a try - not sure how many syntax errors could be there. We could get the ASCII values and mark the counts at the index.

Class Findfreq
{
Public static void main(String [] args)
{
String str = “hellohowdoyoudo”;
Int [] arr = scanstring( string str );

For (k=0; k<arr.length ; k++)
{
system.out.println( str.charAt(i) + “ “ + arr(str.charAt(i) - ‘a’) );
}
}
Int[] scanstring (String str)
{
Int countarr [] = new int ;
Int index = 0;
For (int i = 0; i<str.length(); i++)
{
index = str.charAt(i) - ‘a’ ;
countarr(index) ++;
}
Return countarr;
}
}

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

Thought we could use ASCII values and mark the count at that index location in a separate array

``````Class Findfreq
{
Public static void main(String [] args)
{
String str = “hellohowdoyoudo”;
Int [] arr = scanstring( string str );

For (k=0; k<arr.length ; k++)
{
system.out.println( str.charAt(i) + “ “ + arr(str.charAt(i) - ‘a’) );
}
}
Int[] scanstring (String str)
{
Int countarr [] = new int ;
Int index = 0;
For (int i = 0; i<str.length(); i++)
{
index = str.charAt(i) - ‘a’ ;
countarr(index) ++;
}
Return countarr;
}
}``````

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

tell me plzz

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

Ruby: Solution 1 using array
Input
a=[11,22,33,22,44]

a.uniq.each do |k,v|
v = a.count(k)
puts "#{k} appears #{v} times"
end

Output:
11 appears 1 times
22 appears 2 times
33 appears 1 times
44 appears 1 times

Solution 2 using Hash
a=[11,22,33,22,44]
# make the hash default to 0 so that += will work correctly
b = Hash.new(0)

# iterate over the array, counting duplicate entries
a.each do |v|
b[v] += 1
end

b.each do |k, v|
puts "#{k} appears #{v} times"
end

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

I have used a hash map to find the occurrence of each number
Here is my solution:
public static void occurrenceArray(){

int[] aList = new int[]{1, 2, 8, 3, 2, 2, 2, 5, 1};
Arrays.sort(aList);
int count = 1;

HashMap<Integer,Integer> hmap = new HashMap<Integer, Integer>();

for(int i=0;i<aList.length-1;i++){

if(aList[i]==aList[i+1]){
count ++;
hmap.put(aList[i],count);
}
else{
count=1;
hmap.put(aList[i+1],count);
}
}
System.out.println("Hash Map" +hmap);
}

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.

### 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.