Module 4: Chain-of-Thought and Reasoning

4. CoT for Specific Tasks

Overview

Every type of task has an optimal reasoning style. A good CoT prompt for arithmetic doesn't work the same way for code debugging. In this capsule you'll find CoT templates optimized for five task types: math/arithmetic, logical reasoning, code analysis, factual verification, and multi-constraint problems.

Estimated time: 75-90 minutes


Why the Domain Matters in CoT

A generic CoT template like "Let's think step by step" switches on the reasoning, but it doesn't tell the model how to reason in that domain. Math problems need operation-by-operation steps. Logic problems need the premises identified formally. Code debugging needs the execution traced.

Designing domain-specific templates gives you:

  • More consistent reasoning, in the format you expect
  • A lower chance of skipping critical steps
  • Output that's easier to parse programmatically
  • Higher accuracy on domain-specific benchmarks

Domain 1: Math / Arithmetic

Base Template

from openai import OpenAI

client = OpenAI()

MATH_COT = """Solve the math problem step by step.
For each step:
- Write down which operation you're performing
- Show the calculation
- Write down the intermediate result

At the end write: "Final answer: [number with units]"

Problem: {problem}
"""

def solve_math(problem: str) -> dict:
    """Solves a math problem with structured CoT."""
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": MATH_COT.format(problem=problem)}],
        temperature=0,
        max_tokens=700
    )
    output = response.choices[0].message.content
    
    import re
    match = re.search(r'Final answer:\s*(.+)', output, re.IGNORECASE)
    final_answer = match.group(1).strip() if match else None
    
    return {
        "reasoning": output,
        "final_answer": final_answer
    }

Advanced Template: Algebra with Variables

ALGEBRA_COT = """Solve the algebra problem following this structure:

1. VARIABLES: Define what each variable represents (e.g. x = Anna's age)
2. EQUATIONS: Write the equations of the system
3. SOLVING: Isolate the unknowns step by step
4. CHECK: Substitute the values back and confirm the equations hold
5. ANSWER: Write the answer in natural language

Problem: {problem}
"""

def solve_algebra(problem: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": ALGEBRA_COT.format(problem=problem)}],
        temperature=0,
        max_tokens=800
    )
    return response.choices[0].message.content


# Sample algebra problems
ALGEBRA_PROBLEMS = [
    "Maria is 3 times John's age. In 10 years, their ages will add up to 60. How old is each of them now?",
    "You mix 2 liters of a 30% solution with X liters of a 60% solution to get a 40% solution. How many liters is X?",
    "Train A leaves Madrid at 120 km/h. Train B leaves 30 min later in the same direction at 150 km/h. When does B catch A?",
]

if __name__ == "__main__":
    for p in ALGEBRA_PROBLEMS:
        print(f"\nProblem: {p}")
        print(solve_algebra(p))
        print("-" * 60)

Template: Probability

PROBABILITY_COT = """Solve the probability problem step by step.

Required structure:
1. SAMPLE SPACE: What is the universe of possibilities?
2. EVENT: Which event are we computing?
3. COUNTING: How many favorable and how many total cases are there?
4. PROBABILITY: P(event) = favorable_cases / total_cases
5. CHECK: Is the probability between 0 and 1? Does it make sense?

Problem: {problem}
"""

if __name__ == "__main__":
    p = "A bag has 5 red balls, 3 blue and 2 green. If you draw 2 balls without replacement, what is the probability that both are red?"
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": PROBABILITY_COT.format(problem=p)}],
        temperature=0, max_tokens=500
    )
    print(response.choices[0].message.content)
    # Expected answer: P = C(5,2)/C(10,2) = 10/45 = 2/9 ≈ 0.222

Domain 2: Logical Reasoning

Template: Argument Validity

LOGIC_VALIDITY_COT = """Analyze the logical validity of the argument using this structure:

1. PREMISES: List every premise, numbered
2. CONCLUSION: Identify the conclusion that's being argued for
3. LOGICAL FORM: Write the argument in symbolic form (∀, ∃, →, ¬, ∧, ∨)
4. ANALYSIS: Does the conclusion follow necessarily from the premises?
   - If it's valid: Which inference rule applies? (modus ponens, modus tollens, syllogism)
   - If it's invalid: What's the fallacy? Is there a counterexample?
5. VERDICT: VALID or INVALID, with a one-line justification

Argument: {argument}
"""


