Amazon Interview Question for Quality Assurance Engineers

Country: India
Interview Type: In-Person

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

Use a Hashmap and traverse the array storing Key = element, Value = Count of occurrences of element. O(n) . Pick any desired element - Key and retrieve number of occurrences. O(1).

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

Agree with above comment.. Hashtable should be the most efficient way to do this in my opinion...

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

If the input array is sorted it can be done in O(log n) time complexity and O(1) auxiliary space complexity. Use Binary Search twice to find start and end index of the element. No. of occurrence = endIndex - startIndex + 1.

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

``````var arr = [1,2,3,2,3,4,5,2,2,4,5,1,2];
var output = arr.reduce(function (memo, current, index, arr) {

if (memo[current]) {
memo[current] += 1;
} else {
memo[current] = 1
}

return memo;
}, {});``````

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

int[] array = {1, 3, 10, 3, 1, 10, 9, 10, 12, 1};

if (array == null || array.length == 0) {
System.out.println("Not Possible");
System.exit(0);
}

Map<Integer, Integer> mp = new HashMap<Integer, Integer>();

for (int i = 0; i < array.length; i++) {
if (!mp.containsKey(array[i])) {
mp.put(array[i], 1);
} else {
int c = mp.get(array[i]);
c += 1;
mp.put(array[i], c);
}
}

for (Iterator<Integer> iterator = mp.keySet().iterator(); iterator.hasNext();) {
Integer type = iterator.next();
System.out.println(type + "\t" + mp.get(type));

}

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

int[] array = {1, 3, 10, 3, 1, 10, 9, 10, 12, 1};

if (array == null || array.length == 0) {
System.out.println("Not Possible");
System.exit(0);
}

Map<Integer, Integer> mp = new HashMap<Integer, Integer>();

for (int i = 0; i < array.length; i++) {
if (!mp.containsKey(array[i])) {
mp.put(array[i], 1);
} else {
int c = mp.get(array[i]);
c += 1;
mp.put(array[i], c);
}
}

for (Iterator<Integer> iterator = mp.keySet().iterator(); iterator.hasNext();) {
Integer type = iterator.next();
System.out.println(type + "\t" + mp.get(type));

}

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

``````private void PrintElementOccurence(int[] arrayInts)
{

var ElementArray = new int[256];
foreach (var arrayInt in arrayInts)
{
ElementArray[(int)Convert.ToChar(arrayInt.ToString(CultureInfo.InvariantCulture))]++;
}
for (int i = 0; i < ElementArray.Length; i++)
{
if (ElementArray[i] > 0)
{
Console.WriteLine("Occurence of {0} is {1}", Encoding.ASCII.GetChars(new[] { Convert.ToByte(i) })[0], ElementArray[i]);
}
}``````

}

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

``````package core_java_Programs;

import java.util.HashMap;
import java.util.Set;

public class DuplicateNumbers {

public static void main(String[] args) {
int[]a={1,2,1,5,9,5,8,3,5,1,6,1,8,9};
HashMap<Integer,Integer> map = new HashMap<>();
for (int Num : a) {
if(map.containsKey(Num)){
map.put(Num,(map.get(Num)+1));
}
else{
map.put(Num,1);
}
}
Set<Integer> set = map.keySet();
for (Integer integer : set) {
System.out.println("The Number "+integer+" occurs "+map.get(integer));
}
System.out.println(map.toString());
}

}``````

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

package core_java_Programs;

import java.util.HashMap;
import java.util.Set;

public class DuplicateNumbers {

public static void main(String[] args) {
int[]a={1,2,1,5,9,5,8,3,5,1,6,1,8,9};
HashMap<Integer,Integer> map = new HashMap<>();
for (int Num : a) {
if(map.containsKey(Num)){
map.put(Num,(map.get(Num)+1));
}
else{
map.put(Num,1);
}
}
Set<Integer> set = map.keySet();
for (Integer integer : set) {
System.out.println("The Number "+integer+" occurs "+map.get(integer));
}
System.out.println(map.toString());
}

}

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

``````import java.util.HashMap;
import java.util.Map;

public class CountOccuranceInArray {

public static void main(String[] args) {
int arr[] = {2,4,5,7,2,7,8,3,1,5,3,5,6,9,2,1};
Map<Integer,Integer> occuranceMap = new HashMap<>();
for(int i:arr){
if(!occuranceMap.containsKey(i)){
occuranceMap.put(i,1);
}
else{
int c = occuranceMap.get(i);
occuranceMap.put(i, ++c);
}
}

for(Map.Entry<Integer, Integer> entry : occuranceMap.entrySet()){
System.out.println("Occurance of "+entry.getKey()+" is "+entry.getValue());
}
}

}``````

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

C# solution by using Dictionary

``````private static Dictionary<int, int> CountNumberOfOccurrences(int[] input)
{
Dictionary<int, int> numberOfOccurrences = new Dictionary<int, int>();

foreach(int element in input)
{
if(numberOfOccurrences.ContainsKey(element))
numberOfOccurrences[element] += 1;
else
}

return numberOfOccurrences;
}``````

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

``````static void Main(string[] args)
{
// Count the number of occurrence of an element in an array

char[] charArray = { 'a','a','b', 'c','a','B','b','D'};

var list = new List<KeyValuePair<char, int>>();
List<char> listItem = null;
int counter = 0;
foreach (var element in charArray)
{
listItem = (from kvp in list select kvp.Key).ToList();

if (!listItem.Contains(element))
{
}
else {

for(int i=0; i< list.Count; i++)
{
if(list[i].Key == element)
{
counter = list[i].Value + 1;
list[i] = new KeyValuePair<char, int>(element, counter);
}
}
}

}

for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("Array Element:{0}, Number of Occurrences:{1}", list[i].Key, list[i].Value);
}``````

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

