So what sort of answer do you find satisfactory? It seems like the "right" solution is non-trivial bit twiddling, do you expect people to come up with that, or stop sooner than that?
I'm asking as I doubt "know about how computers work" is necessary for most tech roles.
I get it, having technical expertise is important, but still ...
Edit: never mind, this can overflow too
int avg(int a, int b) {
if ((a < 0) != (b < 0)) // not the same sign?
return (a + b) / 2;
else
return a + (b - a) / 2;
}This will round (-2,-1) to -2, i.e. away from zero. For comparison, if we perform the canonical (a+b)/2 instead it will round to -1, i.e. towards zero.
Now, the problem statement does not tell us how to round, so you’re technically correct, but the inconsistency bothers me.