You are viewing gregbo

Previous Entry | Next Entry

Cisco interview

classic cylon
I had an onsite interview with a QA group at Cisco last Thursday. It went ok – better than the recent Brocade interview, but not as well as I would have liked it to. One of the people who interviewed me asked me lots of protocol questions, most of which I gave very good answers to. The time I've spent on the LinkedIn IPv6 and other computer networking-related forums has paid off.

There was a C programming question that I had trouble with involving pre- and postincrement of variables. I had never encountered this type of situation, where the target of an assignment statement is also an operand:


int v, x, y;

v = x = 5;
y = 11;

v = v++;

x = ++y + x++;

printf("%d %d %d\n", y, x, v);


The value of v is 5. The values of y and x are 12 and 18, respectively. I don't understand why either v is not equal to 6 or x is not equal to 17. I checked K&R, which doesn't say anything explicit about what happens when the target of an assignment statement is also pre- or postincremented. My general opinion is that one wouldn't want to write such code, which could have unexpected side effects, but perhaps I'm wrong.

Update: Didn't get the job. Oh well.

Comments

( 4 comments — Leave a comment )
gconnor
Apr. 8th, 2012 10:43 pm (UTC)
I think v=v++ is interpreted as 1. get v (5) 2. increment v (6) 3. put the result of v++ (5) in to v (back to 5).

But, I would also have put x at 17 so, yeah.
siderea
Apr. 9th, 2012 12:51 am (UTC)
My general opinion is that one wouldn't want to write such code,

++
(Anonymous)
May. 18th, 2012 01:12 pm (UTC)
Hope you find an interview without a pretenious interviewer...
Every compiler environment that I've tried this on (*ix, Windows, Linux) results in the same answer, the one that I expect.

12 18 6

However the behavior of both v = v++ and x = ... x++ are undefined behavior. There is no sequence point and to quote the C99 spec...

"If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side
effect occurs in any of the orderings.

84) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;"

Good luck in further interviews.
gregbo
May. 18th, 2012 06:27 pm (UTC)
Re: Hope you find an interview without a pretenious interviewer...
Thanks. That is the kind of information I am looking for.
( 4 comments — Leave a comment )