def analyze_validity(argument: str) -> dict:
    """Analyzes whether a logical argument is valid."""
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": LOGIC_VALIDITY_COT.format(argument=argument)}],
        temperature=0,
        max_tokens=600
    )
    output = response.choices[0].message.content
    
    import re
    verdict_match = re.search(r'VERDICT:\s*(VALID|INVALID)', output, re.IGNORECASE)
    
    return {
        "full_analysis": output,
        "is_valid": verdict_match.group(1).upper() == "VALID" if verdict_match else None
    }


# Test arguments with known answers
TEST_ARGUMENTS = [
    # Valid
    ("If it rains, the ground gets wet. It's raining. Therefore the ground gets wet.", True),
    ("All humans are mortal. Socrates is human. Therefore Socrates is mortal.", True),
    ("If there's no network, the app fails. The app doesn't fail. Therefore there's a network.", True),
    
    # Invalid
    ("If it rains, the ground gets wet. The ground is wet. Therefore it's raining.", False),
    ("Some dogs are dangerous. Rex is a dog. Therefore Rex is dangerous.", False),
    ("If I work hard, I'll succeed. I'm not succeeding. Therefore I didn't work hard.", False),
]

if __name__ == "__main__":
    correct_count = 0
    for argument, expected in TEST_ARGUMENTS:
        result = analyze_validity(argument)
        obtained = result["is_valid"]
        is_correct = obtained == expected
        if is_correct:
            correct_count += 1
        status = "✓" if is_correct else "✗"
        print(f"{status} Expected: {expected}, Got: {obtained}")
        print(f"  Argument: {argument[:60]}...")
    
    print(f"\nAccuracy: {correct_count}/{len(TEST_ARGUMENTS)}")

Template: Common-Sense Reasoning

COMMON_SENSE_COT = """Reason about the situation using common-sense knowledge.

Structure:
1. SITUATION: Summarize the situation in 1-2 sentences
2. RELEVANT KNOWLEDGE: What world knowledge applies here?
3. INFERENCES: What can be inferred logically?
4. CONSTRAINTS: Are there implicit assumptions or constraints?
5. ANSWER: The most reasonable conclusion

Question: {question}
"""

COMMON_SENSE_QUESTIONS = [
    "If Sarah is in a dark room and finds a switch, what should she do first?",
    "A restaurant has a 45-minute wait. The diners standing in line — do they probably want to go, or not want to go, to the restaurant?",
    "Your phone is at 3% battery and you need to make an urgent call. What should you do first?",
]

Domain 3: Code Analysis and Debugging

Template: Exhaustive Debugging

CODE_DEBUG_COT = """Analyze and debug the following Python code, following these steps:

1. WHAT THE CODE DOES: Briefly describe the purpose of the code
2. EXECUTION TRACE: Simulate the execution line by line for the given input (or the case that triggers the error)
3. ERROR IDENTIFICATION: Which line fails and why? The exact error type.
4. ROOT CAUSE: Why does this error exist? (unhandled edge case, wrong type, etc.)
5. FIX: The corrected code
6. ADDITIONAL CASES: Are there other edge cases that would also fail?

Code to analyze:
```python
{code}

{additional_context} """

def debug_code_exhaustive(code: str, context: str = "") -> dict: """ Runs an exhaustive code analysis with CoT.

Args:
    code: The Python code to analyze
    context: Additional information (e.g. "The error happens with input=[]")

Returns:
    dict with the full analysis and the corrected code
"""
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user", 
            "content": CODE_DEBUG_COT.format(
                code=code, 
                additional_context=context or ""
            )
        }
    ],
    temperature=0,
    max_tokens=900
)
output = response.choices[0].message.content

import re
# Pull the corrected code out of the code block
code_match = re.search(r'```python\n(.*?)```', output, re.DOTALL)
fixed_code = code_match.group(1).strip() if code_match else None

return {
    "analysis": output,
    "fixed_code": fixed_code
}

Samples to try

