Arista Networks Interview Question for Software Engineers


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[1] ,becomes 7 then we continue with second number 6 and a[1] becomes 8 and by thrid 6 a[1] becomes 9, by 0 a[0] becomes 7,and by 18 a[3] 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);
	  }
  }

- EPavlova January 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- zr.roman January 17, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- aka January 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

Any comments?

- fayezelfar January 19, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- aka January 19, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- aka[1] January 19, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use hashtable, quite straightforward.

- zr.roman January 16, 2016 | Flag Reply
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

- EPavlova January 18, 2016 | Flag Reply
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]);
}
}

- Samar March 10, 2016 | Flag Reply
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;
}

- My solution with O(n) September 07, 2016 | 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