Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Algorithms & Competitive Programming — Alpha School

Lesson 19: Contest Strategy — Reading Problems and Planning

Knowing algorithms is only half of competitive programming. The other half is strategy: how to spend a limited contest well. Most contests give you a set of problems and a fixed time — two hours is common — and the problems are deliberately ordered from easiest to hardest, though not always. The first skill is reading: read every problem before writing any code. A problem that looks hard may be easy once you see the trick, and a problem that looks easy may hide a trap in its limits. Spend the first ten minutes reading, not coding.

For each problem, run a short checklist before you code. What are the input and output formats, exactly? What are the limits, and what complexity do they imply? What are the edge cases — empty input, one item, everything equal, maximum size? Can you solve a small example by hand, and does your idea reproduce it? If you cannot solve a small case by hand, you do not yet understand the problem, and coding will only produce a faster confusion. Write the small case down, solve it on paper, and only then open your editor.

The second skill is time management. Solve the easiest problem first — a solved problem is points in the bank, and confidence for the rest. If you are stuck for more than fifteen minutes without progress, switch problems; a fresh mind often sees what a stuck one cannot, and the other problem may be easier than it looked. Keep a template file with your favourite snippets — fast input reading, a Union-Find class, a sieve — so you are not rewriting them under pressure. And test as you go: run your solution on the sample input, on your edge cases, and on a random small input checked against a slow but obviously correct version.

Debugging under pressure

When a solution fails, do not guess. Read the verdict: a wrong answer means the logic is wrong somewhere — test the smallest failing case you can construct. A time-limit error means the complexity is too high — re-read the limits and look for the repeated work. A runtime error usually means an edge case — an empty list, an index out of range, a recursion too deep. The most common contest bug is not a wrong algorithm but a misread problem: the input format, the output format, or the exact definition of the answer. When stuck, re-read the statement slowly, out loud if you can. More solutions are saved by re-reading than by rewriting.

Finally, remember what a contest is for. The rating, the leaderboard and the prizes are real, but the durable reward is the skill: each contest teaches you a trick, a bug, or a gap in your knowledge. Keep a notebook of what went wrong after every contest — one line per mistake — and read it before the next one. Improvement in competitive programming is rarely dramatic; it is the slow accumulation of solved problems, recognised patterns, and mistakes not repeated. That is a process you can start today, and this course has given you the tools.

Going further

Speed matters in the mechanics of a contest, not just in the algorithm. Reading input with Python's built-in input() in a loop is slow for large inputs; reading everything at once with sys.stdin.read().split() and parsing the tokens is far faster, and it is the standard pattern in Python contest solutions. Similarly, building output in a list and printing it once with "\n".join() beats printing line by line. These are small changes, but on a large input they can be the difference between passing and timing out — and they cost nothing to adopt.

The work of a contest does not end when the timer stops. The practice called up-solving is to go back afterwards and solve the problems you could not finish, with no time pressure and with the editorial open. Up-solving is where most of the learning happens: the contest shows you what you cannot yet do, and the editorial shows you how. A contestant who up-solves one problem after every round improves far faster than one who only competes. Make it a habit, and keep the solutions in your notebook alongside the mistakes.

After a contest, the most valuable document you own is your notebook of mistakes. Write down each one in a single line — "misread the output format", "forgot the empty input", "used a list where a set was needed" — and read the list before the next contest. Most competitors repeat the same three or four mistakes for months, because they never write them down. A notebook turns a vague feeling of having made silly errors into a concrete checklist, and a checklist is something you can actually act on when the clock is running.

Key Terms

Contest
A timed competition where you solve a set of algorithmic problems, usually within a couple of hours.
Sample input/output
The example given in a problem statement, used to check your understanding and your solution.
Time-limit error (TLE)
The verdict when a solution runs longer than the allowed time; usually a complexity problem.
Wrong answer (WA)
The verdict when a solution's output differs from the expected output; usually a logic or edge-case bug.
Runtime error (RE)
The verdict when a solution crashes, such as an index error or a stack overflow.
Template file
A personal file of ready-made snippets (fast input, Union-Find, sieve) reused across contests.
Brute force
A slow but obviously correct solution, used to check a fast solution on small random inputs.

Discussion Questions

  1. Why is it worth spending the first ten minutes of a contest reading every problem instead of starting to code immediately?
  2. You have been stuck on one problem for twenty minutes. What should you do, and why is switching problems not giving up?
  3. Why is a slow but correct brute-force solution useful even when you know it will time out on the full input?

Practice Questions

  1. What should you check first when a solution gets a wrong-answer verdict?
  2. What does a time-limit error usually mean?
  3. Why solve the easiest problem first?
  4. What is a template file, and what belongs in it?
  5. How can a brute-force solution help you test a fast solution?
Show answers
  1. The smallest input where it fails, and your understanding of the problem. Re-read the statement: most wrong answers come from a misread requirement or an unhandled edge case.
  2. The complexity is too high for the input limits. Look for repeated work — a hidden O(n) inside a loop, or an algorithm of the wrong complexity class.
  3. It banks points early and builds confidence. It also warms up your thinking; a stuck start on the hardest problem wastes the whole contest.
  4. A personal file of ready-made snippets such as fast input, Union-Find and a sieve. It saves time and prevents re-typing errors under pressure.
  5. Run both on many small random inputs and compare outputs. Any disagreement reveals a bug in the fast solution; the brute force is slow but obviously correct.
Keep reading to complete this lesson