Problem-Solving Guide β
Mastering competitive programming isn't about memorizing algorithms β it's about developing systematic thinking habits and disciplined problem-solving techniques. This guide provides a proven framework that transforms chaotic problem-solving into a structured, repeatable process.
π― The Golden Rule: Think First, Code Last β
90% of your time should be spent understanding and planning. Only 10% should be spent coding.
Most programmers fail because they rush to code before they truly understand the problem. This guide will teach you to resist that urge and develop the discipline of top-tier competitive programmers.
Phase 1: π Deep Problem Understanding β
Step 1: The Two-Read Rule β
- First read: Get the general idea. Don't worry about details yet.
- Second read: Focus on specifics. Underline or highlight key information.
Step 2: Extract the Core Elements β
Ask these questions and write down the answers:
Input Analysis:
- What exactly am I given?
- What's the format? (single line, multiple test cases, etc.)
- What are the data types? (integers, strings, arrays, etc.)
Output Analysis:
- What exactly should I produce?
- What's the exact format? (spaces between numbers? newlines? case-sensitive?)
- Are there special cases for the output?
Constraints Analysis:
- What are the limits on input size?
- What are the value ranges?
- How much time do I have? (usually 1-2 seconds)
Step 3: Constraint-Driven Complexity Planning β
Use this table to guide your approach:
| Size | Time Complexity | Approach |
|---|---|---|
| n β€ 10 | Any approach works | Brute force, recursion |
| n β€ 100 | O(nΒ²) or O(nΒ³) | Nested loops, simple DP |
| n β€ 1,000 | O(nΒ²) | Two nested loops max |
| n β€ 10,000 | O(n log n) or O(nβn) | Sorting, optimized algorithms |
| n β€ 100,000 | O(n log n) | Sorting, segment trees, binary search |
| n β€ 1,000,000 | O(n) or O(n log log n) | Linear algorithms, sieve |
| n > 1,000,000 | O(log n) or O(1) | Math formulas, precomputation |
Phase 2: βοΈ Paper-Based Problem Solving β
This is where champions are made. Never skip this phase.
Step 1: Restate the Problem β
Write the problem in your own words in 1-2 sentences. This forces true understanding.
Example: "Given an array of integers, find the second-largest unique value."
Step 2: Manual Sample Tracing β
For each given example:
- Write the input clearly
- Step through your mental process
- Verify you get the expected output
- If not, re-read the problemβyou misunderstood something
Step 3: Create Your Own Test Cases β
Design cases that could break your solution:
Essential Test Cases:
- Minimum case: Smallest possible valid input
- Maximum case: Largest input within constraints
- Edge cases: Boundary conditions, special values
- Corner cases: Empty inputs, all same values, negative numbers
- Stress cases: Random large inputs (if time permits)
Step 4: Algorithm Design on Paper β
- Start with brute force: How would you solve this if efficiency didn't matter?
- Analyze the brute force: Count the operations. Too slow?
- Optimize step by step: What repeated work can you eliminate?
- Write pseudocode: High-level steps, not actual code
Phase 3: π Strategic Problem Analysis β
Pattern Recognition Checklist β
Is this a known problem type?
- Sorting/Searching problem?
- Graph traversal (BFS/DFS)?
- Dynamic programming (optimal substructure)?
- Greedy algorithm (local optimal β global optimal)?
- Mathematical formula/pattern?
- Data structure problem (stack, queue, priority queue)?
Problem Transformation Techniques:
- Can I reduce this to a simpler problem?
- Can I solve the reverse/opposite problem?
- Can I solve a smaller version first?
- Are there symmetries I can exploit?
- Can I precompute something to make queries faster?
If you find yourself stuck, don't get obssessed with a single idea and try these approaches:
- Simplify: Solve for n=1, n=2, n=3 and look for patterns
- Visualize: Draw the problem if possible
- Transform: Change variables, coordinate systems, or perspective
Phase 4: π Systematic Implementation β
Pre-Coding Checklist β
- I have a clear algorithm written in pseudocode
- I've traced through my algorithm on paper
- I know the time and space complexity
- I know which data structures I'll use
Implementation Strategy β
1. Template First Start with input/output scaffolding:
// Read input
// Process
// Output result2. Build Incrementally
- Implement one function/section at a time
- Test each piece before moving on
- Use placeholder values to test structure
3. Defensive Programming
- Use meaningful variable names
- Add comments for complex logic
- Consider integer overflow issues
Common Implementation Pitfalls β
- Off-by-one errors: Double-check array indices and loop bounds
- Integer overflow: Use long long if values can exceed 10βΉ
- Precision issues: Use appropriate data types for floating-point
- Case sensitivity: Match output format exactly
- Trailing spaces/newlines: Follow format specifications precisely
Phase 5: β Rigorous Testing and Debugging β
Testing Protocol β
Level 1: Sample Tests
- Run all provided examples
- If any fail, debug immediatelyβdon't proceed
Level 2: Edge Case Tests
- Test your manually created edge cases
- Pay special attention to boundary values
Level 3: Stress Testing (if time permits)
- Generate random test cases
- Compare with brute force solution
- Look for patterns in failures
Debugging Methodology β
When Wrong Answer:
- Re-read the problem: 50% of wrong answers are misunderstandings
- Check sample cases: Trace through manually again
- Print intermediate values: See where logic diverges from expectation
- Simplify the input: Use smaller cases to isolate the bug
When Time Limit Exceeded:
- Verify complexity: Count operations in your algorithm
- Look for infinite loops: Check loop termination conditions
- Optimize bottlenecks: Profile which parts are slowest
- Consider algorithm change: Sometimes you need a fundamentally different approach
When Runtime Error:
- Array bounds: Check all array accesses
- Null pointers: Verify pointer/reference validity
- Stack overflow: Check recursion depth
- Division by zero: Verify denominators
More advice on debugging is available in the Debugging Practices guide.
Phase 6: π Post-Solution Reflection β
Learning Extraction Process β
Immediately After Solving:
- What was the key insight that led to the solution?
- Which part took the longest? (reading, thinking, coding, debugging)
- What would I do differently next time?
After Reading Editorial (if available):
- Was there a simpler solution I missed?
- What techniques did the editorial use that I didn't know?
- How can I recognize similar problems in the future?
Personal Problem Database β
Keep a log with:
- Problem name and difficulty
- Key techniques used
- Time spent on each phase
- Mistakes made and lessons learned
π The Mindset of Champions β
Remember: Programming contests reward systematic thinking and careful execution, not just algorithmic knowledge.
The 90/10 Principle: If you spend 90% of your time thinking and planning, the remaining 10% (coding) becomes almost trivial.
Consistency beats intensity: Daily practice for 30 minutes is better than weekend marathons.
Mistakes are data: Every wrong submission teaches you something about your thought process.
Patience pays off: The best problem solvers take their time to understand fully before coding.

