## Spell autocorrect with edit distance – Code

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, …

## Depth first search and adjacent matrix – Code

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 …

## Monarchy succession order – code

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 …

## Remove cycle in directed graph and convert graph to tree – code

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, …

## Build hierarchy tree – Code

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 …

## Shortest path and 2nd shortest path using Dijkstra – code

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 …

## Autocomplete with trie – Code

Autocomplete is a feature that search box returns the suggestions based on what you have typed. Autocomplete with trie provides an implementation of auto-complete by using data structure trie. What is trie? A trie is a tree-like data structure in which every node stores a character. After building the trie, …

## Word break using memoization – Code

What is word break? 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. Memoization is one of Dynamic programming(DP) technique. DP is able to solve a complex …

## Huffman coding and compression – Code

What is Huffman code? Huffman code is a particular type of optimal prefix code. It is commonly used for lossless data compression. The explanation of Huffman coding and compression can be found in wiki. How to implement Huffman coding and compression? We use frequency-sorted binary tree to encode symbols. Here …

## Prefix to postfix (2 solutions) – Code

In mathematics expressions, there are infix, prefix and postfix notations. Infix notation is characterized by the placement of operators between operands. For example, the plus sign in 2 + 2. Prefix notation is a notation in which operators precede their operands, such as + 3 4. Postfix notation is a …