Queue

Arrange the children in a queue so that every rule about who stands where is satisfied at once. Each level sits right at the edge of what is possible: add one more child and there would be no arrangement at all.

How to play

Tap two children and they swap places; that is the only move you have.

  1. Think in blocks Break the line into a few blocks, each descending inside itself. A rising run can then take at most one child from each block.
  2. Make the blocks rise Put the shortest block first and the tallest last. Then a falling run cannot leave the block it starts in.
  3. Count the blocks Use exactly as many blocks as the rising limit, each as long as the falling limit. That is the arrangement, and it fits exactly.
Full instructions

In depth

How to solve the Queue ordering puzzle

Queue is an ordering puzzle built on a real theorem from combinatorics. You are given a line of children of different heights and two limits: one for the longest rising run and one for the longest falling run. Your job is to rearrange the line by swapping pairs so that nobody can pick out a rising run longer than the first limit or a falling run longer than the second. The runs do not have to be adjacent, which is what makes the puzzle genuinely hard. This guide explains the rules, the controls, the mathematics behind the puzzle, and the strategy that leads to a clean solution.

Screenshot: How to play Queue
How to play Queue as it appears when the page opens.

Rules and objective

You see a row of children drawn as bars of different heights. Each child has a number. Two limits are displayed: a maximum allowed length for any rising subsequence and a maximum allowed length for any falling subsequence. A rising subsequence is any set of children, taken left to right along the line, whose heights go up. They do not have to stand next to each other. Any children selected in order from left to right count, as long as each one is taller than the previous.

A falling subsequence works the same way but in the downward direction. You need both limits satisfied at the same time. The longest rising run must be at or below the first limit, and the longest falling run must be at or below the second. The puzzle is solved the moment both conditions are met.

Controls and how to play

Tap one child to select them, then tap another to swap their places. Tapping the same child a second time deselects them. The number of swaps is tracked and displayed as your score, with fewer being better. Two counters show the current longest rising run and the current longest falling run, each compared against its limit. A counter turns green when it meets the limit and stays highlighted when it does not.

The Class selector offers three sizes. Small uses a two by three limit, giving six children. Medium is the default at three by three, giving nine children. Large raises the limits to three by four, giving twelve. Press New game at any time to get a fresh scrambled line at the selected size.

The mathematics behind the puzzle

The puzzle is based on a result proved by Paul Erdos and George Szekeres in 1935. Their theorem says that any sequence of more than the product of the two limits must contain either a rising run past the first limit or a falling run past the second. The bound is exact: with exactly that many elements, it can always be arranged to avoid both, and with one more element it never can.

Every queue in this puzzle sits right at that boundary. If the limits are three rising and three falling, you get nine children, which is three times three. With ten children it would be impossible no matter how you shuffled them. That is not a difficulty setting but a mathematical fact, which is what the tagline on the page means. The result is usually seen as the starting point of Ramsey theory in geometry, and the original question that prompted it was asked by Esther Klein, who later married Szekeres. Erdos called it the happy ending problem for that reason.

The block strategy

The arrangement that always works has a clean structure. Divide the children into blocks. The number of blocks should equal the rising limit, and the size of each block should equal the falling limit. Inside each block, arrange the children in descending order. Then make sure the blocks themselves go in ascending order, meaning the tallest child in the first block is shorter than the shortest child in the second block.

Why does this work? A rising run can take at most one child from each block, because within a block the heights fall. So the longest rising run is at most the number of blocks, which equals the rising limit. A falling run can never leave the block it starts in, because moving to the next block would mean jumping to a taller child. So the longest falling run is at most the size of one block, which equals the falling limit.

Fixing the worse number first

When you start swapping, look at the two counters. Whichever limit is further over is the one to attack first. If the longest rising run is five against a limit of three but the longest falling run is only four against a limit of three, focus on breaking up the rising run. The two limits fight each other. Breaking a long rising run tends to build a long falling one, because pulling a tall child out of a rising sequence often drops it into the middle of a descending stretch.

The key insight is that you are not looking for any arrangement that meets both limits. You are looking for the block arrangement described above, because it is the only structure that guarantees both limits are met. Every swap should move the line closer to that block pattern. If a swap fixes one counter but makes the other worse by the same amount, it was not progress.

Working out the blocks by hand

