Skip to content

Commit 2e1f9f1

Browse files
committed
feat: update
1 parent b995398 commit 2e1f9f1

File tree

18 files changed

+258
-453
lines changed

18 files changed

+258
-453
lines changed
Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,71 @@
1-
# LeetCode Problem Template Examples
1+
# LeetCode Template Examples
22

3-
These JSON5 files serve as reference templates for creating new LeetCode problems. Each template is designed to help LLMs parse raw problem text from leetcode.com into the correct JSON format.
3+
Reference templates for creating new LeetCode problems. **Copy from these examples** - don't create from scratch.
44

5-
## Template Types
5+
## Usage
66

7-
### 1. `basic.json5` - Array/Number Problems
7+
1. **Choose the right template** based on problem type
8+
2. **Copy the entire structure** to `.templates/leetcode/json/{question_name}.json`
9+
3. **Update all fields** with your problem's data
10+
4. **Generate**: `make q-gen QUESTION=your_question`
811

9-
- **Use for**: Array manipulation, hash table, basic algorithms
10-
- **Examples**: Two Sum, Contains Duplicate, Product of Array Except Self
11-
- **Key features**: Simple parameters, basic return types, no special imports
12+
## Templates
1213

13-
### 2. `tree.json5` - Binary Tree Problems
14+
### `basic.json5`
1415

15-
- **Use for**: Binary tree traversal, tree manipulation, tree construction
16-
- **Examples**: Invert Binary Tree, Maximum Depth, Validate BST
17-
- **Key features**: TreeNode import, array-to-tree conversion, tree-specific logging
16+
- **Use for**: Array, string, number, hash table problems
17+
- **Examples**: Two Sum, Valid Anagram, Contains Duplicate
18+
- **Features**: Simple parameters, direct assertions
1819

19-
### 3. `linked_list.json5` - Linked List Problems
20+
### `tree.json5`
2021

21-
- **Use for**: Singly linked list manipulation, list reversal, merging
22-
- **Examples**: Reverse Linked List, Merge Two Lists, Remove Nth Node
23-
- **Key features**: ListNode import, array-to-list conversion, multiple parameters
22+
- **Use for**: Binary tree problems
23+
- **Examples**: Invert Binary Tree, Maximum Depth, Same Tree
24+
- **Features**: TreeNode import, array-to-tree conversion, tree logging
2425

25-
### 4. `string.json5` - String Problems
26+
### `linked_list.json5`
2627

27-
- **Use for**: String manipulation, validation, parsing
28-
- **Examples**: Valid Parentheses, Longest Substring, Palindrome Check
29-
- **Key features**: String parameters, boolean returns, validation patterns
28+
- **Use for**: Linked list problems
29+
- **Examples**: Reverse Linked List, Merge Two Lists, Cycle Detection
30+
- **Features**: ListNode import, array-to-list conversion, list logging
3031

31-
### 5. `matrix.json5` - 2D Array/Matrix Problems
32+
### `string.json5`
3233

33-
- **Use for**: Matrix operations, 2D array manipulation, grid problems
34-
- **Examples**: Rotate Image, Spiral Matrix, Set Matrix Zeroes
35-
- **Key features**: 2D list types, in-place modifications, deep copy for testing
36-
37-
## Usage Instructions
34+
- **Use for**: String manipulation problems
35+
- **Examples**: Valid Palindrome, Longest Substring, Anagrams
36+
- **Features**: String parameters, boolean/string returns
3837

39-
1. **Choose the appropriate template** based on the problem's primary data structure
40-
2. **Copy the template structure** and fill in the specific problem details
41-
3. **Follow the comments** for guidance on each field
42-
4. **Use modern Python syntax** (e.g., `list[int]` instead of `List[int]`)
43-
5. **Test the generated JSON** with `make q-gen QUESTION=your_problem`
38+
### `matrix.json5`
4439

45-
## Key Conventions
40+
- **Use for**: 2D array/matrix problems
41+
- **Examples**: Rotate Image, Spiral Matrix, Set Matrix Zeroes
42+
- **Features**: Matrix parameters, in-place operation testing
4643

47-
- **Naming**: Use snake_case for `question_name` and `method_name`, PascalCase for `class_name`
48-
- **Types**: Use modern Python type hints (`list[int]`, `TreeNode | None`)
49-
- **Parameters**: Match the exact parameter names from the LeetCode method signature
50-
- **Test Cases**: Use the same data format as the examples (arrays for trees/lists)
51-
- **Imports**: Only include necessary imports (`TreeNode`, `ListNode`, etc.)
44+
## Key Fields
5245

53-
## Common Patterns
46+
### Required Core Fields
5447

