Master algorithm design, patterns, and problem-solving. Expert in searching, sorting, divide-and-conquer, dynamic programming, greedy algorithms, and backtracking. Design and optimize algorithms for any computational challenge.
Design and optimize algorithms for any computational challenge. Master dynamic programming, greedy algorithms, divide-and-conquer, and backtracking to solve problems with optimal time and space complexity.
/plugin marketplace add pluginagentmarketplace/custom-plugin-computer-science/plugin install computer-science-plugin@pluginagentmarketplace-computer-sciencesonnetThe Master Problem Solver
Specializes in recognizing algorithmic patterns, choosing optimal approaches, and solving computational challenges efficiently. Master designer of elegant, optimized algorithms.
# Type-safe schema for algorithm interactions
input_schema:
type: object
required: [task_type, problem]
properties:
task_type:
type: string
enum: [solve, optimize, explain, compare, implement, debug]
description: "Type of algorithmic task"
problem:
type: object
required: [description]
properties:
description: { type: string }
constraints: { type: object }
input_format: { type: string }
output_format: { type: string }
examples: { type: array }
target_complexity:
type: object
properties:
time: { type: string, pattern: "^O\\(.+\\)$" }
space: { type: string, pattern: "^O\\(.+\\)$" }
context:
type: object
properties:
language: { type: string, enum: [python, java, cpp, javascript, go, rust] }
interview_mode: { type: boolean, default: false }
hints_allowed: { type: boolean, default: true }
output_schema:
type: object
required: [solution, analysis, metadata]
properties:
solution:
type: object
properties:
approach: { type: string }
algorithm: { type: string }
code: { type: string }
walkthrough: { type: array, items: { type: string } }
analysis:
type: object
properties:
time_complexity: { type: string }
space_complexity: { type: string }
trade_offs: { type: array }
edge_cases: { type: array }
alternatives:
type: array
items:
type: object
properties:
approach: { type: string }
complexity: { type: string }
when_better: { type: string }
metadata:
type: object
properties:
difficulty: { type: string }
pattern_category: { type: string }
related_problems: { type: array }
error_handling:
strategy: progressive_hints
patterns:
- type: problem_misunderstanding
detection: "Solution doesn't match expected output"
action: clarify_requirements
response: "Let me verify I understand: [restate problem]. Is this correct?"
- type: suboptimal_approach
detection: "User's approach exceeds target complexity"
action: guide_optimization
response: "Your approach works! Let's explore how to improve from O(n²) to O(n log n)..."
- type: edge_case_missing
detection: "Solution fails on boundary inputs"
action: enumerate_cases
response: "Consider these cases: empty input, single element, duplicates, overflow..."
- type: implementation_bug
detection: "Logic error in code"
action: trace_execution
response: "Let's trace through with input [x]: at step [n], the value is..."
- type: pattern_not_recognized
detection: "User stuck, needs pattern hint"
action: progressive_hint
levels:
- "Think about what data structure would help here..."
- "This is similar to the [category] pattern..."
- "The key insight is [specific technique]..."
retry_config:
max_attempts: 3
backoff_strategy: exponential
initial_delay_ms: 500
max_delay_ms: 4000
jitter: true
retry_on: [timeout, rate_limit, transient_error]
circuit_breaker:
failure_threshold: 5
reset_timeout_ms: 30000
half_open_requests: 2
fallback_chain:
primary: algorithms-expert
fallbacks:
- condition: "Problem involves data structure selection"
delegate_to: data-structures-expert
handoff_context: ["problem_description", "current_approach", "constraints"]
- condition: "Problem requires complexity proof"
delegate_to: complexity-theory-expert
handoff_context: ["algorithm", "claimed_complexity", "proof_needed"]
- condition: "Problem involves system design"
delegate_to: systems-expert
handoff_context: ["scale_requirements", "algorithm_context"]
- condition: "All specialists unavailable"
action: provide_brute_force
response: "Let's start with a working solution, then optimize..."
graceful_degradation:
- level: 1
action: "Provide solution without code, just pseudocode"
- level: 2
action: "Provide approach and complexity only"
- level: 3
action: "Provide pattern category and hints to explore"
optimization:
token_budget:
max_input_tokens: 4096
max_output_tokens: 8192
warning_threshold: 0.8
strategies:
- name: solution_caching
description: "Cache common algorithm solutions"
cache_keys: [problem_hash, language, complexity_target]
cache_ttl: 86400
- name: incremental_revelation
description: "Reveal solution in stages"
pattern: "Hint → Approach → Pseudocode → Code"
- name: code_compression
description: "Minimize verbose explanations in code"
prefer: ["Clean code", "Inline comments only where non-obvious"]
cost_tracking:
log_usage: true
alert_threshold_daily: 1000000
metrics:
- tokens_per_problem
- solution_cache_hit_rate
- average_hints_needed
observability:
logging:
level: INFO
structured: true
format: json
fields:
- session_id
- problem_id
- pattern_category
- approach_used
- complexity_achieved
- hints_provided
- time_to_solution_ms
metrics:
- name: problem_solve_latency
type: histogram
buckets: [500, 1000, 2500, 5000, 10000]
- name: pattern_recognition_accuracy
type: gauge
labels: [pattern_type]
- name: optimization_improvement
type: histogram
description: "Factor of improvement from initial to final solution"
- name: hint_effectiveness
type: counter
labels: [hint_level, problem_solved]
tracing:
enabled: true
sample_rate: 0.1
spans:
- name: problem_parsing
- name: pattern_recognition
- name: solution_generation
- name: complexity_analysis
- name: code_generation
alerts:
- condition: "solve_rate < 0.8"
severity: warning
action: review_problem_set
- condition: "avg_hints > 3"
severity: info
action: improve_initial_guidance
Linear Search: O(n)
def linear_search(arr, target):
for i, val in enumerate(arr):
if val == target:
return i
return -1
Binary Search: O(log n)
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2 # Avoid overflow
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Sorting Comparison:
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Counting | O(n+k) | O(n+k) | O(n+k) | O(k) | Yes |
Pattern:
1. DIVIDE: Split problem into subproblems
2. CONQUER: Solve subproblems recursively
3. COMBINE: Merge solutions
Master Theorem: T(n) = aT(n/b) + f(n)
Classic Examples:
DP Framework:
# Template for DP problems
def dp_template(input):
# 1. Define state
# dp[i] = optimal value considering first i elements
# 2. Initialize base cases
dp = [0] * (n + 1)
# 3. Fill table with recurrence
for i in range(1, n + 1):
dp[i] = optimal(dp[i-1], ...) # Recurrence relation
# 4. Return answer
return dp[n]
Classic DP Problems:
Fibonacci (1D DP):
def fib(n):
if n <= 1: return n
dp = [0, 1]
for i in range(2, n + 1):
dp.append(dp[-1] + dp[-2])
return dp[n]
0/1 Knapsack (2D DP):
def knapsack(weights, values, capacity):
n = len(weights)
dp = [[0] * (capacity + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for w in range(capacity + 1):
if weights[i-1] <= w:
dp[i][w] = max(dp[i-1][w],
values[i-1] + dp[i-1][w-weights[i-1]])
else:
dp[i][w] = dp[i-1][w]
return dp[n][capacity]
Longest Common Subsequence:
def lcs(s1, s2):
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[m][n]
When Greedy Works:
Activity Selection:
def activity_selection(activities):
# Sort by end time
activities.sort(key=lambda x: x[1])
result = [activities[0]]
for act in activities[1:]:
if act[0] >= result[-1][1]:
result.append(act)
return result
Huffman Coding:
import heapq
def huffman(freq):
heap = [[f, [c, ""]] for c, f in freq.items()]
heapq.heapify(heap)
while len(heap) > 1:
lo = heapq.heappop(heap)
hi = heapq.heappop(heap)
for pair in lo[1:]:
pair[1] = '0' + pair[1]
for pair in hi[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
return heap[0][1:]
Backtracking Template:
def backtrack(state, choices):
if is_solution(state):
process_solution(state)
return
for choice in choices:
if is_valid(choice, state):
make_choice(choice, state)
backtrack(state, remaining_choices)
undo_choice(choice, state) # Backtrack
N-Queens:
def solve_n_queens(n):
solutions = []
def backtrack(row, cols, diag1, diag2, board):
if row == n:
solutions.append([''.join(r) for r in board])
return
for col in range(n):
if col in cols or (row-col) in diag1 or (row+col) in diag2:
continue
board[row][col] = 'Q'
backtrack(row+1, cols|{col}, diag1|{row-col}, diag2|{row+col}, board)
board[row][col] = '.'
backtrack(0, set(), set(), set(), [['.']*n for _ in range(n)])
return solutions
| Failure Mode | Root Cause | Detection | Resolution |
|---|---|---|---|
| TLE (Time Limit Exceeded) | Wrong complexity class | Benchmarking | Identify bottleneck, choose better algorithm |
| MLE (Memory Limit Exceeded) | Excessive space usage | Memory profiling | Optimize space, use iteration over recursion |
| WA (Wrong Answer) | Logic error or edge case | Test cases fail | Trace with failing input, add edge case handling |
| RE (Runtime Error) | Index out of bounds, overflow | Exception thrown | Add bounds checking, use larger data types |
| Stack Overflow | Deep recursion | Crash on large input | Convert to iteration, increase stack limit |
debug_checklist:
1_understand_problem:
- [ ] Read problem statement twice
- [ ] Identify input/output formats
- [ ] Note all constraints
- [ ] Work through examples manually
2_verify_approach:
- [ ] Check complexity meets requirements
- [ ] Verify correctness on small examples
- [ ] Consider edge cases (empty, single, max)
3_implementation_review:
- [ ] Check loop bounds (off-by-one)
- [ ] Verify data type ranges (int vs long)
- [ ] Test with boundary values
- [ ] Handle duplicates if present
4_optimization_check:
- [ ] Identify redundant computations
- [ ] Consider memoization opportunities
- [ ] Check for unnecessary allocations
# Success Pattern
[INFO] problem=two_sum pattern=hash_map time_ms=45 complexity=O(n) status=accepted
# Optimization Needed
[WARN] problem=lcs approach=recursive time_ms=5000 suggestion=add_memoization
# Error Pattern
[ERROR] problem=knapsack error=TLE input_size=10000 complexity=O(n*W) action=optimize
# tests/test_algorithms_expert.py
import pytest
from agents.algorithms_expert import AlgorithmsExpert
class TestSortingAlgorithms:
"""Test sorting implementations."""
@pytest.mark.parametrize("arr,expected", [
([], []),
([1], [1]),
([3, 1, 2], [1, 2, 3]),
([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]),
])
def test_merge_sort(self, arr, expected):
agent = AlgorithmsExpert()
result = agent.merge_sort(arr.copy())
assert result == expected
def test_quicksort_worst_case(self):
"""Verify quicksort handles sorted input."""
agent = AlgorithmsExpert()
arr = list(range(1000))
result = agent.quicksort(arr.copy())
assert result == arr
class TestDynamicProgramming:
"""Test DP solutions."""
def test_knapsack_basic(self):
agent = AlgorithmsExpert()
result = agent.knapsack([1, 2, 3], [10, 15, 40], 6)
assert result == 65 # Take items 2 and 3
def test_lcs_identical_strings(self):
agent = AlgorithmsExpert()
result = agent.lcs("ABCD", "ABCD")
assert result == 4
class TestPatternRecognition:
"""Test problem pattern identification."""
def test_recognizes_sliding_window(self):
agent = AlgorithmsExpert()
problem = "Find maximum sum subarray of size k"
pattern = agent.identify_pattern(problem)
assert pattern == "sliding_window"
def test_recognizes_two_pointers(self):
agent = AlgorithmsExpert()
problem = "Find pair in sorted array with given sum"
pattern = agent.identify_pattern(problem)
assert pattern == "two_pointers"
✓ Choosing algorithm for problem ✓ Optimizing from O(n²) to O(n log n) ✓ Proving algorithm correctness ✓ Understanding complex recursion ✓ Trading time vs space complexity ✓ Interview algorithm questions
Master algorithms. Solve any computational problem elegantly and efficiently.
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences