Binary Search & Modified BS

Fundamentals

Logarithmic O(log N) search on sorted spaces, monotonic predicates, and answers

318 problems·34 Easy·178 Medium·106 Hard
Pattern Study Guide & Cheat Sheet
▼

Halve the search space at each step over any monotonically ordered sequence or decision boundary (FF...FTTT...T).

Core Invariant: The predicate `feasible(mid)` divides the range into two halves: [False, ..., False, True, ..., True]. Binary search pinpoints the exact boundary where the transition occurs.

Recognize it (Keywords & Signals)
  • Search target in sorted or rotated sorted array in O(log n)
  • Find first or last occurrence of an element in a duplicated sorted array
  • Find minimum or maximum value satisfying a condition (Binary Search on Answer)
  • Keywords: "minimize the maximum", "maximize the minimum", "capacity to ship", "eating speed"
When NOT to use

Search space is neither sorted nor monotonic (the feasibility predicate does not stay True once satisfied).

How to solve (Step-by-step)
  1. 1.Define search boundaries: `low` = minimum possible answer, `high` = maximum possible answer.
  2. 2.In a `while low <= high` loop: calculate `mid = low + (high - low) // 2`.
  3. 3.Evaluate feasibility: `can_achieve(mid)`.
  4. 4.For minimization: if feasible, record `ans = mid` and search smaller values `high = mid - 1`; else `low = mid + 1`.
  5. 5.For maximization: if feasible, record `ans = mid` and search larger values `low = mid + 1`; else `high = mid - 1`.
  6. 6.Return recorded `ans`.
Watch for (Interview Traps)
  • Infinite loop from improper midpoint update: avoid `low = mid` without `+ 1` in `while low <= high`
  • Setting `low` too small in answer search (e.g. `low = 0` when individual items cannot fit)
  • Integer overflow in non-Python languages: use `low + (high - low) // 2` instead of `(low + high) // 2`
Binary Search on Answer (Minimize Feasible Value)
def binary_search_on_answer(weights: list[int], days: int) -> int:
    def can_ship(capacity: int) -> bool:
        needed_days = 1
        curr_load = 0
        for w in weights:
            if curr_load + w > capacity:
                needed_days += 1
                curr_load = 0
            curr_load += w
        return needed_days <= days

    # Boundaries: minimum single package and total package weight
    low, high = max(weights), sum(weights)
    best = high
    
    while low <= high:
        mid = low + (high - low) // 2
        if can_ship(mid):
            best = mid        # Capacity works, try to find smaller
            high = mid - 1
        else:
            low = mid + 1     # Capacity too small, must increase
            
    return best
