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

- anonymous August 25, 2020 | Flag Reply
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)

- Anonymous September 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- A D September 23, 2020 | Flag
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)

- mausam15171098 September 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- subhasispatra94 September 25, 2020 | 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