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
{ "problem_description": "You are tasked with implementing a function that determines if a given binary tree is height-balanced. A binary tree is balanced if the depth of the two subtrees of every node never differ by more than 1.", "input": "The root node of a binary tree.", "output": "A boolean value indicating whether the tree is height-balanced.", "examples": [ { "input": "TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))", "output": "True", "explanation": "The tree has a balanced structure since the subtrees of every node have heights differing by no more than 1." }, { "input": "TreeNode(1, TreeNode(2, TreeNode(3, TreeNode(4), None), None), TreeNode(2, None, TreeNode(3, None, TreeNode(4))))", "output": "False", "explanation": "The left subtree of the node with value '2' and the right subtree of the same node have height difference of more than 1, making the tree unbalanced." }, { "input": "TreeNode(1)", "output": "True", "explanation": "A single-node tree is trivially balanced." }, { "input": "None", "output": "True", "explanation": "An empty tree is balanced by definition." } ], "function_signature": "def is_balanced(root):", "solution": { "code": " def height(node):\n if not node:\n return 0\n left_height = height(node.left)\n right_height = height(node.right)\n if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:\n return -1\n return max(left_height, right_height) + 1\n\n return height(root) != -1" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.