1. What an input-output question tests
An input-output question describes a "machine" that is given one line of words and numbers — the input — and rearranges it one step at a time. You are shown the input and some of the steps, and you must work out the single rule the machine follows, then use it to answer a question: what a particular step looks like, how many steps the machine takes, or which item sits at a given place in some step.
The machine never invents anything. Every step is the previous line with one more item moved into its final place; the items that are not yet placed keep their order and simply shift along. Find the rule from the first step or two, and the rest of the machine is fixed.
- Compare the input with Step I. Exactly one item has jumped to an end — that item tells you the ordering being used.
- Check Step II against Step I to confirm the rule, then you can reproduce every later step yourself.
- Decide what "finished" looks like (numbers in order, words alphabetical, or both) before counting steps.
2. How one step works
The commonest machine arranges the line from the left. At each step it finds the item that belongs next in the final order, pulls it to the extreme left, and lets everything still out of place slide one position to the right. The already-arranged items on the left are never touched again.
Worked example (smallest number first). Input: 85 34 12 67 23 56. The final order is the numbers increasing, so the smallest number, 12, moves to the front first.
Carry on and the machine produces 12 23 34 85 67 56, then 12 23 34 56 85 67, and finally 12 23 34 56 67 85 — the numbers in increasing order. It took 5 steps.
The item moved at Step k always ends up in position k (the kth from the left). That single fact answers most "which item is at Step k" questions without writing the whole line.
3. The common rules
Almost every machine is one of these. Read the first step, name the order, and you are done.
| What Step I does | The rule | Finished line |
|---|---|---|
| Smallest number jumps left | numbers in increasing order | 12 23 34 56 67 85 |
| Largest number jumps left | numbers in decreasing order | 85 67 56 34 23 12 |
| A word jumps left | words in alphabetical order | ant bee cat dog owl |
| The shortest word jumps left | words by length, then alphabet | ant bee crab moth tiger |
| A number and a word both move | numbers one end, words the other | see the next section |
When the line mixes words and numbers, the machine usually brings all the numbers to one side in order and all the words to the other side in alphabetical order. Input cat 52 18 ant 34 dog under "numbers first, then words" becomes 18 cat 52 ant 34 dog at Step I (18 is the smallest number) and finishes as 18 34 52 ant cat dog.
Know the two orders cold: for numbers, smallest-first or largest-first; for words, A before Z. A word beginning with a later letter is "larger". If two words tie on length, the earlier alphabet word wins.
4. Finding the rule quickly
You are not told the rule — you must read it off the steps. Put the input directly above Step I and look at what changed.
- One item has moved to an end. Which end, and is it the smallest/largest number or the first/last word? That is the order.
- Nothing else changed order; items only shifted to make room. If something else moved, you have the rule wrong.
- Confirm with Step II: the item that moves should be the *next* one in the same order.
- If a number and a word both move each step, the machine is arranging numbers at one end and words at the other.
Once two consecutive steps agree with a rule, trust it. The machine is mechanical: there are no exceptions later in the line.
5. Machines that work both ends
Some machines arrange from both ends at once. In each step the smallest number goes to the extreme left and the largest to the extreme right; the items in the middle keep their order. Two items are placed per step, so these machines finish in about half as many steps.
Worked example. Input: 46 18 72 29 63 35.
Step II gives 18 29 46 35 63 72, and the line is fully arranged as 18 29 35 46 63 72 after 3 steps. The left side grows with the smallest numbers, the right side with the largest.
For a both-ends machine, after Step k the first k items and the last k items are already final. The middle is all that is still moving.
6. Counting steps and the last step
To count steps, you do not always have to run the machine to the very end. A left-arranging machine needs one step for every item that is out of place; the last item falls into place for free. In practice: keep stepping until the line is fully ordered, and the number of the step at which that first happens is the answer.
For the alphabetical example pig cat owl bee dog, the machine runs bee pig cat owl dog, bee cat pig owl dog, … and is sorted as bee cat dog owl pig at Step IV: 4 steps in all.
- The last step is simply the fully arranged line. Build it directly from the rule instead of stepping.
- A line that is already partly in order needs fewer steps — count only the items that must still move.
- For a both-ends machine, the number of steps is about half the number of items.
7. Position questions
"In Step III, which item is third from the left?" You only need Step III, not the final line. Run the rule three times and read the position. Counting from the right? Count places from the end of that step, not from the input.
Example on 85 34 12 67 23 56: Step III is 12 23 34 85 67 56, so the 2nd item from the left is 23 and the 2nd item from the right is 67.
Shortcut: in a left-arranging machine, after Step k the first k positions hold the first k items of the final order. If the position you are asked about is within that arranged part, you can read it straight off the final line.
8. Traps and revision
- Wrong end. Check whether items gather on the left or the right before you decide the smallest or largest moves.
- Counting steps from the input. The input is not a step. Step I is the first line the machine produces.
- Re-ordering the settled part. Items already placed never move again; only the unplaced part shifts.
- Mixing up the order for words. Alphabetical means A first; "last word" means nearest to Z.
- Reading a position in the input instead of the step asked for. Always read the line for that exact step.
| You are asked | Do this |
|---|---|
| What is Step k | run the rule k times from the input |
| Which item is mth from an end in Step k | run to Step k, count from that end |
| How many steps | step until fully ordered; that step number is the count |
| The last step | write the fully arranged line straight from the rule |
| Which step is this line | match the number of placed items to the step number |
Practice
45 questions in 3 sets. Every answer comes with its explanation, and each question you get right earns XP.
- Easy · 15
- Medium · 15
- Hard · 15