``````static void CountNumberOccurenceElementsInArray(int [] arr)
{
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (var a in arr)
{
if (!dic.Keys.Contains(a))
{
}
else
{
dic[a]++;
}
}
foreach (var k in dic.Keys)
{
Console.WriteLine("Number: {0} occurred: {1}", k, dic[k]);
}
Console.WriteLine();
}``````

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

``````static void Main(string[] args)
{
int [] arr = new int[] { 1, 2, 3, 2, 3, 4, 5, 2, 2, 4, 5, 1, 2 };
CountNumberOccurenceElementsInArray(arr);

char[] charArray = { 'a', 'a', 'b', 'c', 'a', 'B', 'b', 'D' };
CountNumberOccurenceElementsInArray(charArray);

}

static void CountNumberOccurenceElementsInArray<T>(T [] arr)
{
Dictionary<T,int> dic = new Dictionary<T, int>();
foreach (var a in arr)
{
if (!dic.ContainsKey(a))
{
}
else
{
dic[a]++;
}
}
foreach (var k in dic.Keys)
{
Console.WriteLine("Number: {0} occurred: {1}", k, dic[k]);
}
Console.WriteLine();
}``````

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

Its written in C

``````int arr[]={10,9,8,7,9,10,2,1};

int occur(int dat)
{
int i=0,count=0;
while(arr[i]!=0x00)
{
if(arr[i]==dat)
count++;
i++;
}
return count;
}``````

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

public class Problem3 {

public HashMap<Integer,Integer> sortCount(int array[]){

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

for(int i:array){

if(numInfo.containsKey(i)){

int count=numInfo.get(i);
count++;

numInfo.remove(i);
numInfo.put(i,count);

}
else if(numInfo.isEmpty() || numInfo.containsKey(i)==false){
numInfo.put(i, 1);
}

}

return numInfo;

}

public static void main(String ... ar){

Problem3 problem3=new Problem3();

int []array={1,2,4,3,1,2,4,5,3,2,1,0,3,4,5};

HashMap<Integer, Integer>nums=problem3.sortCount(array);

for(Map.Entry<Integer, Integer>item:nums.entrySet()){

System.out.println(item.getKey()+" : "+item.getValue());
}

}

}

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

package com.bc.code;

import java.util.HashMap;
import java.util.Scanner;

public class CountNumberOccr {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
}

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

for(int i = 0; i < n; i++) {
Integer k = map.get(a[i]);
if(k == null) {
map.put(a[i], 1);
} else {
map.put(a[i], k.intValue() + 1);
}
}

for(Integer i : map.keySet()) {
System.out.println(i.intValue() + " " + map.get(i));
}

in.close();
}

}

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

``````package com.bc.code;

import java.util.HashMap;
import java.util.Scanner;

public class CountNumberOccr {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
}

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

for(int i = 0; i < n; i++) {
Integer k = map.get(a[i]);
if(k == null) {
map.put(a[i], 1);
} else {
map.put(a[i], k.intValue() + 1);
}
}

for(Integer i : map.keySet()) {
System.out.println(i.intValue() + "  " + map.get(i));
}

in.close();
}

}``````

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

``````Dictionary<int, int> D = new Dictionary<int, int>();

for (int i = 0; i < A.Length; i++)
{
if (!D.ContainsKey(A[i]))
else
D[A[i]]++;

}``````

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

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

``````#--Count Dup Numbers in Array
a = [1,1,2,2,3,4,1]
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``````

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

``````#--Count Dup values using hash--Ruby
a = [1,1,2,1,2,3,4,1]
l = a.length - 1
h = Hash.new
for i in 0..l
k = h.has_key?(a[i])
if !(h.include? a[i])
h.store(a[i],1);
else
c = h.fetch(a[i])
c = c+1
h.store(a[i],c)
end
end
h.each do |key, no|
puts "#{key} #{no}"
end``````

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

int a [] = {1,1,1,5,5,6,2,5,8,4,6,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,1,2,3,4,5,6,7,8,9,10,1,1,15,8};
int n=4;
int count=0;

for(int i=0; i<a.length; i++)
{
if(n == a[i])
{
count++;
}
}

System.out.println(count);

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

public static void main(String[] args)
{
int a [] = {1,1,1,5,5,6,2,5,8,4,6,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,1,2,3,4,5,6,7,8,9,10,1,1,15,8};
int n=4;
int count=0;

for(int i=0; i<a.length; i++)
{
if(n == a[i])
{
count++;
}
}

System.out.println(count);
}

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

``````public static void main(String[] args)
{
int a [] = {1,1,1,5,5,6,2,5,8,4,6,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,1,2,3,4,5,6,7,8,9,10,1,1,15,8};
int n=4;
int count=0;

for(int i=0; i<a.length; i++)
{
if(n == a[i])
{
count++;
}
}

System.out.println(count);
}``````

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

#In Python
a=[1,2,3,3,5,7,2]
d={}
for i in a:
try:
d[i] += 1
except:
d[i] =1
print d

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

#in Python
a=[1,2,3,3,5,7,2]
d={}
for i in a:
try:
d[i] += 1
except:
d[i] =1
print d

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.