Public Tech Thinking
Entries
-
Two Faces of Binary Search: A Tale of `lo <= hi` and `lo < hi`
Binary search is often taught as a single algorithm: cut the range in half, throw away the wrong side, repeat. Yet anyone who has solved more than a handful of binary-search problems knows a quiet frustration — sometimes the loop is written while (lo <= hi), sometimes while (lo < hi), and swapping one for the other turns correct code into an infinite loop or an off-by-one bug. LeetCode 704. Binary Search and 875. Koko Eating Bananas are the perfect pair to dissolve this confusion, because they are both binary searches and yet they belong to two genuinely different species. Understanding why each demands its own loop condition is understanding binary search itself.
-
Iterative DFS: Mark-on-Pop vs. Mark-on-Push — A Comparative Analysis
Depth-First Search (DFS) is one of the most fundamental graph traversal algorithms in computer science. While its recursive form is elegant and intuitive, practical applications often demand an iterative version using an explicit stack to avoid stack overflow on large inputs. However, there is a subtle but important design decision in iterative DFS that is rarely discussed in textbooks: when should a node be marked as visited — when it is pushed onto the stack, or when it is popped off?