The Art of Reading Problems
Why Most People Fail Before They Start
The problem isn’t your algorithm knowledge. It’s how you read. Most competitive programmers rush through problem statements, miss critical details, and waste 20+ minutes debugging something that was never the right approach.The best CP players spend 40% of their time understanding the problem and 60% coding. Beginners do the opposite—and that’s why they fail.
The 5-Layer Reading System
Layer 1: The 30-Second Scan
Before reading anything in detail, scan for:| Constraint | Maximum Complexity | Likely Approach |
|---|---|---|
| n ≤ 10 | O(n!) | Brute force, all permutations |
| n ≤ 20 | O(2ⁿ) | Bitmask DP, meet in middle |
| n ≤ 100 | O(n³) | Floyd-Warshall, triple loop |
| n ≤ 1,000 | O(n²) | DP, nested loops |
| n ≤ 100,000 | O(n log n) | Sorting, binary search, segment tree |
| n ≤ 1,000,000 | O(n) | Linear scan, two pointers, prefix sum |
| n ≤ 10⁸ | O(log n) or O(1) | Math formula, binary search |
Layer 2: Story Extraction
CF problems often have elaborate stories. Learn to extract the core problem from the narrative.“Princess Twilight has a magic garden with n flowers arranged in a row. Each flower has a beauty value. The evil Discord wants to steal flowers, but he can only take consecutive flowers. Princess Twilight will be sad if Discord takes flowers with total beauty more than k. Find the maximum number of consecutive flowers Discord can take without making Twilight too sad.”Translation:
- Array of n integers (beauty values)
- Find longest subarray with sum ≤ k
- Pattern: Sliding Window / Two Pointers
The Translation Table
| Story Element | Technical Translation |
|---|---|
| ”Choose k items” | Subset selection |
| ”Arrange in order” | Permutation |
| ”Consecutive elements” | Subarray / Sliding window |
| ”Reach from A to B” | Graph path / BFS |
| ”Minimum operations” | BFS / Greedy / DP |
| ”Count ways” | DP / Combinatorics |
| ”Is it possible” | Yes/No → Often greedy or graph |
| ”Maximum/Minimum” | DP / Greedy / Binary search |
| ”For all pairs” | Often O(n²) or clever O(n) |
Layer 3: Constraint Implications
Constraints tell you the algorithm before you even understand the problem.Layer 4: Example Tracing
Never skip the examples. Trace them by hand.1
Read Input Format
Understand what each line/number represents.
2
Trace Example 1 Manually
Work through it step by step, writing on paper.
3
Verify Against Output
Does your manual trace match the expected output?
4
Read the Note Section
This often explains the solution approach!
5
Create Your Own Test
Make a simpler example to verify understanding.
Layer 5: Edge Case Hunting
Before coding, think about edge cases:Visualization Techniques
Technique 1: Draw the Data Structure
- Array? Draw boxes with indices
- Graph? Draw nodes and edges
- Tree? Draw the hierarchy
- Grid? Draw the 2D layout
- String? Write it out with indices
Technique 2: Trace State Changes
For problems involving operations or transitions:Technique 3: The Two-Example Method
If you don’t understand the problem:- Create the simplest possible valid input
- Create a slightly more complex input
- Solve both by hand
- Look for patterns in your solutions
Mental Models for Common Patterns
”Choosing Subset” Problems
- Can each element be taken independently? → Greedy
- Does order matter? → Permutation
- Does taking one affect others? → DP
”Range Query” Problems
- Static array, sum/min/max query? → Prefix Sum / Sparse Table
- Array with updates? → Segment Tree / Fenwick Tree
- Counting in range? → Merge Sort Tree / Wavelet Tree
”Shortest Path” Problems
- All edges weight 1? → BFS
- Edges have weights? → Dijkstra
- Negative weights? → Bellman-Ford
- All pairs? → Floyd-Warshall
The “What If” Framework
When stuck, ask “What If” questions:Common Reading Mistakes
Practice Exercise: Read Like a Pro
Try reading these problem descriptions and identify:- What is the core problem?
- What pattern does it match?
- What’s the likely complexity?
Given an array of n integers, find the minimum number of operations to make all elements equal. In one operation, you can increase or decrease any element by 1.
Analysis
Analysis
Core problem: Find target value that minimizes total distance
Pattern: Median minimizes sum of absolute differences
Complexity: O(n log n) for sorting, O(n) for sum
Approach: Sort and pick median
Pattern: Median minimizes sum of absolute differences
Complexity: O(n log n) for sorting, O(n) for sum
Approach: Sort and pick median
You have n items with weights w[i] and values v[i]. Find maximum value of items you can take such that total weight doesn’t exceed W.
Analysis
Analysis
Core problem: 0/1 Knapsack
Pattern: DP with state (index, remaining capacity)
Complexity: O(n × W)
Approach: DP table or recursive with memoization
Pattern: DP with state (index, remaining capacity)
Complexity: O(n × W)
Approach: DP table or recursive with memoization
Given a string s of length n, find the longest palindromic substring.
Analysis
Analysis
Core problem: Longest palindromic substring
Pattern: Expand from center / DP / Manacher
Complexity: O(n²) naive, O(n) with Manacher
Approach: For CP, usually O(n²) is fine if n ≤ 5000
Pattern: Expand from center / DP / Manacher
Complexity: O(n²) naive, O(n) with Manacher
Approach: For CP, usually O(n²) is fine if n ≤ 5000
The 3-Minute Rule
Before coding anything:- Minute 1: Scan constraints, identify complexity bound
- Minute 2: Extract core problem from story
- Minute 3: Trace smallest example, verify understanding
Reading Checklist (Print This)
Key Takeaways
Constraints First
Constraints tell you the algorithm before the problem does.
Trace Examples
If you can’t trace Example 1 by hand, you don’t understand the problem.
Translate Stories
Convert narrative to technical terms: “consecutive” → subarray, “reach” → path.
Visualize Everything
Draw arrays, graphs, grids. Visual thinking catches errors.