[output] | def powers_of_two(start, end): powers = [] current_power = 1 while current_power < end: if current_power >= start: powers.append(current_power) current_power *= 2 return powers [instruction] | Write code to find the powers of two in a given range. [input] | Range: 0 to 20
[output] | class TreeNode: def __init__(self, key): self.left = None self.right = None self.key = key def quick_sort_tree(arr): if not arr: return None pivot = arr[0] pivot_node = TreeNode(pivot) left_subtree_elements = [x for x in arr[1:] if x < p [instruction] | Build a quickly-sortable tree from a given array. [input] | Array = [3, 8, 4, 5, 2, 10, 7]
[output] | class GroceryInventory: def __init__(self): self.inventory = {} def add_item(self, item, quantity): if item not in self.inventory: self.inventory[item] = quantity else: self.inventory[item] += quantity def remove_item(self, it [instruction] | Design a class for a grocery store inventory management system. [input] |
[output] | def get_count(arr, element): left_index = bisect.bisect_left(arr, element) right_index = bisect.bisect_right(arr, element) return right_index - left_index [instruction] | Write a function that returns the number of times an element appears in a sorted array. [input] | Array = [3, 8, 10, 10, 10, 13, 15] Element = 10
[output] | def find_edit_distance(str1, str2): len_str1 = len(str1) len_str2 = len(str2) matrix = [[0] * (len_str2+1) for _ in range(len_str1+1)] for i in range(len_str1+1): matrix[i][0] = i for j in range(len_str2+1): matrix[0][j] = j for i in range(1, len_str1+1): [instruction] | Given two strings, find the minimum number of operations required to convert one string to the other. [input] | String 1 = "horse" String 2 = "ros"
[output] | def rotate_in_place(arr, rotation): rotation %= len(arr) start = 0 count = 0 while count < len(arr): current_index = start current_value = arr[start] next_index = (current_index + rotation) % len(arr) while next_index != start: next_va [instruction] | Implement a function to perform in-place array rotation. [input] | arr = [1, 2, 3, 4, 5] rotation = 2
[output] | def longest_increasing_sequence(arr): lengths = [1]*len(arr) for i in range(1, len(arr)): for j in range(i): if arr[i] > arr[j]: lengths[i] = max(lengths[i], lengths[j] + 1) max_length_index = 0 for i in range(len(lengths)): if le [instruction] | Given an array of numbers, find the longest increasing sequence in the array. [input] | Array = [2, 3, 5, 4, 7, 9, 6, 8]
[output] | SELECT * from users WHERE id = 2; [instruction] | Create a query in SQL to find the record with id 2 from the table "users". [input] | Table Name: users
[output] | def oneAway(str1, str2): len1 = len(str1) len2 = len(str2) if abs(len1 - len2) > 1: return False i = 0 j = 0 count = 0 while i < len1 and j < len2: if str1[i] != str2[j]: if count == 1: return False count = [instruction] | Write a function that takes two strings and determines if the first string is one character away from the second string. [input] | String 1 = "cat", String 2 = "cot"
[output] | while counter < 10: counter += 1 [instruction] | Create a while loop in Python to increment a counter by 1 until it reaches 10. [input] | counter = 0
[output] | string s = "Hello!&@ This#%^$ is a string"; for (int i = 0; i < s.length(); i++) { if (!isalnum(s[i])) s.erase(s.begin() + i); } [instruction] | Manipulate a string in C++ to delete all special characters from it. [input] | String = “Hello!&@ This#%^$ is a string”
[output] | int age = 18; [instruction] | Assign the variable "age" with the value 18. [input] |
[output] | public void traverseTree(Node root) { S tack<Node> stack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { Node node = stack.pop(); // Process the node if (node.right != null) stack.push(node.right); if (node.left != null) stack.push(node.left); } } [instruction] | Write a function to traverse a tree using depth-first search algorithm in Java. [input] |
[output] | let regex = /\w{6,}/g; let words = sentence.match(regex); [instruction] | Create a Regex in JavaScript to extract all words of length 6 or more from a sentence. [input] | Sentence = "This is a sample sentence with some words of length 6 or more"
[output] | Hash Table [instruction] | Identify the data structure most suitable for storing 100 unique integers. [input] |
[output] | int sum = 0; for (int i = 0; i < arr.length; i++) sum += arr[i]; [instruction] | Write a loop in Java to add all elements of a given array. [input] | int[] arr = {1, 2, 3, 4, 5};
[output] | Linear search is a technique for searching for an element in a list by repeatedly checking every element until a match is found or the end of the list is reached. It is typically implemented as a loop, where each element is compared to the search key until a match is found, or until all the elements [instruction] | Explain the process of linear search in a few sentences. [input] |
[output] | int n = numbers.length; if (n % 2 == 0) { // if n is even int m = n/2; double median = (numbers[m-1] + numbers[m]) / 2.0 } else { // if n is odd int m = (n+1) / 2; double median = numbers[m-1]; } [instruction] | Suppose a set of n numbers and write a program to calculate the median [input] | int[] numbers = {3,7,5,14,15,17};
[output] | A Turing machine can be tested by providing sample inputs that cover various cases and examining the output. An ideal test would consist of input strings that cover all possible scenarios, such as inputs with no computation, inputs with long computations, inputs that flip a single bit, etc. Addition [instruction] | How would you test a Turing machine? [input] |
[output] | public class Fibonacci{ public static void main(String args[]) { int n1=0,n2=1,n3,i,count=10; System.out.print(n1+" "+n2);//printing 0 and 1 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; [instruction] | Write a java program to generate a Fibonacci series up to N numbers [input] | int N = 10;
[output] | A cycle can be detected in a linked list using the Floyd's Algorithm. This algorithm works by using two pointers; a slow pointer and a fast pointer. The slow pointer moves one node of the list at a time, while the fast pointer moves two nodes at a time. If the two pointers point to the same node, th [instruction] | Construct an algorithm to detect a cycle in a linked list [input] |
[output] | def product(arr): product = 1 for x in arr: product *= x return product # Driver Code array = [1,2,3,4,5] print(product(array)) [instruction] | Create a function that takes an array of integers and prints out their product. [input] | array = [1,2,3,4,5]
[output] | def largest_int(arr): largest_int = arr[0] for i in range(1, len(arr)): if arr[i] > largest_int: largest_int = arr[i] return largest_int # Driver Code array = [6, 8, 1, 3, 4] print(largest_int(array)) [instruction] | Write a function to determine the largest integer in an unordered array. [input] | array = [6, 8, 1, 3, 4]
[output] | import string import random def generate_password(length): # Generate a random string of characters letters = string.ascii_letters + string.digits password = ''.join(random.choice(letters) for i in range(length)) return password # Driver Code length = 8 print(generate_pa [instruction] | Write a code to generate a random password using random library of Python. [input] | length = 8
[output] | def frequency_chart(input_string): # Dictionary to store character frequency freq = {} # Loop over the input string for char in input_string: if char in freq: freq[char] += 1 else: freq[char] = 1 return freq # Driver Code [instruction] | Build a program to print the frequency of characters in an input string. [input] | input_string = "abcabcabcabc"
[output] | def string_two_characters(input_string): # Get the length of the string n = len(input_string) # Return the string which is made of the first and last two characters return input_string[0:2] + input_string[n-2:n] # Driver Code input_string = "spring" print(string_two_chara [instruction] | Write a function to get a string made of the first and last two characters of a given string. [input] | input_string = "spring"
[output] | The maximum number of 256-bit numbers that fit in a 1GB RAM is 134217728. [instruction] | Return the maximum number of 256-bit numbers that fit in a 1GB RAM. [input] |
[output] | The maximum number of nodes in a full binary tree with a given height of 4 is 2^4 - 1, which is 15. [instruction] | What is the maximum number of nodes in a full binary tree with a given height? [input] | Height: 4
[output] | def string_to_int(s): res = 0 for char in s: res = res * 10 + int(char) return res [instruction] | Convert a string to its corresponding integer value. [input] | String: "123"
[output] | def remove_duplicates(arr): res = [] seen = set() for element in arr: if element not in seen: res.append(element) seen.add(element) return res [instruction] | Remove all duplicates from an array. [input] | [1, 1, 2, 3, 4, 5, 3, 4]