Amazon Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




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

is the output correct in the question

{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

- jv August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hash table

- Tuotuo August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- subbu September 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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

- rich August 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

question not clear?

- wadhwajatin94 August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please explain the question again..?

- vgeek August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

- Pranab Nandi August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think either sample data or question is not clear.

- glebstepanov1992 August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question is "create a new array such that element at index "i" will be the element with highest frequency so far till i'th index in original array"

- Anonymous August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, Question not clear.

- OTR August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- N August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- juskuehn August 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Qs and output doesn't match.The Qs and output doesn't match.

- Srikant Aggarwal August 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't quite understand the question

- Zstone August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- Anonymous August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous August 28, 2013 | 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