Amazon Interview Question
SDE-2sCountry: United States
Interview Type: Phone Interview
If u dnt knw 2 form a sentence just leave it dnt post any qustn further.,Anyway i understood the ques..
#####Given an array of positive integers, create another array of the same length and integers such that in the new array ,1) u should return only the integers which have highest frequency ,2)In the given array 1 is placed in 1st index and 5th index
so from this array the difference between the indexes 4 ,so u hav 2 return 1 as 4 times..
3) next 3,in array 3 is at 3rd index and at 9th index,so return 6 times..
input array: {1,2,3,6,1,3,6,7,3,9,4,2}
output: {1,1,1,1,3,3,3,3,3,3,3}
correct me if i m wrong..
var node = list.First;
if (node == null) return;
var freqList = new LinkedList<int>();
var scores = new Dictionary<int, int>();
// Initialize the arrays and dictionary
int mostFrequentValue = node.Value;
int mostFrequentCount = 1;
scores.Add(node.Value, 1);
freqList.AddFirst(node.Value);
node = node.Next;
while (node != null)
{
if (scores.ContainsKey(node.Value))
{
scores[node.Value]++;
if (mostFrequentValue != node.Value)
{
if (scores[node.Value] > mostFrequentCount)
{
mostFrequentValue = node.Value;
mostFrequentCount = scores[node.Value];
}
}
else
{
mostFrequentCount++;
}
}
else
{
scores.Add(node.Value, 1);
}
freqList.AddLast(mostFrequentValue);
node = node.Next;
}
Use a max heap along with a hash table.
Loop through the array and for every value, get the heap node (which stores count) from the hash table and increase the count for the node. The element at the top of the heap will be the one with the maximum frequency.
I don't think you would need to use a max heap in this case. Just keep track of the current max value and every time you increment a value in the hash table perform a check to see if it is greater than the stored max. That way you could avoid the cost of constantly having to reorder the max heap to keep the right value on top.
#define SIZE(A) (sizeof(A)/sizeof(A[0]))
void PrintFrequencies(int *a, int n)
{
int i;
int m=INT_MIN;
int max=INT_MIN;
int val;
try
{
if(a==NULL || n<0) throw "\nInvlaid Input\n";
}catch(const char *s)
{
puts(s);
return;
}
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
int *t=new int[m+1];
memset(t,0,sizeof(int)*(m+1));
val=a[0];
for(i=0;i<n;i++)
{
t[a[i]]++;
if(t[a[i]]>max)
{
max=t[a[i]];
val=a[i];
}
printf("%d ",val);
}
printf("\n");
delete [] t;
}
Use c++11 compiler:
#include <iostream>
#include <string>
#include<vector>
#include<unordered_map>
using namespace std;
std::vector<int> get_freq_map( std::vector<int> & arr);
int main()
{
cout << "Hello World" << endl;
std::vector<int> input = {1,2,3,6,1,3,6,7,3,9,4,2 };
std::vector<int> output = get_freq_map( input);
for (auto &i: output) {
std::cout << i ;
}
std::cout <"\n";
return 0;
}
std::vector<int> get_freq_map( std::vector<int> & arr) {
std::unordered_map<int, int> a_map;
int max_freq_value = arr[0];
std::vector<int> freq_map;
freq_map.push_back(max_freq_value);
a_map[arr[0]] = 1;
for ( auto &i : arr) {
if (&i == &arr[0]) continue;
if ( a_map.find(i) == a_map.end()){
a_map[i] = 0;
} else {
a_map[i]++;
}
if ( a_map[i] > max_freq_value) {
max_freq_value= i;
}
freq_map.push_back(max_freq_value);
}
return freq_map;
}
is the output correct in the question
- jv August 13, 2013{1,2,3,6,1,3,6,7,3,9,4,2} -> Input
{1,1,1,1,1,1,1,1,3,3,3,3} -> output should be
{1,1,1,1,3,3,3,3,3,3,3} -> output given
correct me if wrong