BUGGY_CODE_SAMPLES = [ (""" def fibonacci(n): if n == 0: return 0 if n == 1: return 1 return fibonacci(n-1) + fibonacci(n-2)

This code works but has a serious performance problem

print(fibonacci(40)) # Very slow """, "The code is correct but not efficient"),

("""

def find_duplicates(items): duplicates = [] for i in range(len(items)): for j in range(len(items)): if i != j and items[i] == items[j]: if items[i] not in duplicates: duplicates.append(items[i]) return duplicates

print(find_duplicates([1, 2, 3, 2, 1, 4])) """, "Finds duplicates"),

("""

class Stack: def init(self): self.items = []

def push(self, item):
    self.items.append(item)

def pop(self):
    return self.items.pop(0)  # LIFO

def is_empty(self):
    return len(self.items) == 0

s = Stack() s.push(1) s.push(2) s.push(3) print(s.pop()) # Should be 3, not 1 """, "A LIFO stack implementation"), ]

if name == "main": for code, description in BUGGY_CODE_SAMPLES: print(f"\n=== {description} ===") result = debug_code_exhaustive(code, description) print(result["analysis"]) print("\n--- Corrected code ---") if result["fixed_code"]: print(result["fixed_code"])


### Template: Code Review

```python
CODE_REVIEW_COT = """Perform a professional code review against these criteria:

1. FUNCTIONALITY: Does the code do what it's supposed to?
2. EDGE CASES: List the unhandled boundary cases
3. PERFORMANCE: Are there inefficiencies? What is the O() complexity?
4. READABILITY: Is the code clear? Are the names descriptive?
5. SECURITY: Are there potential vulnerabilities?
6. SUGGESTIONS: A prioritized list of improvements (high/medium/low)

Code to review:
```python
{code}

"""


---

## Domain 4: Factual Verification

### Template: Verify Against Context

```python
FACTUAL_VERIFY_COT = """Verify whether the claim is correct based on the context provided.

Structure:
1. CLAIM: State clearly what is being claimed
2. EVIDENCE IN CONTEXT: What exactly does the context say about this?
3. ANALYSIS: Is the claim consistent with the evidence?
   - If there's direct evidence: quote it
   - If the evidence is indirect: explain the inference
   - If there's no evidence: say so
4. VERDICT: CORRECT / INCORRECT / CANNOT BE VERIFIED
5. CONFIDENCE: High / Medium / Low (and why)

Context:
{context}

Claim to verify: {claim}
"""


def verify_claim(context: str, claim: str) -> dict:
    """
    Verifies a claim against a given context.
    
    Useful for: RAG, fact-checking, QA over documents.
    """
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "user",
                "content": FACTUAL_VERIFY_COT.format(
                    context=context,
                    claim=claim
                )
            }
        ],
        temperature=0,
        max_tokens=500
    )
    output = response.choices[0].message.content
    
    import re
    verdict_match = re.search(
        r'VERDICT:\s*(CORRECT|INCORRECT|CANNOT BE VERIFIED)', 
        output, re.IGNORECASE
    )
    confidence_match = re.search(r'CONFIDENCE:\s*(High|Medium|Low)', output, re.IGNORECASE)
    
    return {
        "analysis": output,
        "verdict": verdict_match.group(1) if verdict_match else "UNKNOWN",
        "confidence": confidence_match.group(1) if confidence_match else "Unknown"
    }


# Example use in RAG
COMPANY_CONTEXT = """
TechCorp was founded in 2018 by Ana Garcia and Robert Martinez in Barcelona.
The company has 250 employees and operates in 12 countries across Europe and Latin America.
In 2023, it billed €45 million, a 28% growth over 2022.
Its main product is a SaaS platform for inventory management.
The current CEO is Ana Garcia, who is also a co-founder.
"""

TEST_CLAIMS = [
    ("TechCorp was founded in 2018", True),
    ("TechCorp has more than 300 employees", False),
    ("TechCorp's product is a mobile app", False),
    ("TechCorp operates in Asia", None),  # Cannot be verified
    ("Ana Garcia is a co-founder and the CEO", True),
]

if __name__ == "__main__":
    for claim, expected in TEST_CLAIMS:
        result = verify_claim(COMPANY_CONTEXT, claim)
        print(f"\nClaim: {claim}")
        print(f"Verdict: {result['verdict']} (confidence: {result['confidence']})")

Domain 5: Multi-Constraint Problems

Template: Constraint Satisfaction

MULTI_CONSTRAINT_COT = """Solve the problem while satisfying ALL of the given constraints.

