bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
🎯Code CheckjavascriptEasyBinary Search

Starter: read a branch

Which score should this branch include? Goal: Make 70 count as a passing score, not only scores above 70.

Starter step 4 of 20Read code one line at a time

Track what changes after each comparison.

Write down left, right, and middle before choosing the next branch.

Problem Brief

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.

Puzzle Hints
  1. Which score should this branch include?

  2. A score of 70 should pass, so the comparison includes equality. The right choice is "70 and every score above it" because it preserves the goal: Make 70 count as a passing score, not only scores above 70.

  3. Tie your answer back to Binary Search: what data is stored, when it updates, and what condition uses it?

Asked at 8 companies
AdobeAmazonApple
Binary Search — Divide & Conquer
two pointers
lomidhi-1001325394125
lo=0
mid=2
hi=5
1
4

Target=9. lo=0, hi=5, mid=2. nums[2]=3

How to think: Binary Searchguide

The search space is sorted or monotonic — each guess eliminates half the possibilities.

1.Ask: "Is the data sorted?" → Classic binary search
2.Ask: "Can I frame this as yes/no with a threshold?" → Binary search on answer
3.The template: lo, hi, mid. Decide which half to eliminate.
4.Key: you don't just search arrays. You can binary search on any monotonic function.

vs Linear scan O(n): Data is sorted — binary search is O(log n)

vs Two Pointers: You need to find a single target (not a pair)

sortedfind minimumsearchrotatedkoko eating bananasminimum speed
How to think: Binary Searchguide

The search space is sorted or monotonic — each guess eliminates half the possibilities.

1.Ask: "Is the data sorted?" → Classic binary search
2.Ask: "Can I frame this as yes/no with a threshold?" → Binary search on answer
3.The template: lo, hi, mid. Decide which half to eliminate.
4.Key: you don't just search arrays. You can binary search on any monotonic function.

vs Linear scan O(n): Data is sorted — binary search is O(log n)

vs Two Pointers: You need to find a single target (not a pair)

sortedfind minimumsearchrotatedkoko eating bananasminimum speed
1// Goal: Make 70 count as a passing score, not only scores above 70.
2function starterExample() {
3 if (score >= 70) {
4 return "pass";
5 }
6}

Which score should this branch include?