Binary Search Algorithm
Step by Step
Check the middle of what is left and throw away the half that cannot hold the answer.
Step through every halving at your own pace, then share a link to the step where you got stuck.
What You Watch While the Range Halves
The range, the middle and the discarded half change on every step, and the visualiser tracks them for you.
Halving looks short on paper and gets slippery on step nine, when lo, hi and mid all move at once.
CodeStory's visualiser shows the search space halving and greys out the half it discards. Code lines stay highlighted beside the array. Step forward, step back or let it play.
Try an empty array, a single number and a target that is not in the array. Duplicates and targets at either end matter most in interviews.
How Binary Search Narrows the Range
Picture and list below move in the same order.
- 1Keep the answer inside a range from lo to hi.
- 2Look at the middle of what is left.
- 3Decide which half cannot hold the answer and discard it.
- 4Repeat on the half that remains until the range holds one value.
Where Binary Search Goes Wrong
Three switches live in the visualiser. Flip one on and watch the search miss, crawl or never finish.
- Linear scanChecks every number from the left, so the worst case takes n checks instead of about log₂ n.
- Bug: while (lo < hi)Stops one step early, so the last candidate is never checked. Try a target at either end of the array.
- Bug: lo = midForgets that mid was already checked. When two numbers are left, lo never moves and the loop never ends.
Binary Search Inputs Worth Trying
One preset per edge case, and two of them pair with a bug switch.
- Not in the arrayWatch where lo ends up.
- Target is the last numberSwitch on the lo < hi bug here.
- DuplicatesSearch finds a 3, not necessarily the first one.
- Two numbers leftSwitch on the lo = mid bug here.
Binary Search Cost in Time and Space
Interviewers ask why O(log n) and O(1), and the visualiser counts real operations.
- Time
- O(log n)counted from the real checks on your array
- Space
- O(1)a few pointers and nothing else
- Pattern
- Binary searchHalf of the range disappears at every check.
Practise Binary Search on Real Problems
Watching shows the halving and solving builds the instinct.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
Sign up for 100 AI credits now and get 100 more every month. Start free in the app
Binary Search Visualiser FAQs
Open the visualiser and watch the answers play out.
Open the visualiserWhat does the binary search visualiser show?
CodeStory's visualiser shows the search space halving and greys out the half it discards, on an array you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.
What is the time complexity of binary search?
Binary search takes O(log n) time and O(1) space. CodeStory's visualiser counts real operations as it plays, so you watch the number instead of trusting the formula.
Why does my binary search code fail on some inputs?
Binary search code fails when the loop stops early with while (lo < hi) or never ends with lo = mid. CodeStory's visualiser ships both bugs as switches, so you flip one on, replay the input and watch exactly where the state goes wrong.
Does binary search find the first duplicate?
No. In the duplicates preset it finds a 3, not necessarily the first one.
Can I use my own input?
Yes. Type your own sorted array and the visualiser plays binary search on it, edge cases included.
Is the visualiser free?
Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.
Stuck on Binary Search? Get a Free Callback
Tell CodeStory where binary search loses you, and a mentor plans your next steps.
- 1CodeStory finds your starting point, from your first midpoint to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.