Coding Interview Prep · Lesson

bisect_left and bisect_right

Find insertion points in a sorted list.

Lesson 2 of 413 steps

bisect_left and bisect_right is a free Coding Interview Prep lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, and your progress syncs across the web and the CoddyKit app. The Coding Interview Prep course includes 4 lessons in total.

Search Without the Boilerplate

Python's bisect module gives you a tested binary search for sorted lists. No hand-written loop means no off-by-one bugs to debug.

import bisect

Insertion Points, Not Booleans

Instead of true or false, bisect returns an index where a value would be inserted to keep the list sorted. That index is the real power.

a = [1, 3, 3, 3, 7]

bisect_left Leans Left

bisect_left returns the first position where the value could go. For duplicates it lands before all equal items, never after.

bisect.bisect_left(a, 3)  # 1

bisect_right Leans Right

bisect_right returns the position just past the last equal item. With duplicates it lands after every matching value.

bisect.bisect_right(a, 3)  # 4

Count Equal Elements

Subtract the two to count duplicates of a value in O(log n). right minus left gives exactly how many times it appears.

lo = bisect.bisect_left(a, 3)
hi = bisect.bisect_right(a, 3)
print(hi - lo)  # 3

Did the Value Exist?

To check membership, get i from bisect_left and confirm a[i] equals the target. Guard against i reaching the list length first.

i = bisect.bisect_left(a, x)
found = i < len(a) and a[i] == x

First Element At Least X

bisect_left also finds the first element greater than or equal to x. That index points straight at your lower-bound answer.

i = bisect.bisect_left(a, x)  # first >= x

First Element Strictly Greater

Need the first element strictly greater than x? bisect_right gives that index directly, the upper-bound counterpart.

i = bisect.bisect_right(a, x)  # first > x

Insert and Stay Sorted

insort finds the spot and inserts in one call, keeping the list ordered. Handy when you build a sorted structure on the fly.

bisect.insort(a, 5)  # a stays sorted

Search Inside a Window

Optional lo and hi arguments restrict the search to a slice. This avoids copying when you only care about a sub-range.

bisect.bisect_left(a, x, 2, 5)

Keys via a Helper List

bisect compares whole elements, so to search by a field, build a parallel list of just those keys and bisect that instead.

keys = [p[0] for p in pairs]
i = bisect.bisect_left(keys, target)

Quick Check

Reason about duplicates and insertion points.

Recap: Bisect Mastery

You can now find insertion points, count duplicates, and locate lower and upper bounds in log time. Reach for bisect before writing a loop. ✨

Free to start

Learn Coding Interview Prep with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
90
Lessons
360

Frequently Asked Questions

Is the “bisect_left and bisect_right” lesson free?

Yes — the full text of “bisect_left and bisect_right” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO. The Coding Interview Prep course includes 4 lessons in total.

What will I learn in “bisect_left and bisect_right”?

Find insertion points in a sorted list. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “bisect_left and bisect_right” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Classic Binary Search Without Bugs
  2. bisect_left and bisect_right
  3. First True: Predicate Binary Search
  4. Binary Search on the Answer
← Back to Coding Interview Prep