Combinations of adding operators and parentheses

combinations of operators and parentheses

DFS (Depth first search) with memoization has been introduced in the post of word break using memoization. It uses one of dynamic programming technique “memoization” to save previous results and used for the following recursive steps. This will avoid overlapping and improve complexity. This interview question is another example to …

Continue reading

Get suggested friends (2 solutions) – code

suggested friends

When you are asked to design the data structures for social network, normally the answer is graph. In the graph, We can use DFS or BFS to find the connections between people. Alternatively, we can also use Union find to group the people by their connections. Here we provide solutions …

Continue reading

Find Least common set with union find – code

find least common set

When we have number of sets, and need to group them into joint and disjoint-sets, union–find is the algorithm to go. This question is one step further, we need to find least common set with union find. What is union find? Union-find is to stores data in non-overlapping sets (ie …

Continue reading

Web scrape and parsing in Java (5 Ways)

web scrape

Internet is the great resource to get information, for study, business or hobby. There are many ways of information retrieval with web scrape and parsing from Internet. All programing languages provide the libraries to do so. In this post, I introduce 5 ways of web scrape and parsing. 1. HttpURLConnection …

Continue reading

Spell autocorrect with edit distance – Code

spell autocorrector

Google provides a powerful spell correct for validating the keywords we type into the input text box. It checks against a dictionary. If it doesn’t find the keyword in the dictionary, it suggests a most likely replacement. To do this it associates with every word in the dictionary a frequency, …

Continue reading

Depth first search and matrix – Code

DFS and adjacent matrix

Depth first search (DFS) is an algorithm to traverse each element in the data structure. It starts from the source node, which can be root of the tree, or the start of the string, or a cell in matrix. We visit its adjacent node (or children) down the path before …

Continue reading

Monarchy succession order – code

royal succession order

Monarchy succession order is also known as The line of succession to the British throne. The succession to the British throne is determined by descent, sex, legitimacy, and religion. Under common law, the Crown is inherited by a sovereign’s children or by a childless sovereign’s nearest collateral line. The basis …

Continue reading

Detect cycle in graph and convert graph to tree – code

detect graph cycle

A graph is a data structure that consists of a set of vertices (aka nodes) connected by edges. A graph is cyclic if the graph comprises a path that starts from a vertex and ends at the same vertex. A tree is a simple graph with no cycle. In theory, …

Continue reading

Build hierarchy tree – Code

Build hierarchy tree java

Build hierarchy tree reads employee data and build a corporation hierarchy tree from the list. HashMap plays important role to store the data when reading the input. The trick of this question is to find the root. You can get the root by finding the employee without the manager. Starting …

Continue reading

Shortest path and 2nd shortest path using Dijkstra – code

shortest path using Dijkstra java

What is Dijkstra’s algorithm? Dijkstra’s algorithm is an algorithm to find the shortest paths between vertices in a graph. It uses greedy technique by picking the un-visited vertex with the lowest distance. When all vertices have been evaluated, the result is the shortest path. Find 2nd shortest path is a …

Continue reading