Create a programming task related to common algorithms and data…
- User
Assistant: User: Create a programming task related to common algorithms and data structures. The task should follow this format:
{ "problem_description": "You are given an array of integers. Write a function to find the maximum sum of a subarray within the array.", "input": "An array of integers.", "output": "The maximum sum of a subarray within the input array.", "examples": [ { "input": "[-2, 1, -3, 4, -1, 2, 1, -5, 4]", "output": "6", "explanation": "The subarray [4, -1, 2, 1] has the maximum sum of 6." }, { "input": "[1, 2, 3, 4, -10, 5, 6]", "output": "15", "explanation": "The subarray [1, 2, 3, 4, -10, 5, 6] has the maximum sum of 15." }, { "input": "[5, 4, -1, 7, 8]", "output": "23", "explanation": "The subarray [5, 4, -1, 7, 8] has the maximum sum of 23." }, { "input": "[-1, -2, -3, -4]", "output": "-1", "explanation": "The subarray [-1] has the maximum sum of -1." } ], "function_signature": "def max_subarray_sum(arr):", "solution": { "code": " max_sum = float('-inf') current_sum = 0 for num in arr: current_sum += num if current_sum > max_sum: max_sum = current_sum if current_sum < 0: current_sum = 0 return max_sum" } }
Assistant:
- Assistant
Here is a programming task related to common algorithms and data structures:
{ "problem_description": "You are given a binary tree. Write a function to compute the maximum depth of the tree. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.", "input": "A binary tree represented by a TreeNode class with 'val', 'left', and 'right' attributes.", "output": "An integer representing the maximum depth of the tree.", "examples": [ { "input": "root = TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))", "output": "3", "explanation": "The longest path is from root node 3 to leaf node 7, passing through nodes 20 and 7." }, { "input": "root = TreeNode(1, None, TreeNode(2, TreeNode(3), None))", "output": "3", "explanation": "The tree has the longest path from root node 1 to leaf node 3, passing through node 2." }, { "input": "root = None", "output": "0", "explanation": "An empty tree has a depth of 0." }, { "input": "root = TreeNode(0)", "output": "1", "explanation": "A tree with only the root node has a depth of 1." } ], "function_signature": "def max_depth(root):", "solution": { "code": " if root is None:\n return 0\n\n left_depth = max_depth(root.left)\n right_depth = max_depth(root.right)\n\n return 1 + max(left_depth, right_depth)" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.