Contiguous subarray with sum in a range

Given an array int32 arr[] of size n, return the number of non-empty contigious subarrays whose sum lies in range [a, b] Examples: count([1,2,3], 0, 3) = 4 ( [1], [2], [3], [1, 2]) count([-2,5,-1], -2, 2) = 3 ( [-2], [-1], [-2, 5, -1] ) Naive O(n^2) solution – def naive_algorithm(lst, a, b): result […]

Binary Tree All Paths, Max Sum Path, Diameter, Longest Path, Shortest Path, Closest Leaf

Given a Binary Tree T. 1. Find all paths from root to each of the leaves in T. 2. Find the longest path from root to a leaf (also called Max Depth or Height of the tree). 3. Find the path that has largest sum along the path in T. 4. Find distance (shortest) between […]

Binary Search Tree (BST) insert, delete, successor, predecessor, traversal, unique trees

From wiki, A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree additionally satisfies the binary search tree property, which states that the key in each node must be greater than all […]

Assign binary operators to evaluate to target value

Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value. For example: “232”, 8 -> [“2*3+2”, “2+3*2”] “00”, 0 -> [“0+0+0”, “0-0”, “0*0”] “102”, 2 -> [“1*0+2”] “45435”, 9191 -> […]

Min subarray (or sublist) to sort to make an unsorted array (or list) sorted

Give a list of unsorted number, find the min window or min sublist or min subarray of the input, such as if sublist is sorted, the whole list is sorted too. For example, given array a={1,2,3,5,4,4,3,3,7,8,9} then min subarray to sort the complete array sorted is {5,4,3,3}. More example : for a={1,2,3,5,6,4,2,3,3,7,8,9} then min subarray […]