Flow network and min cut

mini cut feature

A flow network is a directed graph where each edge has a capacity and each edge receives a flow. The amount of flow on an edge cannot exceed the capacity of the edge. A flow network has designated source and sink nodes s and t. A flow must satisfy the …

Continue reading

Permutation of multiple arrays and iterator – code

permutation

Permutation of multiple arrays and iterator has two tasks. First is the permutation of multiple arrays and output as an array of new combinations. The second is to implement the iterator of this new array. Permutation is an important topic in the fields of combinatorics. It is usually implemented using …

Continue reading

Build nested object from hierarchical list- code

build nested object

Similar to convert JSON file to objects, we can covert hierarchical list to a nested object. The input is like a table of content with multiple levels, the output is a nested object obj.children[1].children[2]. “obj” is the top level object. Each nested object’s level maps with the level in the …

Continue reading

Clean directories using recursion – code

delete files older than n days

Delete files older than n days and remove the empty directories after the files are deleted. This can be done using recursion. Recursion is a technique that a function calls itself.  When the base condition is met, the rest of call stacks return from the last call to the first. …

Continue reading

Sort squares of a sorted array in one pass

sort squares

To sort squares of a sorted array, you don’t want to use any sorting algorithms such as bubble sort. Because it takes O(nlogn) ~ O(n^2) time. Here we introduce a better way to sort in one pass. The solution is using two pointers. Initialize two pointers, one points to the …

Continue reading

British royal succession order – code

royal succession order

British royal succession order is also known as the line of succession to the British throne. Under common law, the crown is inherited by a sovereign’s children or by a childless sovereign’s nearest collateral line. You can get the current British monarchy succession order here. When you are asked to …

Continue reading

Word break using memoization – Code

wordbreak with memoization

Word break is to divide a string into sub-strings that defined in dictionary. The problem is usually solved with recursion. The time complexity is exponential. Here we introduce word break using memoization, which helps to improve complexity. Memoization is one of Dynamic programming(DP) techniques. DP is able to solve a …

Continue reading

Word ladder using bidirectional BFS – time complexity explained

word ladder bidirectional bfs

Word ladder is to find the number of steps to convert one string to another by changing one letter at a time. The intermediate words should be valid and can be found in pre-defined dictionary. The intuitive solution is using breadth first search (BFS). It starts from the original word, …

Continue reading

Find a random number NOT in array – code

find random not in array

Find random number not in array is a tricky question. It is pretty straight forward to find a random element in array by getting an random index within the range. If you are asked to find a random element that is NOT in array, your brain might not response quickly …

Continue reading

Combinations of adding operators and parentheses

possible expressions

Given a set of numbers, there are many different ways to add operators and parentheses to form an valid expression. The operations are +, -, *, /. The operator * and / have higher precedence than + and – . In infix expressions, we add parentheses to override the normal …

Continue reading