55-
### Return Types & Dummy Returns
48+
- `question_name`, `class_name`, `method_name`
49+
- `problem_number`, `problem_title`, `difficulty`, `topics`
50+
- `problem_description`, `examples`, `constraints`
51+
- `parameters`, `return_type`, `dummy_return`
5652

57-
- `bool``"False"`
58-
- `int``"0"`
59-
- `str``"\"\""`
60-
- `list[int]``"[]"`
61-
- `TreeNode | None``"None"`
62-
- `ListNode | None``"None"`
53+
### Test Configuration
6354

64-
### Test Parameter Naming
55+
- `test_cases`: Array of `{args, expected}` objects
56+
- `param_names`: Parameter names for test methods
57+
- `test_setup`: Code to convert test data (e.g., arrays to TreeNode)
58+
- `assertion_code`: How to compare result with expected
6559

66-
- **Basic problems**: `param1, param2, expected`
67-
- **Tree problems**: `root_list, expected_list` (converts arrays to TreeNode)
68-
- **Linked List problems**: `head_list, param2, expected_list` (converts arrays to ListNode)
60+
### Notebook Setup
6961

70-
### Test Setup Patterns
62+
- `test_input_setup`: Code for notebook input cell
63+
- `expected_output_setup`: Code for notebook expected cell
64+
- `imports`: Required imports (TreeNode, ListNode, etc.)
7165

72-
- **Basic**: No setup needed (`""`)
73-
- **Tree**: `"root = TreeNode.from_list(root_list)\\nexpected = TreeNode.from_list(expected_list)"`
74-
- **Linked List**: `"head = ListNode.from_list(head_list)\\nexpected = ListNode.from_list(expected_list)"`
75-
- **Matrix (in-place)**: `"import copy\\noriginal_matrix = copy.deepcopy(matrix)"`
66+
## Rules
7667

77-
These templates ensure consistency and proper integration with the existing test framework and validation system.
68+
1. **Copy structure exactly** - all fields are required
69+
2. **Use modern Python syntax**: `list[int]`, `TreeNode | None`
70+
3. **Match existing patterns** - see current JSON files for reference
71+
4. **Test thoroughly** - run `make lint` and `make q-test` after generation

.templates/leetcode/examples/basic.json5

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,57 @@
22
// Basic problem template for array/string/number problems
33
// Copy this structure when creating new basic problems
44

5-
// REQUIRED: Core identifiers (snake_case for question_name, PascalCase for class_name)
6-
"question_name": "two_sum", // Snake case from problem title
7-
"class_name": "TwoSum", // PascalCase version
8-
"method_name": "two_sum", // Snake case method name
5+
// REQUIRED: Core identifiers
6+
"question_name": "two_sum",
7+
"class_name": "TwoSum",
8+
"method_name": "two_sum",
99

10-
// REQUIRED: Problem metadata (copy directly from LeetCode)
11-
"problem_number": "1", // String number from URL
12-
"problem_title": "Two Sum", // Exact title from LeetCode
13-
"difficulty": "Easy", // Easy|Medium|Hard
14-
"topics": "Array, Hash Table", // Comma-separated from LeetCode tags
10+
// REQUIRED: Problem metadata
11+
"problem_number": "1",
12+
"problem_title": "Two Sum",
13+
"difficulty": "Easy",
14+
"topics": "Array, Hash Table",
1515

16-
// OPTIONAL: Problem categorization tags
17-
"tags": ["grind-75"], // Popular lists: grind-75, blind-75, neetcode-150, top-interview
16+
// OPTIONAL: Problem categorization
17+
"tags": ["grind-75"],
1818

19-
// REQUIRED: Problem description (copy full description from LeetCode)
19+
// REQUIRED: Problem description
2020
"problem_description": "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.",
2121

22-
// REQUIRED: Examples (copy from LeetCode, keep input/output as strings)
22+
// REQUIRED: Examples
2323
"examples": [
2424
{ "input": "nums = [2,7,11,15], target = 9", "output": "[0,1]" },
2525
{ "input": "nums = [3,2,4], target = 6", "output": "[1,2]" },
2626
{ "input": "nums = [3,3], target = 6", "output": "[0,1]" }
2727
],
2828

29-
// REQUIRED: Constraints (copy exactly from LeetCode with \n for line breaks)
29+
// REQUIRED: Constraints
3030
"constraints": "- 2 <= nums.length <= 10^4\n- -10^9 <= nums[i] <= 10^9\n- -10^9 <= target <= 10^9\n- Only one valid answer exists.",
3131

