Tools / Binary Search

Binary Search

Step through the halving strategy from picoCTF's Binary Searchchallenge, adjust targets, and watch the range collapse in real time.

Binary Search Explorer

Watch how the search space shrinks on every guess. Set a target value, step manually, or auto-play the logic used in the challenge.

Known low

1

Known high

1000

Current guess

500

Use service feedback to guide the next guess.

Client input

500

Service response

Click the button that matches the challenge's reply for the current guess.

Search history(0 / 10 guesses)

No guesses yet. Step through the algorithm to start capturing the decision trail.

Tree view (depth ≤ 9)

Nodes highlighted when the algorithm inspects their midpoint.

depth 0

500

1 - 1000

depth 1

250

1 - 499

depth 1

750

501 - 1000

depth 2

125

1 - 249

depth 2

375

251 - 499

depth 2

625

501 - 749

depth 2

875

751 - 1000

depth 3

62

1 - 124

depth 3

187

126 - 249

depth 3

312

251 - 374

depth 3

437

376 - 499

depth 3

562

501 - 624

depth 3

687

626 - 749

depth 3

812

751 - 874

depth 3

938

876 - 1000

depth 4

281

251 - 311

depth 4

343

313 - 374

depth 4

406

376 - 436

depth 4

468

438 - 499

depth 4

531

501 - 561

depth 4

593

563 - 624

depth 4

656

626 - 686

depth 4

718

688 - 749

depth 5

390

376 - 405

depth 5

421

407 - 436

depth 5

452

438 - 467

depth 5

484

469 - 499

depth 5

515

501 - 530

depth 5

546

532 - 561

depth 5

577

563 - 592

depth 5

609

594 - 624

depth 6

444

438 - 451

depth 6

460

453 - 467

depth 6

476

469 - 483

depth 6

492

485 - 499

depth 6

507

501 - 514

depth 6

523

516 - 530

depth 6

538

532 - 545

depth 6

554

547 - 561

depth 7

472

469 - 475

depth 7

480

477 - 483

depth 7

488

485 - 491

depth 7

496

493 - 499

depth 7

503

501 - 506

depth 7

511

508 - 514

depth 7

519

516 - 522

depth 7

527

524 - 530

depth 8

486

485 - 487

depth 8

490

489 - 491

depth 8

494

493 - 495

depth 8

498

497 - 499

depth 8

501

501 - 502

depth 8

505

504 - 506

depth 8

509

508 - 510

depth 8

513

512 - 514

depth 9

491

491 - 491

depth 9

493

493 - 493

depth 9

495

495 - 495

depth 9

497

497 - 497

depth 9

499

499 - 499

depth 9

502

502 - 502

depth 9

504

504 - 504

depth 9

506

506 - 506

How binary search works

Binary search locates a target value in a sorted range by repeatedly halving the search space. Starting with the full range, you guess the midpoint. If the guess is too high, the upper bound moves down to just below the midpoint; if too low, the lower bound moves up. Each comparison eliminates half the remaining candidates, so a range of 1,000 values is resolved in at most 10 guesses (log₂1000 ≈ 10).

In the picoCTF Binary Search challenge, you connect to a remote server over SSH and are given a limited number of guesses to find a hidden number between 1 and 1000. The server tells you whether your guess is too high or too low. Using the halving strategy, you always find the answer within the guess limit. This visualizer lets you practice and verify each step before submitting.

The algorithm has a time complexity of O(log n), which is far more efficient than a linear scan at O(n). This makes it the standard approach for sorted-array lookups in both competitive programming and real-world software.

A common mistake is rounding the midpoint inconsistently. Always use integer division (floor): mid = (low + high) / 2 (rounded down). If you round up on some steps and down on others, you risk an infinite loop at boundaries. This visualizer always floors the midpoint, matching the behavior expected by the challenge server.