Cisco Systems 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

The output depends upon the architecture i.e. word length of the micro-processor.
If the word length is 16 bits, then the output is 4 bytes.
If the word length is 32 bits, then the output is 8 bytes.
One more factor that may affect the output is the word alignment which can be changed by #pragma. By default, in turbo-c, the word alignment is 1 byte.There is no effect of #pragma directive. So, there you will find the output as 3 bytes.

- Aashish July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain the #pragma part . I couldn't understand how the output becomes 3 bytes .

- Shobhit July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik
Are u sure u r correct?
if yes then plz point out my mistake.in case of fundamental data types the alignment remains the same as size while in case of user defined,alignment is on the basis of the largest element of the struct which here is int(4 byte) so 4+4=8 should be correct
I have implementd the code ,o/p is 8(for 64 bit sys).

- codez July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@codez : you are correct . Output is 8 (64 bit and 32 bit)
It depends on the machine architecture .

- Shobhit July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sry misread shondik's post
@shondik. yes plz exlain a lil bit more related to #pragma

- codez July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah sure. There is a concept called as memory banking which is undertaken by the microprocessor for faster memory access. In INTEL micro-processor, an address always starts from the even one. If it starts from the odd address, then the micro-processor will take two clock cycles to access a memory.
I am not talking about the word alignment in case of fundamental types. Word alignment plays a very vital role in case of use defined types for faster memory access.

Now, coming to your issue. Your output is correct but you are missing some points. Word alignment is never done based upon the size of biggest type. Consider the below example:

struct s
{
        int i;
        double d;
};
 
int main()
{
        struct s s1;
        printf("%d ",sizeof(s1));
 
        return 0;
}

The output is 12 & not 16. See here: ideone.com/uVdlU

One more point must be noted. Alignment is also done based on the structure variables.

- Aashish July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#pragma directive can be used to change the word alignment of the user defined type.
Used as: #pragma pack(1).
Hope its clear.

- Aashish July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah thnx for the details and correcting me.It appears more clear....It's the word size of processor that matters not the largest size element.

- codez July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik : plz explain the #pragma concept using an example . the value inside the pack must be the power of 2 .

- Shobhit July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

See this example:

#pragma pack(1)
 
struct s
{
        char c;
        int i;
};
 
int main()
{
        printf("%d ",sizeof(struct s));
        return 0;
}

The output is 5 because i have changed the alignment to 1 byte.
So, 1byte for char + 4 bytes for int. ideone.com/2Gg6n

- Aashish July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanx . I got it .

- Shobhit July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting stuff with the pragmas. I've never seen such a thing. It's important to keep in mind, of course, that pragmas are completely nonstandard and options for using them may differ from compiler to compiler. I'm guessing that in the event you say pack(1), the compiler automatically uses 2 cycles to access misaligned memory locations and manipulates the results with some bit shifting to return the correct value.

- Anonymous July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is beacause of the word size which is 8 bytes(64 bits) on your machine( 8 byte chunks )

- Barney August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is 8
But is this process called address alignment or bit alignment/bit padding
I am not sure asking you guys

- Avi July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes , it is . depends on the word size of a particular machine .

- Barney August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

see ideone.com/EUnyw

- Anonymous July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends on the compiler, which tries to optimize layout for the target architecture.

So the answer will vary. Saying there is one answer is wrong, IMO. Compilers have a certain degree of freedom here, and the behavior can also be changed with command line args.

The ISO C standard does not specify required struct sizes, but it does have rules about alignment. (i.e. alignment of a struct has to be a multiple of smallest common multiple of the field alignments).

This is why, if you require a certain packing (as protocol stack and hardware driver structs often do), you may need to add compiler extensions to specify the alignment you need.

For examples, see the packed, align, and bitwise attributes for GCC. They are used throughout the linux kernel.

- ajfabbri May 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

6

- airtel July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

8

- codez July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@shondik- that program is outputting 16 nd not 12 whyy?

- rohiy August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think , It depends on the word size of a particular machine .

- Barney August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It depends on the size of the data bus. Alignment always adjusts the starting address to being with a power of 2. When data is transferred from memory to CPU registers, there is a minimum number of bits that can be transferred in a clock cycle. On a 32 bit machine, it is 32 bits. Thus if you have addresses that are not aligned, there would be wastage of bus capacity.

- Noobie August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | 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