Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Insertion Gate

Learn
Play
Prove
Problem context and objectives
Mission briefing

Signal rescue operator · A beacon is hidden inside an ordered frequency band

Lock onto the signal without testing every frequency: The Insertion Gate.

Each probe costs precious time; searching the wrong half moves the rescue team farther away.

How you win

  1. 1Recognize when Lower-bound search matches the clues
  2. 2Keep this true after every move: every index before left is too small and every index at or after right is a valid insertion candidate
  3. 3Reach the result within O(log n)

Rules and pressure

  • Target cost: O(log n)
  • State rule: every index before left is too small and every index at or after right is a valid insertion candidate

New words in this mission

Open a term for a plain-language explanation.
O(log n)+

The work grows by one step when the input roughly doubles. Binary search achieves this by discarding half of the remaining search space each time.

Lesson 1 of 3

Predict the first qualifying position. Equality still searches earlier duplicates. The position after the array is also a valid answer. Lesson questions refer to the original example.

Live algorithm trace

Lower bound: keep the first qualifying position

Complete execution
1 of 8

Live indices: 0, 1, 2, 3. Short blocks are discarded candidates.

Search cells in [0, 4). Position 4 after the array is also a possible insertion answer, not an array cell.

1left, right = 0, len(nums)
2while left < right:
3 mid = left + (right - left) // 2
4 if nums[mid] < target: left = mid + 1
5 else: right = mid
6return left
target = 5left = 0right = 4comparisons = 0operation = initializeendPositionPossible = yes
Truth to preserve / Cost target
Truth to preserve

Every value before left is below target. The answer lies in [left, right], while inspected cells lie in [left, right).

Cost target

O(log(n + 1)) comparisons and O(1) auxiliary space

Keep a qualifying midpoint as a possible answer.

Your call · mid=2 contains 5 and target is 5. How do you find the first qualifying position?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead