Any advice on solving binary search problems?

Hi all,

I seem to have quite a bit of trouble solving even leetcode easy binary search problems in comparison to other leetcode problem types (solved 300+ problems so far). Does anyone have any advice/resources on how to get better at them (other than just practicing them of course)?

A lot of what I struggle with are the different edges cases that pop-up

  1. Infinite loops due to rounding
  2. Not knowing when to terminate if l < r vs l <=r
  3. When to return the l or r instead of m
  4. I also sometimes struggle on figuring out exactly how binary search is applicable to the problem as well

What ends up occurring is that I normally end up writing a search algorithm and then spend forever trying to fix every edge case that comes up and ultimately end up with a hacky mess.

Comments (2)