Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 16 min to read

Play it as a journeyTutor comes with a plan

Which way does it turn: cross products and convex hull

Algorithm Design Patterns
Algorithm Design Patterns · Open board

The board is a map of the patterns that solve most problems. This lesson adds a small one for points on a flat page: one multiplication that says which way a path turns.

Take a made-up farmer, Ramesh. He has seven wooden pegs in a field and one rope. He wants the rope to go round the outside of all the pegs, pulled tight, so that every peg is inside the rope or on it. Which pegs does the rope actually touch?

A person looks at the pegs and sees the answer. A program has only the peg positions, as pairs of numbers. It cannot look. This lesson gives it one cheap test that works on numbers alone, and builds three things on that test.

By the end you will be able to find which way a path turns, check whether two straight lines cross, find the tight rope round a set of points (the convex hull), and count the most points on one straight line, without ever dividing.

The history (optional)

The method used here to find the rope is called the monotone chain. It was published in 1979 by A. M. Andrew. It is a version of an older method, Graham scan, that sorts the points first. Its running time is O(n log n), and O(n) when the points are already sorted.

Before you start. Fibonacci in forty steps: matrix exponentiation gave you a 2 by 2 table of numbers and the habit of multiplying across and adding up, one cell at a time. The test below is one such cross-multiplication. Foundations #05: Sorting & Searching gave you sorting, and here we sort points by x and then by y. Foundations #04: Hashing (O(1) "yaad") gave you the dictionary, which we use to count points that share a direction. What is near me (geohash cells, and fences by ray casting) counted how many edges a line crosses to tell inside from outside, and here you will test whether two single lines cross.

Lesson 83 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Three pegs and one number: the cross product
  • Do two ropes cross? Four turns decide
  • Which pegs does the rope touch: the convex hull
  • Building the rope in one pass: the monotone chain
  • Most points on one straight line, with no division
  • Why the sign means left or right
  • Overflow and floats
  • The code, run on 2026-11-28 with Python 3
  • The hull, and two free LeetCode problems
  • Limits and trade-offs

Terms it explains: cross product, collinear, convex hull, monotone chain, Common mistakes, backtracking, brute force, constraint satisfaction, divide & conquer, dynamic programming, exchange argument, greedy, Master theorem, pruning, the meta-method.

Lab

Grabbing the biggest coin that fits gives the fewest coins with 1, 5, 10 and 25. Does it with any coins?

Pick 1, 3, 4 under The coins, set The amount to make to 6, and watch Coins greedy wasted.

Open The greedy bet

Review DSA