Binary-search

64 articles

dsa5 min read

Find Peak Element — Binary Search on Slope O(log n) [LC 162]

LeetCode 162 — asked at Google, Meta, and Amazon. Find any peak element in O(log n) using binary search on slope direction. If nums[mid] < nums[mid+1], a peak must exist in the right half — guaranteed by virtual negative infinity at both boundaries.

Read →
dsa11 min read

H-Index II — Binary Search on Sorted Citations [LC 275]

Find the h-index from a sorted citations array in O(log n) time using left-boundary binary search. Full FAANG interview breakdown with visual dry run, all edge cases, and intuition behind the search condition.

Read →
dsa10 min read

Aggressive Cows — Binary Search on Answer (SPOJ / GFG Classic)

Place C cows in N stalls to maximise the minimum distance between any two cows. Learn the canonical binary-search-on-answer pattern — the template behind Magnetic Force Between Two Balls, Split Array, and dozens of other hard problems.

Read →
dsa9 min read

Guess Number Higher or Lower — Binary Search with API [LC 374]

Find the secret number between 1 and n using the guess() API in O(log n) calls. Master the exact binary search template used in all interactive/guessing problems and understand why the API return values map directly to the lo/hi update rules.

Read →
dsa7 min read

Maximum Events Attended — Greedy and Min-Heap [LC 1353, Google]

LC 1353 asks for the maximum number of events you can attend given start and end days. Greedy approach: each day, attend the event with the earliest end date using a min-heap. Sort events by start day and use a pointer to add available events. O(n log n).

Read →
dsa11 min read

Longest Increasing Subsequence — From O(n²) DP to O(n log n) Patience Sorting

LC 300 Longest Increasing Subsequence finds the length of the longest strictly increasing subsequence. The O(n²) DP is the expected starting point; the O(n log n) patience sorting binary search optimization is what FAANG interviewers look for. This problem is asked at Amazon, Google, and Microsoft and is the foundation for Russian Doll Envelopes.

Read →
dsa11 min read

Minimum Effort Path — Dijkstra or Binary Search + BFS

Find the path from top-left to bottom-right that minimizes the maximum absolute difference between consecutive cells. Dijkstra treats effort as edge weight; binary search + BFS checks feasibility for each candidate effort.

Read →
dsa6 min read

Time Based Key-Value Store — Binary Search on Timestamps

Design a time-stamped key-value store that retrieves the latest value at or before a given time using binary search on sorted timestamp lists. Google and Amazon test this to evaluate versioned data design and binary search mastery.

Read →
dsa6 min read

Count Nodes in a Complete Binary Tree — LC 222 O(log^2 n) Proof

LC 222 Count Complete Tree Nodes has an O(log^2 n) solution that exploits perfect subtree detection — a FAANG interview problem where the naive O(n) answer is wrong. Learn the left/right spine height comparison trick tested at Amazon and Google.

Read →