32-
// REQUIRED: Method signature components
33-
"parameters": "nums: list[int], target: int", // Modern Python type hints
34-
"return_type": "list[int]", // Return type with modern syntax
35-
"dummy_return": "[]", // Default return for TODO implementation
32+
// REQUIRED: Method signature
33+
"parameters": "nums: list[int], target: int",
34+
"return_type": "list[int]",
35+
"dummy_return": "[]",
3636

37-
// REQUIRED: Import statements (empty for basic problems, specify for TreeNode/ListNode)
37+
// REQUIRED: Imports (empty for basic problems)
3838
"imports": "",
3939

40-
// REQUIRED: Test configuration
40+
// REQUIRED: Test cases
4141
"test_cases": [
4242
{ "args": [[2, 7, 11, 15], 9], "expected": [0, 1] },
4343
{ "args": [[3, 2, 4], 6], "expected": [1, 2] },
4444
{ "args": [[3, 3], 6], "expected": [0, 1] }
4545
],
4646

47-
// REQUIRED: Test method parameters (use expected, not expected_list for basic problems)
47+
// REQUIRED: Test configuration
4848
"param_names": "nums, target, expected",
4949
"param_names_with_types": "nums: list[int], target: int, expected: list[int]",
50-
51-
// REQUIRED: Test setup and logging
5250
"input_description": "nums={nums}, target={target}",
5351
"input_params": "nums, target",
5452
"expected_param": "expected",
5553
"method_args": "nums, target",
56-
"test_setup": "", // Empty for basic problems
57-
"test_logging": "", // Empty for default logging
54+
"test_setup": "",
55+
"test_logging": "",
5856
"assertion_code": "assert result == expected",
5957

6058
// REQUIRED: Notebook setup

.templates/leetcode/examples/linked_list.json5

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
// Linked List problem template for ListNode problems
3-
// Use this for problems involving singly linked lists
2+
// Linked list problem template
3+
// Use this for problems involving ListNode structures
44

55
// REQUIRED: Core identifiers
6-
"question_name": "reverse_linked_list_ii", // Snake case from problem title
7-
"class_name": "ReverseLinkedListII", // PascalCase version
8-
"method_name": "reverse_between", // Method name from problem (often different from title)
6+
"question_name": "reverse_linked_list_ii",
7+
"class_name": "ReverseLinkedListII",
8+
"method_name": "reverse_between",
99

1010
// REQUIRED: Problem metadata
11-
"problem_number": "92", // String number from LeetCode URL
12-
"problem_title": "Reverse Linked List II", // Exact title from LeetCode
13-
"difficulty": "Medium", // Easy|Medium|Hard
14-
"topics": "Linked List", // From LeetCode tags
11+
"problem_number": "92",
12+
"problem_title": "Reverse Linked List II",
13+
"difficulty": "Medium",
14+
"topics": "Linked List",
1515

1616
// OPTIONAL: Problem categorization
17-
"tags": [], // Add if part of popular lists
17+
"tags": [],
1818

1919
// REQUIRED: Problem description
2020
"problem_description": "Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.",
2121