Structure:
1. LIST OF CONSTRAINTS: Enumerate each constraint clearly
2. COMPATIBILITY ANALYSIS: Are there constraints that could conflict with each other?
3. STRATEGY: How should the search for a solution be approached?
4. CANDIDATE SOLUTION: Propose a solution
5. VERIFICATION: For EACH constraint, confirm whether the solution satisfies it with ✓ or ✗
6. FINAL ANSWER: The verified solution, or "No solution exists" if there's a conflict

Problem: {problem}
Constraints:
{constraints}
"""


def solve_multi_constraint(problem: str, constraints: list[str]) -> dict:
    """
    Solves a problem with multiple constraints.
    
    Args:
        problem: Description of the problem
        constraints: List of constraints that must be satisfied
    
    Returns:
        dict with the verified solution and a check on each constraint
    """
    constraints_text = "\n".join(f"- {r}" for r in constraints)
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "user",
                "content": MULTI_CONSTRAINT_COT.format(
                    problem=problem,
                    constraints=constraints_text
                )
            }
        ],
        temperature=0,
        max_tokens=900
    )
    output = response.choices[0].message.content
    
    # Count how many constraints came back checked with ✓
    checkmarks = output.count("✓")
    crossmarks = output.count("✗")
    
    return {
        "full_solution": output,
        "constraints_met": checkmarks,
        "constraints_failed": crossmarks,
        "all_met": crossmarks == 0 and checkmarks == len(constraints)
    }


# Example: Scheduling a meeting
meeting_problem = "Schedule a 2-hour meeting for 4 people"

meeting_constraints = [
    "The meeting must be between 9:00 and 18:00",
    "Anna can't do Mondays",
    "Ben can only do Tuesdays and Thursdays",
    "Carl is blocked 12:00-14:00 every day",
    "Diana has a standing meeting Wednesdays 10:00-12:00",
    "The conference room is only free on Tuesdays and Thursdays",
]

if __name__ == "__main__":
    result = solve_multi_constraint(meeting_problem, meeting_constraints)
    print(result["full_solution"])
    print(f"\nConstraints met: {result['constraints_met']}")
    print(f"Constraints failed: {result['constraints_failed']}")

Template: Constrained Optimization

OPTIMIZATION_COT = """Find the solution that MAXIMIZES/MINIMIZES the given objective while satisfying every constraint.

Structure:
1. OBJECTIVE: What is being optimized? Maximize or minimize?
2. DECISION VARIABLES: Which variables can we control?
3. CONSTRAINTS: List each one formally
4. SEARCH SPACE: How many possible solutions are there?
5. CANDIDATES: Evaluate 2-3 concrete options
6. COMPARISON: A table with the objective value for each candidate
7. OPTIMAL SOLUTION: The one that best meets the objective while respecting the constraints
8. VERIFICATION: Confirm that the optimal solution satisfies every constraint

