## Arista Networks Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

If we understanding padding correctly here the largest is double hence it will take them and multiply number of elements (except the a[0] as there is no array) hence answer is 24
if we have a[1] the answer will be 32.

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

The standard doesn't even define how much space an int takes, so it definitely doesn't define the size of this struct. Perhaps a good answer would be sizeof (char) + sizeof (int) + sizeof (double) + sizeof (void*) + possibly some space for alignment. I think your interviewer wants to see that you understand that the standard doesn't strictly define the size of things like this and that you know what alignment is.

A char is 1 byte (well, not quite: it's 1 word, which is usually 1 byte), and on a typical system an int is 4 bytes and a double is 8 bytes. On a typical 64-bit system, the pointer would be 64 bits, or 8 bytes (if this question asked about a 32-bit system, the pointer would typically be 4 bytes). So the total would then come to 21, which would probably be aligned to the nearest 8 and be 24.

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

It should be:
sizeof(char) + padding here + sizeof(int) + sizeof(double) + sizeof(void *)

In my 32 bit system it comes to: 1+3+4+8+4 = 20 bytes

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

On a 32 bit machine, this will be 20 bytes only i suppose. Though a double is of size 8 bytes, it needs only a 4 byte alignment. Even if it is aligned on a 8 byte boundary, it would need two memory fetch. Thus the size is going to be 20 bytes for a 32 bit machine. For a 64 bit machine, it will be 24 bytes

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

due byte padding the bits would be aligned multiple of four bytes.hence total bytes would be 24. byte padding is done for optimization by processor.

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

It's important to keep in mind, however, that the existence of padding is not guaranteed, and there is no absolute guarantee that the size will in fact be 24. It's important to use sizeof to really know the size.

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

Ans is 24
here is the explanation---> geeksforgeeks.org/archives/9705

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

I would be careful about saying the answer is any specific value...see my answer for the reasoning behind that. I agree that 24 is likely to be the size, though.

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

The explanation in the article you refer to is not accurate. For this example, on Win 7 x64 and Linux x64, I do get 24, but on Linux x86, I get 20.

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

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

On 32 bit system, it is 20 bytes since pointer is 4 bytes and a[0] occupies
zero byte. 'a[0]' is a place holder. If it is 24 bytes, then a[0] is a[1] which is not
possible,

On 64 bit system, it is 24 bytes since pointer is 8 bytes and a[0] still occupies
zero byte.

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

Then the real question becomes: why do we need a[0]? :-) :-) :-).
In certain situation in Arista network, this a[0] comes in handy.

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

The a[0] is trick to allocate variable size buffer at the end of the structure. E.g., if you need 20 bytes buffer in the a[], you would do something like

``struct s *s1 = malloc(sizeof(struct s) + 20 * sizeof(int));``

This would enable you to allocate both the structure and the buffer in a single malloc call(and also allow you to free it in one call). Instead of doing something like

struct s * s1 = malloc(sizeof(struct s));
s->a = malloc(20 * sizeof(int));

and in free

free(s->a);
free(s);

It's commonly used technique to prevent memory leaks(people forgetting to free a and just freeing s)
Ofcourse, this assumes the member a is defined as
int *a;

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

I meant 20 integers not 20 bytes

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

Then the real question becomes: why do we need a[0]? :-) :-) :-).
In certain situation in Arista network, this a[0] comes in handy.

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.