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 First True Beacon

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 First True Beacon.

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

How you win

  1. 1Recognize when First-true boundary template matches the clues
  2. 2Keep this true after every move: all indices before left are proven false and right is the earliest remaining true candidate
  3. 3Reach the result within O(log n)

Rules and pressure

  • Target cost: O(log n)
  • State rule: all indices before left are proven false and right is the earliest remaining true 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 result first. With no true value, the search reaches n and returns -1. Lesson questions refer to the original example.

Live algorithm trace

First true: locate the transition

Complete execution
1 of 11

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

F means false and T means true. Search [0, 6); position 6 is a sentinel for no true value, never a cell to read.

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

Every position before left is false. If right<n, right is true and remains a possible answer.

Cost target

O(log(n+1)) predicate checks and O(1) auxiliary space

Finding true is not yet proof that it is the first true.

Your call · mid=3 is true. How do you find the first true value?

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