PROGRAMMING INTERVIEWS: Find integer average of 2 integers
Find integer average of 2 integers I generally start with a very simple question with my interview candidates. I ask the for writing a program for giving the integer average of 2 integers using only integer type variables. Assume that input will always be with in integer limits. The definition of integer average is the highest smaller integer if average is floating point number. Also the condition if that they can not use any typecasting or any datatype other than int. Example: a = 4, b = 5, avg = 4 a = 4, b = 6, avg = 5 a = -4, b = -6, avg = -5 a = 4, b = -5, avg = -1 a = -4, b = -5, avg = -5 95% of candidates smell some issues in this problem and when I say nothing is there they don't even ask any clarification and come up with something like this: int get_average(int a, int b) { return (a+b)/2; } Then I ask them to have a look if this is correct on definition and then they see that it is. But there test failed on very first floating point negative average.Read full article from PROGRAMMING INTERVIEWS: Find integer average of 2 integers
No comments:
Post a Comment