Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's funny is that the safer alternative:

   int mid = low + ((high - low) / 2);
is probably what most of us originally came up with before we saw the shorter, more elegant, but overflow-prone approach.



I wrote one before I read the article to see if I would hit the bug and yep, I wrote it the safer way.

For me, that is the most readable way to do it because it lines up both conceptually and visually with how I'm thinking of the problem.


I was once penalised for doing this in a leetcode problem given during an interview. I was told to rethink things about calculating a mid-point from first principles.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: