Detailed Problem Analysis
From the introduction to algorithm curriculum
TL;DR
Detailed problem analysis is the crucial first step in algorithm design, ensuring you fully understand a problem before trying to solve it. It involves breaking down the problem, identifying inputs and outputs, constraints, and potential edge cases. This thorough understanding prevents wasted effort and leads to more effective and correct algorithms.
1. The Mental Model
Think of detailed problem analysis like planning a road trip. Before you even start the car, you figure out your destination, where you're starting, what roads you can take (constraints), and potential detours or roadblocks (edge cases). You wouldn't just jump in and drive, right?
2. The Core Material
Detailed problem analysis is about asking the right questions to fully grasp what a problem is asking for. It's the detective work you do before becoming the architect.
2.1 Understanding the Problem Statement

Photo by Vitaly Gariev on Pexels
First, read the problem carefully, perhaps multiple times. Identify the main goal. What exactly is the problem asking you to achieve or calculate?
2.2 Identifying Inputs

Photo by Ann H on Pexels
What information will your algorithm receive?
* Data Types: Are they integers, strings, arrays, floating-point numbers?
* Format: How is the data structured (e.g., a list of numbers, a single string)?
* Examples: Are there example inputs provided? If not, create some simple ones.
2.3 Identifying Outputs

Photo by Ann H on Pexels
What should your algorithm produce?
* Data Types: What type of data should the result be?
* Format: How should the output be presented (e.g., a single number, a sorted list, a boolean)?
* Examples: What should the output be for your example inputs?
2.4 Defining Constraints

Photo by MART PRODUCTION on Pexels
Constraints are limitations or conditions placed on the inputs or the solution. These are vital because they often dictate which algorithms are feasible.
* Input Size: How large can the input be (e.g., array length up to 10^5, number values between 1 and 1000)?
* Time/Space Limits: Are there limits on how fast your algorithm must run or how much memory it can use?
* Value Ranges: What are the minimum and maximum possible values for numbers?
2.5 Spotting Edge Cases and Special Conditions
Edge cases are extreme or unusual scenarios that might break a general solution. Missing these often leads to bugs.
* Empty Inputs: What if a list is empty, or a string is null?
* Single Element: What if a list has only one element?
* Maximum/Minimum Values: What happens if inputs are at the very upper or lower bounds of their possible range?
* Duplicates: Are duplicates allowed, and how should they be handled?
* Invalid Inputs: What if the input doesn't conform to expectations (e.g., negative numbers where only positive are expected)? (Though often, competitive programming problems assume valid inputs unless specified).
Here’s a process flow for detailed problem analysis:
graph TD
A["Read Problem Statement (Carefully)"] --> B["Identify Goal & Key Verbs"]
B --> C["What are the Inputs? (Types, Formats)"]
C --> D["What are the Outputs? (Types, Formats)"]
D --> E["What are the Constraints? (Size, Values, Time/Space)"]
E --> F["Consider Edge Cases (Empty, Single, Max/Min, Duplicates)"]
F --> G["Develop Simple Examples & Expected Outputs"]
G --> H["Is Problem Fully Understood?"]
H -- Yes --> I["Ready for Algorithm Design"]
H -- No --> A
3. Worked Example
Let's analyze this problem: "Given an array of integers, return the sum of all positive numbers in the array. If the array is empty or contains no positive numbers, return 0."
- Problem Statement: Sum all positive numbers in an integer array. Return 0 for empty or all-negative/zero arrays.
- Inputs:
- Type: An array (or list) of integers.
- Format:
[1, -4, 7, 12],[],[-1, -2, -3]
- Outputs:
- Type: A single integer.
- Format:
15,0
- Constraints:
- The problem statement doesn't specify array size or value ranges, so we'd assume typical integer limits (e.g., -2^31 to 2^31-1) and array lengths that fit in memory. For competitive programming, usually N <= 10^5 or 10^6. Let's assume N <= 1000 for simplicity.
- Edge Cases:
- Empty array:
[]-> Should return0. - Array with no positive numbers:
[-1, -2, -3]-> Should return0. - Array with only positive numbers:
[1, 2, 3]-> Should return6. - Array with mixed positive/negative/zero:
[1, -4, 0, 7, 12]->1 + 7 + 12 = 20. Zero is not positive. - Single positive number:
[5]->5. - Single negative number:
[-5]->0.
- Empty array:
This analysis helps me immediately think about iterating through the array, checking each number if it's greater than 0, and adding it to a running sum, initializing the sum to 0.
4. Key Takeaways
- Understand the goal: Know precisely what the problem is asking you to do.
- Identify inputs: Be clear about the data types and format of your algorithm's input.
- Define outputs: Know exactly what your algorithm should produce and in what format.
- Pin down constraints: These limitations are critical for choosing efficient algorithms and data structures.
- Hunt for edge cases: Always consider the extremes and unusual inputs; they're common sources of bugs.
- Don't skip it: Thorough analysis saves significant time and frustration later on.
- Use examples: Work through simple examples by hand to confirm your understanding.
Common Mistakes to Avoid:
- Jumping straight to code: Don't start coding before you fully understand the problem.
- Ignoring constraints: Overlooking constraints can lead to inefficient solutions or incorrect assumptions.
- Forgetting edge cases: Assuming your solution works for "normal" inputs and missing special conditions.
- Misinterpreting the problem: Reading too quickly and misunderstanding a key word or phrase.
5. Now Try It
Analyze the following problem in detail: "Given a string s, return the character that appears most frequently in the string. If there are ties, return the character that appears first alphabetically."
What to do:
1. Identify the inputs (types, format).
2. Identify the outputs (types, format).
3. List any constraints (e.g., string length, character set).
4. Think of at least three distinct edge cases and their expected outputs.
5. Provide a simple example string and its expected output, explaining your reasoning.
What success looks like: You'll have a clear, structured breakdown of the problem without writing any actual code for the solution yet.
Frequently asked about Detailed Problem Analysis
More from introduction to algorithm
Get the full introduction to algorithm curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free