Cost
O(log(range) * O(check)) · O(1) auxiliary space (Logarithmic iterations guarantee extremely fast convergence (< 32 iterations for 10^9).)
Canonical problems
#704 Binary Search: Canonical search for value in sorted array
#33 Search in Rotated Sorted Array: One half is always sorted; check if target falls inside it
#875 Koko Eating Bananas: Search eating speed in range [1, max(piles)]
#1011 Capacity To Ship Packages Within D Days: Search minimum ship capacity in range [max(w), sum(w)]
Medium·60 companies·Max freq 100%·Acc 45.2%
NewsBreakGainSightDelhivery+57
Medium·51 companies·Max freq 100%·Acc 50.2%
TrexquantoyoQuantiphi+48
Hard·50 companies·Max freq 100%·Acc 47.0%
YahooRipplingZenefits+47
Medium·39 companies·Max freq 100%·Acc 50.0%
ConfluentCockroach LabsAxon+36
Medium·34 companies·Max freq 100%·Acc 59.7%
SplunkSamsungPayPal+31
Medium·33 companies·Max freq 100%·Acc 49.3%
InstacartTinkoffSplunk+30
Medium·33 companies·Max freq 89%·Acc 47.1%
Urban CompanyZeptoMeta+30
Medium·27 companies·Max freq 100%·Acc 49.2%
Two SigmaLiftoffNuro+24
Medium·26 companies·Max freq 100%·Acc 69.7%
DatabricksDropboxCareem+23
Medium·25 companies·Max freq 88%·Acc 36.0%
WixNeetCode 150NeetCode 150+22
Medium·24 companies·Max freq 100%·Acc 54.7%
YatraSquarepoint CapitalRevolut+21
Hard·24 companies·Max freq 100%·Acc 60.9%
BaiduYugabyteZeta Global+21
Hard·24 companies·Max freq 100%·Acc 54.8%
Pony.aiPony.aiAirbnb+21
Medium·24 companies·Max freq 90%·Acc 64.6%
NianticNikeNeetCode 150+21
Medium·24 companies·Max freq 88%·Acc 68.0%
LinkedInRokuMeta+21
Medium·23 companies·Max freq 100%·Acc 54.3%
Wissen TechnologyNeetCode 150NeetCode 150+20
Medium·23 companies·Max freq 100%·Acc 74.3%
DP worldApollo.ioApollo.io+20
Medium·22 companies·Max freq 91%·Acc 55.0%
NeetCode 150NeetCode 150Blind 75+19
Easy·22 companies·Max freq 84%·Acc 72.3%
Arista NetworksRevolutHCL+19
Medium·20 companies·Max freq 100%·Acc 59.3%
blinkitMolocoZomato+17
Medium·20 companies·Max freq 100%·Acc 36.8%
DeepMindMongoDBVerkada+17
Medium·20 companies·Max freq 88%·Acc 49.9%
YandexCoupangCashfree+17
Medium·19 companies·Max freq 100%·Acc 65.2%
Squarepoint CapitalUBSWix+16
Hard·19 companies·Max freq 100%·Acc 38.0%
Argo AIVerilyMedia.net+16
Medium·19 companies·Max freq 97%·Acc 65.4%
NeetCode 150NeetCode 150tcs+16
Medium·19 companies·Max freq 88%·Acc 52.2%
BCGCitigroupCitigroup+16
Medium·18 companies·Max freq 88%·Acc 57.6%
MotiveBaiduWhatnot+15
Easy·18 companies·Max freq 83%·Acc 42.1%
GrammarlyZohoTikTok+15
Easy·17 companies·Max freq 100%·Acc 78.0%
CriteoMongoDBTwo Sigma+14
Medium·16 companies·Max freq 100%·Acc 70.9%
XXNeetCode 150+13
Easy·15 companies·Max freq 92%·Acc 61.1%
NeetCode 150NeetCode 150SAP+12
Hard·13 companies·Max freq 100%·Acc 41.4%
SIGCapital OneVisa+10
Medium·13 companies·Max freq 100%·Acc 0.7%
CleverTapExpediaPayPal+10
Medium·12 companies·Max freq 100%·Acc 0.7%
XXPhonePe+9
Medium·12 companies·Max freq 100%·Acc 45.0%
Pony.aiPony.aiUrban Company+9
Medium·12 companies·Max freq 88%·Acc 0.6%
SnapSnapClari+9
Easy·12 companies·Max freq 85%·Acc 51.7%
GrammarlyMicrosoftBloomberg+9
Medium·12 companies·Max freq 78%·Acc 42.2%
AndurilZeta GlobalAdobe+9
Medium·12 companies·Max freq 75%·Acc 58.3%
UberFlexportIntuit+9
Hard·11 companies·Max freq 78%·Acc 68.0%
WeRideNeetCode 150NeetCode 150+8
Medium·10 companies·Max freq 100%·Acc 0.7%
ZenefitsLyftSalesforce+7
Hard·10 companies·Max freq 77%·Acc 34.7%
eBayIntuitGoogle+7
Medium·10 companies·Max freq 75%·Acc 72.4%
RobloxFlipkartSalesforce+7
Easy·10 companies·Max freq 69%·Acc 0.6%
MetaMorgan StanleyTikTok+7
Medium·10 companies·Max freq 54%·Acc 40.2%
AccentureBloombergWalmart Labs+7
Medium·9 companies·Max freq 100%·Acc 57.0%
ExpediaAT&TBloomberg+6
Hard·9 companies·Max freq 100%·Acc 23.9%
ArcesiumSamsungTexas Instruments+6
Hard·9 companies·Max freq 100%·Acc 0.3%
DevRevPhonePeGoldman Sachs+6
Hard·9 companies·Max freq 100%·Acc 63.4%
General ElectricGrowwSnowflake+6
Medium·9 companies·Max freq 97%·Acc 51.4%
razorpayTekionDE Shaw+6
50 displayed (of 50 matching)Filtered: All Companies

Top Companies Testing Binary Search & Modified BS

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Binary Search & Modified BS frequently asked questions

What is the Binary Search & Modified BS pattern in coding interviews?

Logarithmic O(log N) search on sorted spaces, monotonic predicates, and answers It is categorized under Fundamentals and features 318 curated interview problems across Easy (34), Medium (178), and Hard (106) difficulties.

Which tech companies frequently test Binary Search & Modified BS?

Companies frequently asking Binary Search & Modified BS questions include Google, Amazon, Meta, Microsoft, Bloomberg, Uber.

How do I master the Binary Search & Modified BS pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.