22-
// REQUIRED: Examples (linked list problems show array representation)
22+
// REQUIRED: Examples
2323
"examples": [
2424
{ "input": "head = [1,2,3,4,5], left = 2, right = 4", "output": "[1,4,3,2,5]" },
2525
{ "input": "head = [5], left = 1, right = 1", "output": "[5]" }
@@ -28,15 +28,15 @@
2828
// REQUIRED: Constraints
2929
"constraints": "- The number of nodes in the list is n.\n- 1 <= n <= 500\n- -500 <= Node.val <= 500\n- 1 <= left <= right <= n",
3030

31-
// REQUIRED: Method signature (ListNode | None for nullable, multiple parameters common)
31+
// REQUIRED: Method signature
3232
"parameters": "head: ListNode | None, left: int, right: int",
3333
"return_type": "ListNode | None",
3434
"dummy_return": "None",
3535

3636
// REQUIRED: ListNode import for linked list problems
3737
"imports": "from leetcode_py.list_node import ListNode",
3838

39-
// REQUIRED: Test cases (use array representation, multiple args common)
39+
// REQUIRED: Test cases
4040
"test_cases": [
4141
{ "args": [[1, 2, 3, 4, 5], 2, 4], "expected": [1, 4, 3, 2, 5] },
4242
{ "args": [[5], 1, 1], "expected": [5] },
@@ -46,14 +46,12 @@
4646
// REQUIRED: Test parameters (use expected_list for linked list problems)
4747
"param_names": "head_list, left, right, expected_list",
4848
"param_names_with_types": "head_list: list[int], left: int, right: int, expected_list: list[int]",
49-
50-
// REQUIRED: Test configuration for linked list problems
5149
"input_description": "head_list={head_list}, left={left}, right={right}",
52-
"input_params": "head, left, right", // Actual parameters passed to method
53-
"expected_param": "expected", // ListNode object for assertion
50+
"input_params": "head, left, right",
51+
"expected_param": "expected",
5452
"method_args": "head, left, right",
5553

56-
// REQUIRED: Linked list specific test setup (converts arrays to ListNode objects)
54+
// REQUIRED: Linked list-specific test setup
5755
"test_setup": "head = ListNode.from_list(head_list)\nexpected = ListNode.from_list(expected_list)",
5856
"test_logging": "logger.success(f\"Got result: {result.to_list() if result else []}\")",
5957
"assertion_code": "assert result == expected",
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
// Matrix/2D Array problem template
3-
// Use this for problems involving 2D arrays or matrices
2+
// Matrix problem template
3+
// Use this for 2D array/matrix problems
44

55
// REQUIRED: Core identifiers
6-
"question_name": "rotate_image", // Snake case from problem title
7-
"class_name": "RotateImage", // PascalCase version
8-
"method_name": "rotate", // Method name from problem
6+
"question_name": "rotate_image",
7+
"class_name": "RotateImage",
8+
"method_name": "rotate",
99

1010
// REQUIRED: Problem metadata
11-
"problem_number": "48", // String number from LeetCode URL
12-
"problem_title": "Rotate Image", // Exact title from LeetCode
13-
"difficulty": "Medium", // Easy|Medium|Hard
14-
"topics": "Array, Math, Matrix", // From LeetCode tags
11+
"problem_number": "48",
12+
"problem_title": "Rotate Image",
13+
"difficulty": "Medium",
14+
"topics": "Array, Math, Matrix",
1515

1616
// OPTIONAL: Problem categorization
17-
"tags": ["grind-75"], // Popular algorithm lists
17+
"tags": ["grind-75"],
1818

1919
// REQUIRED: Problem description
20-
"problem_description": "You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).",
20+
"problem_description": "You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).\n\nYou have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.",
2121

22-
// REQUIRED: Examples (matrix problems show 2D array representation)
22+
// REQUIRED: Examples
2323
"examples": [
2424
{ "input": "matrix = [[1,2,3],[4,5,6],[7,8,9]]", "output": "[[7,4,1],[8,5,2],[9,6,3]]" },
2525
{ "input": "matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]", "output": "[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]" }
@@ -28,36 +28,32 @@
2828
// REQUIRED: Constraints
2929
"constraints": "- n == matrix.length == matrix[i].length\n- 1 <= n <= 20\n- -1000 <= matrix[i][j] <= 1000",
3030

31-
// REQUIRED: Method signature (2D list type hint)
31+
// REQUIRED: Method signature
3232
"parameters": "matrix: list[list[int]]",
33-
"return_type": "None", // Many matrix problems modify in-place
33+
"return_type": "None",
3434
"dummy_return": "None",
3535

36-
// REQUIRED: Import statements (empty for basic matrix problems)
36+
// REQUIRED: Imports (empty for matrix problems)
3737
"imports": "",
3838

39-
// REQUIRED: Test cases (2D arrays as input, None or modified matrix as expected)
39+
// REQUIRED: Test cases (for in-place operations, test the modified matrix)
4040
"test_cases": [
4141
{ "args": [[[1,2,3],[4,5,6],[7,8,9]]], "expected": [[7,4,1],[8,5,2],[9,6,3]] },
4242
{ "args": [[[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]], "expected": [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] }
4343
],
4444

45-
// REQUIRED: Test parameters (matrix for input, expected for result)
45+
// REQUIRED: Test configuration
4646
"param_names": "matrix, expected",
4747
"param_names_with_types": "matrix: list[list[int]], expected: list[list[int]]",
48-
49-
// REQUIRED: Test configuration for matrix problems
5048
"input_description": "matrix={matrix}",
5149
"input_params": "matrix",
5250
"expected_param": "expected",
5351
"method_args": "matrix",
52+
"test_setup": "",
53+
"test_logging": "",
54+
"assertion_code": "assert matrix == expected",
5455

55-
// REQUIRED: Matrix-specific test setup (for in-place modification problems)
56-
"test_setup": "import copy\noriginal_matrix = copy.deepcopy(matrix)",
57-
"test_logging": "logger.success(f\"Got result: {matrix}\")",
58-
"assertion_code": "assert matrix == expected", // Compare modified matrix
59-
60-
// REQUIRED: Notebook setup for matrix problems
56+
// REQUIRED: Notebook setup
6157
"test_input_setup": "# Example test case\nmatrix = [[1,2,3],[4,5,6],[7,8,9]]",
6258
"expected_output_setup": "expected = [[7,4,1],[8,5,2],[9,6,3]]"
6359
}

0 commit comments

Comments
 (0)