Amazon Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: Phone Interview
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
----------------------------------------
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)
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 [26];
Int index = 0;
For (int i = 0; i<str.length(); i++)
{
index = str.charAt(i) - ‘a’ ;
countarr(index) ++;
}
Return countarr;
}
}
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 [26];
Int index = 0;
For (int i = 0; i<str.length(); i++)
{
index = str.charAt(i) - ‘a’ ;
countarr(index) ++;
}
Return countarr;
}
}
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
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);
}
Here is my python solution without using any inbuilt functions
def findFrequency(string:str)->dict:
char_freq = {}
for char in string:
if char in char_freq:
char_freq[char] += 1
else:
char_freq[char] = 1
return char_freq
If the given input is an array
def findFrequency2(nums:list)->dict:
num_freq = {}
for num in nums:
if num in num_freq:
num_freq[num] += 1
else:
num_freq[num] = 1
return num_freq
public class Frequency{
- kartik December 06, 2020public 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>();
//iterate through your unique values
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"));
}
}