| name | leetcode |
| description | Structured problem-solving for competitive programming and technical interview prep. Use for LeetCode problems, coding challenges, or interview practice — provides optimised solutions with full complexity analysis. |
Purpose
Structured problem-solving for competitive programming and technical interview prep. Unlike /homework, this skill provides complete, optimized solutions with full complexity analysis — these are practice problems, not graded work.
Activation
/leetcode [problem name, number, or paste the problem]
/leetcode --mode=guided # Walk through solution step by step
/leetcode --mode=solution # Jump to optimized solution
/leetcode --mode=review # Review student's attempted solution
/leetcode --lang=python|typescript|java|cpp|go
Behavior
Step 1 — Problem Intake
Accept the problem as:
- A LeetCode problem number (e.g., "LC 42")
- A problem name (e.g., "Trapping Rain Water")
- A pasted problem statement
- A category request (e.g., "give me a medium DP problem")
Step 2 — Problem Analysis
Before any solution, always analyze:
Category: Dynamic Programming / Graph / Two Pointers / etc.
Difficulty: Easy / Medium / Hard
Key insight: [The non-obvious observation that unlocks the solution]
Patterns: [Known algorithmic patterns this resembles]
Step 3 — Approach Ladder
Present solutions from naive to optimal, so the student understands the progression:
Brute Force
- Describe the naive approach
- State its complexity
- Explain why it's suboptimal
Optimized Approach
- State the key insight that enables the improvement
- Describe the algorithm
- State improved complexity
Optimal Solution
- Full clean implementation
- Inline comments explaining every non-obvious step
- Complexity proof
Step 4 — Full Solution
Provide a clean, production-quality solution:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []
Step 5 — Complexity Analysis
Always provide explicit complexity:
Time Complexity: O(n)
- Single pass through nums array
- Hash map lookup is O(1) average
Space Complexity: O(n)
- Hash map stores at most n elements
- In worst case (no pair until last element), all nums stored
Can we do better?
- Time: No — we must examine each element at least once → Ω(n) lower bound
- Space: Yes — O(1) space with two-pointer (but requires sorted array: O(n log n) time)
Step 6 — Test Cases
Always provide a test suite:
assert solution.twoSum([2,7,11,15], 9) == [0,1]
assert solution.twoSum([3,2,4], 6) == [1,2]
assert solution.twoSum([3,3], 6) == [0,1]
assert solution.twoSum([-1,-2,-3,-4,-5], -8) == [2,4]
Step 7 — Related Problems
Suggest 2-3 related problems to reinforce the pattern:
If you understood this, try:
- LC 167: Two Sum II — Input Array Is Sorted (two pointer variation)
- LC 15: 3Sum (extend to triplets)
- LC 18: 4Sum (generalization)
Problem Categories Reference
| Pattern | Key Data Structures | Classic Problems |
|---|
| Two Pointers | Array | Two Sum II, Container With Most Water |
| Sliding Window | Array, String | Longest Substring, Min Window Substring |
| Binary Search | Sorted Array | Search in Rotated Array, Find Peak |
| BFS/DFS | Graph, Tree | Number of Islands, Word Ladder |
| Dynamic Programming | Array, Matrix | Climbing Stairs, Coin Change, LCS |
| Backtracking | Tree (implicit) | Permutations, Subsets, N-Queens |
| Heap/Priority Queue | Heap | Top K Elements, Merge K Lists |
| Stack/Monotonic Stack | Stack | Valid Parentheses, Largest Rectangle |
| Trie | Trie | Word Search, Autocomplete |
| Union Find | Array | Number of Provinces, Redundant Connection |
Interview Mode
When the student adds --interview:
- Simulate a real interview setting
- Present the problem and wait for the student's approach
- Give hints only when explicitly asked
- Time the session
- Debrief afterward with what an interviewer would note