Optimization problem: {problem}
"""


OPTIMIZATION_PROBLEMS = [
    """
    You have $10,000 to invest. You can choose between:
    - Government bonds: 4% a year, minimal risk
    - Tech stocks: 12% expected, 30% chance of losing everything
    - Real estate: 7% a year, requires a minimum of $8,000
    
    Constraints: At most 50% in any single asset. At least 20% in low-risk assets.
    Objective: Maximize the risk-adjusted expected return.
    """,
    
    """
    A factory can produce tables (profit $50/unit) or chairs (profit $30/unit).
    Constraints:
    - Wood available: 1200 units (a table takes 8u, a chair takes 4u)
    - Labor hours: 480h (a table takes 4h, a chair takes 2h)
    - At least 20 tables, for a pending order
    
    How many tables and chairs should be produced to maximize profit?
    """
]

Comparison Table: Templates by Domain

DomainKey reasoning stepsAnswer formatVerification
ArithmeticOperation → calculation → intermediate resultNumber + unitsBackward check
AlgebraVariables → equations → isolateVariable value(s)Substitute into the equations
LogicPremises → symbolic form → inferenceVALID/INVALIDCounterexample
Code debuggingTrace → error → root causeCorrected codeRun it or trace it
Factual verificationClaim → evidence → analysisCORRECT/INCORRECTConfidence
Multi-constraintList → compatibility → candidateSolution + ✓/✗ checksOn each constraint
OptimizationObjective → space → candidatesOptimal solutionVerify the constraints

Implementation: A Multi-Domain System

from enum import Enum
from dataclasses import dataclass
from openai import OpenAI

client = OpenAI()


class ProblemType(Enum):
    MATH = "math"
    ALGEBRA = "algebra"
    LOGIC = "logic"
    CODE = "code"
    VERIFICATION = "verification"
    MULTI_CONSTRAINT = "multi_constraint"
    OPTIMIZATION = "optimization"
    GENERAL = "general"


@dataclass
class CoTResult:
    problem_type: ProblemType
    problem: str
    reasoning: str
    final_answer: str | None
    tokens_used: int


TEMPLATES: dict[ProblemType, str] = {
    ProblemType.MATH: MATH_COT,
    ProblemType.ALGEBRA: ALGEBRA_COT,
    ProblemType.LOGIC: LOGIC_VALIDITY_COT,
    ProblemType.CODE: CODE_DEBUG_COT,
    ProblemType.VERIFICATION: FACTUAL_VERIFY_COT,
    ProblemType.MULTI_CONSTRAINT: MULTI_CONSTRAINT_COT,
    ProblemType.OPTIMIZATION: OPTIMIZATION_COT,
    ProblemType.GENERAL: "{problem}\n\nLet's think step by step.",
}


def detect_type_automatically(problem: str) -> ProblemType:
    """
    Detects the problem type by using the LLM as the classifier.
    An alternative: keyword-based rules.
    """
    classification_prompt = f"""
    Classify the following problem into one of these categories:
    - math: arithmetic, percentages, fractions
    - algebra: equations, variables, systems of equations
    - logic: arguments, premises, logical validity
    - code: debugging, Python code review
    - verification: verify whether a claim is correct given a context
    - multi_constraint: find a solution that satisfies multiple constraints
    - optimization: maximize/minimize something subject to constraints
    - general: anything else
    
    Reply with the category only, no other text.
    
    Problem: {problem[:300]}
    """
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": classification_prompt}],
        temperature=0,
        max_tokens=20
    )
    
    type_str = response.choices[0].message.content.strip().lower()
    
    try:
        return ProblemType(type_str)
    except ValueError:
        return ProblemType.GENERAL


def solve_domain_specific(
    problem: str,
    problem_type: ProblemType | None = None,
    **kwargs
) -> CoTResult:
    """
    Solves a problem using the template specific to its domain.
    
    Args:
        problem: The problem to solve
        problem_type: Problem type (if None, it's detected automatically)
        **kwargs: Extra parameters for the template (context, constraints, etc.)
    
    Returns:
        CoTResult with the full analysis
    """
    if problem_type is None:
        problem_type = detect_type_automatically(problem)
    
    template = TEMPLATES[problem_type]
    
    # Build the prompt according to the type
    if problem_type == ProblemType.VERIFICATION:
        context = kwargs.get("context", "")
        prompt = template.format(context=context, claim=problem)
    elif problem_type == ProblemType.MULTI_CONSTRAINT:
        constraints = kwargs.get("constraints", [])
        constraints_text = "\n".join(f"- {r}" for r in constraints)
        prompt = template.format(problem=problem, constraints=constraints_text)
    else:
        try:
            prompt = template.format(problem=problem, **kwargs)
        except KeyError:
            prompt = f"{problem}\n\nLet's think step by step."
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0,
        max_tokens=900
    )
    
    output = response.choices[0].message.content
    
    import re
    answer_match = re.search(
        r'(?:(?:Final\s+)?answer|VERDICT|OPTIMAL SOLUTION)[:\s]+(.+?)(?:\n|$)',
        output, re.IGNORECASE
    )
    
    return CoTResult(
        problem_type=problem_type,
        problem=problem,
        reasoning=output,
        final_answer=answer_match.group(1).strip() if answer_match else None,
        tokens_used=response.usage.total_tokens
    )


# System demo
if __name__ == "__main__":
    test_cases = [
        ("What is 17% of $350?", ProblemType.MATH, {}),
        ("All cats are felines. Felix is a cat. What can we conclude?", ProblemType.LOGIC, {}),
        ("def add(a, b):\n    return a - b\nprint(add(3, 4))", ProblemType.CODE, {}),
    ]
    
    for problem, problem_type, kwargs in test_cases:
        result = solve_domain_specific(problem, problem_type, **kwargs)
        print(f"\n=== Type: {result.problem_type.value} ===")
        print(f"Final answer: {result.final_answer}")
        print(f"Tokens: {result.tokens_used}")

Troubleshooting by Domain

Math: Calculation Errors in Intermediate Steps

# ❌ Problem: The model computes 17 × 23 = 390 (a calculation error)
# ✅ Fix: Ask it to verify each step

MATH_COT_WITH_VERIFICATION = """Solve the math problem.

For each operation:
1. Write the operation
2. Compute the result
3. Briefly verify that it's correct

Problem: {problem}
"""

Logic: Confusion with Double Negation

# ❌ Problem: "It's not true that John is NOT guilty" gets interpreted wrong
# ✅ Fix: Ask for an explicit conversion into affirmative form

LOGIC_NEGATION_COT = """Before analyzing the argument, convert every double negation into affirmative form.
"It's not true that not X" → "X"
"It's not false that X" → "X"

Then analyze the validity.

Argument: {argument}
"""

Code: The Model Doesn't Follow the Real Execution

# ❌ Problem: The model "assumes" what the code does without following it line by line
# ✅ Fix: Ask for an explicit trace with values

CODE_TRACE_COT = """Trace the execution line by line, showing the state of ALL the variables.

Trace format:
Line N | variable1=value1, variable2=value2 | Comment

Code:
```python
{code}

Test input: {test_input} """


### Factual Verification: Confusion with Information That Wasn't Provided

```python
# ❌ Problem: The model uses knowledge from training, not the context it was given
# ✅ Fix: An explicit instruction to use only the context

STRICT_FACTUAL_COT = """IMPORTANT: Use only the information in the context provided. 
Do not use your general knowledge. If the information isn't in the context, 
write "CANNOT BE VERIFIED" even if you believe you know the answer.

Context: {context}

Claim: {claim}

Verify step by step.
"""

Exercises

Exercise 1: A template for conditional probability problems

Design a CoT template to solve: "In a class, 60% of the students are women. 70% of the women pass the exam, and 50% of the men pass. What percentage of the total passes?"

See solution
CONDITIONAL_PROB_COT = """Solve the conditional probability problem using the Law of Total Probability.

Structure:
1. DEFINE THE GROUPS: Identify the subgroups or partitions
2. GIVEN PROBABILITIES: List P(group) and P(event|group) for each group
3. LAW OF TOTAL PROBABILITY: P(event) = Σ P(event|group_i) × P(group_i)
4. CALCULATION: Substitute and compute
5. CHECK: Is the probability between 0% and 100%?

Problem: {problem}
"""

problem = """
In a class, 60% of the students are women. 70% of the women 
pass the exam, and 50% of the men pass. What percentage of the total passes?
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": CONDITIONAL_PROB_COT.format(problem=problem)}],
    temperature=0, max_tokens=500
)
print(response.choices[0].message.content)

# Expected answer:
# P(women) = 0.60, P(men) = 0.40
# P(pass|woman) = 0.70
# P(pass|man) = 0.50
# P(pass) = 0.70×0.60 + 0.50×0.40 = 0.42 + 0.20 = 0.62 = 62%

Exercise 2: Design CoT for algorithmic complexity analysis

Create a template that helps the model compute the O() complexity of an algorithm.

See solution
COMPLEXITY_COT = """Analyze the time complexity of the given code.

Structure:
1. IDENTIFY THE LOOPS: List every loop (for, while) and its range
2. NESTED LOOPS: Are there loops inside loops? Multiply the complexities
3. RECURSION: If there's recursion, write the recurrence relation T(n)
4. DOMINANT OPERATIONS: Which operation repeats the most?
5. FINAL COMPLEXITY: O(?) and the justification
6. SPACE COMPLEXITY: O(?) for memory

Code:
```python
{code}

"""

Example usage

example_code = """ def find_pair_sum(arr, target): for i in range(len(arr)): for j in range(i+1, len(arr)): if arr[i] + arr[j] == target: return (arr[i], arr[j]) return None """

response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": COMPLEXITY_COT.format(code=example_code)}], temperature=0, max_tokens=400 ) print(response.choices[0].message.content)

Expected: O(n²) time, O(1) space


</details>

### Exercise 3: CoT for credit risk assessment

Design a CoT template to decide whether to grant a loan to an applicant.

<details>
<summary>See solution</summary>

```python
CREDIT_RISK_COT = """Assess the applicant's credit risk and recommend whether to grant the loan.

Structure:
1. INCOME AND ABILITY TO PAY
   - Monthly income: {income}
   - Proposed installment: {installment}
   - Installment/income ratio: compute it (ideal < 30%)

2. CREDIT HISTORY
   - Score: {score} (>700: good, 600-700: fair, <600: bad)
   - Current debts: {current_debts}

3. COLLATERAL
   - Collateral offered: {collateral}
   - Collateral/loan ratio: compute it

4. RISK FACTORS
   - List the factors that raise the risk
   - List the factors that lower the risk

5. DECISION: APPROVE / REJECT / APPROVE_WITH_CONDITIONS
6. JUSTIFICATION: The main reason for the decision

Applicant data: {applicant_data}
"""

data = {
    "income": "$5,000/month",
    "installment": "$1,200/month",
    "score": 680,
    "current_debts": "$8,000",
    "collateral": "Car valued at $15,000",
    "applicant_data": "Steadily employed for 3 years, requesting $20,000 over 24 months"
}

print(CREDIT_RISK_COT.format(**data))

Exercise 4: Compare factual verification templates

Test FACTUAL_VERIFY_COT and STRICT_FACTUAL_COT with the same context and claim. In which cases do they differ?

See solution
context = """
Product X has the following characteristics:
- Price: $299
- Warranty: 1 year
- Available in: red, blue, green
- Weight: 1.5 kg
"""

test_claims = [
    "Product X costs less than $300",  # Verifiable, correct
    "Product X is available in black",  # Verifiable, incorrect
    "Product X is the best-selling one on the market",  # Cannot be verified from the context
]

for claim in test_claims:
    print(f"\nClaim: {claim}")
    
    # Standard template (may use outside knowledge)
    r1 = verify_claim(context, claim)
    print(f"Standard: {r1['verdict']} ({r1['confidence']})")
    
    # Strict template (context only)
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": STRICT_FACTUAL_COT.format(
            context=context, claim=claim
        )}],
        temperature=0, max_tokens=300
    )
    print(f"Strict: {response.choices[0].message.content[-100:]}")

