## Interview Question for Analysts

Country: India
Interview Type: In-Person

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

I think you can use heap and BST separately for the two problems mentioned.

1. At any point of time, if you want to know only about the element that is repeated maximum number of times, you can use a max heap. For the max heap, the key will be the frequency/count of the elements in the array and an additional field, say, value would hold the element of the array.

2. At any point of time, if you want to know the element(s) that are repeated exactly n times, we can maintain a BST. Here again, the keys will be the frequency/count of the elements seen so far and an additional field called value would hold the element of the array.

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

You will also need a hashset to keep a track of the values that you have already entered in the heap.

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

To find maximum number repeated maximum times:

``````#include <stdio.h>

#define SIZE 13

int boyerMoore(int arr[]) {
int current_candidate = arr[0], counter = 0, i;
for (i = 0; i < SIZE; ++i) {
if (current_candidate == arr[i]) {
++counter;
printf("candidate: %i, counter: %i\n",current_candidate,counter);
} else if (counter == 0) {
current_candidate = arr[i];
++counter;
printf("candidate: %i, counter: %i\n",current_candidate,counter);
} else {
--counter;
printf("candidate: %i, counter: %i\n",current_candidate,counter);
}
}
return current_candidate;
}

int main() {
int numbers[SIZE] = {5,5,5,3,3,1,1,3,3,3,1,3,3};
printf("majority: %i\n", boyerMoore(numbers));
return 0;
}``````

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

Hi,
as mentioned , it is not a fixed array. The size is unknown , like streams in network etc.

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

I think we can use Map and then print the max Map with certain key.

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

That algorithm is not relevant here. The majority voting algorithm applies only to situations where the most frequently occurring element is encountered more than arr.length/2 times.

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

Hi,
You can use Hash Set and Hash Map for this.
-> Initially when you are reading the data insert into hash set, if and integer is repeating insert into hash map as a key, and add count. If that integer repeats again, then increment count. (As hash set doesn't allow duplicates) .
-> Hope this should work fine. as hash set insert into O(1) time and HashMap also takes O(1) time.

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

@prashanthreddy.mtech: does hashmap work for infinite numbers?

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

Yeah, i hope so. It depends on how well u implement hashing Algorithm.

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

static void RepeatedForMaximumTimes()
{
int intCount = 1;
int[] numberList = new int[] { 4,5, 5, 5, 3, 3, 1, 4, 4, 4, 4, 1, 3, 3};
int currentNumber = numberList[0];
int MaxRepeatedNumber = numberList[0];
int MaxRepeatedCount = 1;

for (int intA = 1; intA < numberList.Length; intA++)
{
if (currentNumber == numberList[intA])
{
intCount++;
}
else
{
if(intCount > MaxRepeatedCount)
{
MaxRepeatedNumber = currentNumber;
MaxRepeatedCount = intCount;
}

intCount = 1;

currentNumber = numberList[intA];
}
}

if (intCount > MaxRepeatedCount)
{
MaxRepeatedNumber = currentNumber;
MaxRepeatedCount = intCount;
}

Console.WriteLine("Number: " + MaxRepeatedNumber + " Repeated times:" + MaxRepeatedCount);
}

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

It is possible to do it on-the-fly since the array is not upper-bounded.

1. maxRepeatedVal=0;maxVal=0
2.insert values as they receive into a hash table and increment the corresponding hash entry.
3. if hash[ThisValue]>maxRepeatedVal
{
maxVal = ThisValue;
maxReaptedVal=hash[ThisValue]
}

Note: If the value if maximum of N occurrences is required, then the variable maxRepeatedVal should have a constant value viz. N

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

We can use max heap with every node arranged by number of occurrences of that element.
Root element will give us the element with maximum occurrences.

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.