With nine children numbered one through nine and limits of three and three, the target arrangement is: 3, 2, 1, 6, 5, 4, 9, 8, 7. The first block is three, two, one, which is descending. The second block is six, five, four. The third is nine, eight, seven. The blocks themselves rise because three is less than four and six is less than seven.

On the Large setting with twelve children and limits of three rising and four falling, you need three blocks of four: 4, 3, 2, 1, 8, 7, 6, 5, 12, 11, 10, 9. Each block has four children in descending order, and the blocks ascend. Knowing the target before you start swapping saves a lot of trial and error, because every swap can be planned to move one child toward its correct position in the target.

Common mistakes beginners make

The most common mistake is treating the runs as adjacent. Beginners swap two neighbours and expect the longest run to drop, but the longest run might be spread across the entire line. Moving child five from position two to position eight does nothing if the rising run passes through positions one, four, six, and nine. You need to think about which children are part of the long run and move one of them to a place where it no longer extends the sequence.

Another error is swapping randomly until both counters happen to go green. On the Small size this can sometimes work by luck, but on Medium and Large the number of possible arrangements is enormous and random swapping almost never lands on a valid one. The block method is far more reliable.

Some beginners also focus entirely on one counter while ignoring the other. They rearrange the line to kill every long rising run and end up with a perfectly descending sequence, which is the longest possible falling run. Both limits must be met at the same time, and the block structure is the only way to guarantee that.

Difficulty levels in detail

Small uses limits of two rising and three falling, giving six children in two blocks of three. This is the gentlest introduction because there are only six items to arrange. Medium uses three rising and three falling with nine children, which is the setting where the puzzle has enough depth to be interesting. Large uses three rising and four falling with twelve children and is the hardest setting, not because the block structure is different but because finding the right swaps takes more careful counting.

On all levels, lower swap counts are better. The game tracks your best score across sessions in local storage. Aiming for the minimum number of swaps is a separate challenge once you can solve the puzzle reliably.

Privacy and how the puzzle runs

The entire puzzle runs inside your browser. No data is sent to a server, there is no account to create, and your best score is stored in local storage on this device only. It will not follow you to another machine or another browser on the same machine. Once the page has loaded, the game works fully offline, so you can work through puzzles anywhere without a connection.

Help

How to play Queue

Tap two children and they swap places; that is the only move you have. The runs being counted need not stand next to each other, since any children you could pick out in order along the line count as one, which is why an arrangement that looks jumbled can still break the limit. Both the rising limit and the falling one must hold at once.

Think in blocks

Break the line into a few blocks, each descending inside itself. A rising run can then take at most one child from each block.

Make the blocks rise

Put the shortest block first and the tallest last. Then a falling run cannot leave the block it starts in.

Count the blocks

Use exactly as many blocks as the rising limit, each as long as the falling limit. That is the arrangement, and it fits exactly.

Fix the worse number first

Both counters are shown. Whichever is further over is the one to attack.

The happy ending problem

In any line of more than the two limits multiplied together, you can always find a run rising past the first limit or falling past the second. Paul Erdős and George Szekeres proved it in 1935, and the bound is exact: with exactly that many it can always be arranged, and with one more it never can. The arrangement that works is the obvious one once seen — blocks that descend inside themselves, stacked so the blocks ascend. The paper came out of a problem Esther Klein put to their circle in Budapest, about points in the plane and convex shapes; Erdős named it the happy ending problem because Klein and Szekeres later married. The result is usually credited as the beginning of Ramsey theory in geometry, and the original question Klein asked — how many points force a convex polygon of a given size — was only settled for hexagons in 2006, by computer.

Good to know

The runs need not be adjacent

Any children taken in order along the line count, which is why long runs are so hard to avoid.

The two limits multiply

Three rising and three falling allows nine children. Never more.

Every queue here is at the edge

The class is exactly as large as the limits allow, so there is no room to spare.

Swapping is free

Only the number of swaps is scored, and there is no limit on them.

Common questions

Do the runs have to be next to each other?
No. Any children taken in order along the line count, which is what makes it hard.
Is every queue solvable?
Yes, and only just — the class is exactly as large as the limits allow.
What if there were one more child?
Then it would be impossible, however you arranged them. That is the theorem.
Is there only one answer?
No, there are many. The block arrangement is simply the easiest to find.
Why is it called the happy ending problem?
Because the two mathematicians who worked on the original question married each other.