The key difference: For "the best-selling one on the market", the standard template could give an opinion drawn from the model's general knowledge, while the strict template will force "CANNOT BE VERIFIED".

Exercise 5: A template for dynamic programming problems

Design a CoT template that guides the model through classic dynamic programming problems (e.g. knapsack, Fibonacci, LCS).

See solution
DP_COT = """Solve the problem using dynamic programming.

Structure:
1. SUBPROBLEMS: How does the problem decompose into smaller subproblems?
2. STATE: What defines a state? (the variables that change in the recursion)
3. TRANSITION: How is dp[state] computed from earlier states?
4. BASE CASE: What are the initial values?
5. DP TABLE: Fill in the table for small inputs (show the matrix)
6. RESULT: Which cell holds the final answer?
7. CODE: Implement the solution

Problem: {problem}
"""

knapsack_problem = """
0/1 Knapsack: You have a knapsack with a capacity of 7 kg.
Available items: 
- Item A: weight=2, value=3
- Item B: weight=3, value=4  
- Item C: weight=4, value=5
- Item D: weight=5, value=6

Which items should you take to maximize the total value without exceeding the capacity?
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": DP_COT.format(problem=knapsack_problem)}],
    temperature=0, max_tokens=900
)
print(response.choices[0].message.content)
# Optimal answer: A + B + C = 2+3+4=9kg... no, A+B+D=2+3+5=10... 
# Optimal: A+C = 2+4=6kg, value=8. Or A+B=5kg, value=7. Or B+C=7kg, value=9. → B+C is optimal

Summary

  • Math/Arithmetic: Operation-by-operation steps, explicit intermediate results, backward verification
  • Logic: Formal premises → symbolic form → inference rule → verdict
  • Code: Line-by-line execution trace → error identification → fix → edge cases
  • Factual verification: Explicit claim → evidence in the context → verdict + confidence
  • Multi-constraint: List the constraints → candidate → verify each constraint with ✓/✗
  • Optimization: Objective → variables → candidates → comparison → verified optimal solution

Additional resources

  1. GSM8K Dataset - Grade School Math
  2. MATH Dataset - Difficult Math Problems
  3. LogiQA - Logical Reasoning Dataset
  4. OpenAI Prompt Engineering - Complex Tasks
  5. HumanEval - Code Generation Benchmark
  6. BoolQ - Boolean Questions Dataset