## Arista Networks Interview Question for Software Engineers

• 0

Country: India
Interview Type: Written Test

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

actually you should take into account fact that intergers are in range [0 .. n - 1] and n (array size). So we know that each number has a frequency between 0 and n. Suggest that we modify array and multiply each item by (n+1). For example we have array [1,1,1,0,3] n = 5. We multiply by 6 and the result is [6,6,6,0,18]. Then we iterate through we take each number val and calculate orig value i = value/6 and increment the i index of array with 1
In our case i = 6/6 = 1 , we increment a ,becomes 7 then we continue with second number 6 and a becomes 8 and by thrid 6 a becomes 9, by 0 a becomes 7,and by 18 a becomes 1. Then we move through array and devide by mod each element by n+1, reminder is frequency. Here is some implementation

``````void findFrequency (int[] array) {
int n = array.length;
int m = (n+1);
for (int i = 0; i < n ; i++) {
array[i] *= m;
}
for (int i = 0; i < n ; i++) {
array[array[i]/m] += 1;
}
for (int i = 0; i < n ; i++) {
System.out.println( "number "+i+" : "+ array[i] % m);
}
}``````

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

nice.
How did you guess about all that tricky stuff?

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

nice logic.

``````def frequency(input, n):
for i in range(0, len(input)):
input[i] *= (n+1)
for i in range(0, len(input)):
input[input[i]/(n+1)] += 1
for i in range(0, len(input)):
input[i] = input[i]%(n+1)
return input

input = [1, 2, 3, 4, 1, 1, 2, 4, 7]
print(frequency(input, 7))``````

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

The solution is clever, but it's not O(n); you have multiple iterations. The trick here is to make a decision as you're traversing through the list.
A HashMap where you increment the value on collision is O(n), but then you have to iterate through the HashMap to see which values are >1, which has a worst case of O(N).
I don't think it's possible to do in O(N) because any algorithm will require two steps:
1. Parsing the data
2. Examining the data.

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

fayezelfar: it is o(N). I am afraid but you need to refer what is the meaning of o(n).

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

@fayezelfar: you need to know what is the meaning of O(n). It certainly doesn't mean that you can only traverse once.

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

use hashtable, quite straightforward.

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

Thank you, its just about encoding and practice....I was thinking about how to encode freq. information by indexes. Using remainders is possible because we know in adavance that all elements repeat at most n

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

My solution
private static void numFrequency(int[] A) {
int[] B = new int[A.length];
int i;
for (i = 0; i < A.length; i++) {

B[A[i]] += 1;

}
for (i = 0; i < B.length; i++) {
System.out.println("number " + i + " Frequency " + B[i]);
}
}

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

This just iterate the array once:

``````int freq(int[] array, int n)
{
int i, index;
for (i=0; i<n; i++) {
index = array[i] % n;
array[i] = array[i]/n;

array[index] += i >= index ? 1 : n;
}

printf("Frequency of appear:index   freq\n");
for (i=0; i<n; i++)
printf("%5d %d\n", i, array[i]);

return 0;
}``````

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.