Learn › Competitive programming intro

Reading statements and constraints

A repeatable workflow for turning problem text into edge cases, data structures, and a target Big-O. · 10 min read

Concept

A problem statement is not just a story. It is a specification. The story gives context, but the important pieces are the input format, output format, constraints, examples, and hidden assumptions. Read it like an engineer reading an API contract.

The five-pass reading workflow

Use this workflow before coding:

  1. Restate the task. What exact value must be printed or returned?
  2. Name the inputs. What are the sizes and value ranges?
  3. Find the complexity target. What Big-O can fit?
  4. List edge cases. Empty, one item, duplicates, negative values, all equal, already sorted, disconnected graph, impossible answer.
  5. Choose data structures. Array, hash map, heap, stack, graph, DP table, or something else.

From constraints to Big-O

Rough mental limits are enough most of the time. A judge that allows one or two seconds often tolerates around 10^7 to 10^8 simple operations in C++, less in Python. The exact number varies, but the categories are stable:

  • n <= 20: subsets, bitmasks, backtracking, O(2^n) might work.
  • n <= 500: O(n^3) can be reasonable.
  • n <= 2,000: O(n^2) is plausible.
  • n <= 100,000: aim for O(n log n) or O(n).
  • n <= 1,000,000: usually O(n) and memory-aware.
A common upgrade path
  1. n = 100,000 and O(n^2) — That is 10^10 pair checks, far too many.
  2. sort + scan — Sorting costs O(n log n), then one linear pass.
  3. hash map scan — If order is not needed, one pass can often be O(n) average.

Result: The constraints decide whether brute force survives

Reading examples correctly

Examples are not tests to memorize; they are small explanations of the input/output contract. Trace them by hand. Ask what would change if the array had one item, if the target was absent, if the graph had no path, if values were negative, or if multiple answers were possible.

Hidden tests and edge cases

Hidden tests usually cover exactly the cases beginners skip: boundaries, maximum sizes, repeated values, empty structures, overflow, and impossible cases. Before submitting, write your own tiny tests and your own stressful tests.

Practice: Running Sum (Easy) — Build the array of prefix sums.

Practice: Binary Search (Easy) — Find a target in a sorted array in O(log n).

Previous: C to C++ bridge · Next: First problem walkthroughs

More in Competitive programming intro