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.

- howaboutthis March 21, 2012 | Flag Reply
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.

- eugene.yarovoi March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

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

- anon.coder March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- chid1989 January 13, 2014 | Flag
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.

- pradeep goswami March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- dabbcomputers March 21, 2012 | Flag Reply
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.

- eugene.yarovoi March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- DG April 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the link!

- Puneet November 13, 2013 | Flag
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.

- Anonymous October 20, 2013 | Flag Reply
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.

- Anonymous October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

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;

- Sreekanth November 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I meant 20 integers not 20 bytes

- Anonymous November 20, 2013 | Flag
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.

- Anonymous October 20, 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