Depth first search in weighted graph using recursion

DFS in weighted graph

In a social network, an individual person is a node. Between two people there is an undirected edge because they are mutual friends. When one person tells another person a story, they both know the story. Given timestamp as the time two people meet, timestamp can be seen as the …

Continue reading

Autocorrect with trie and edit distance in Java

autocorrect java

Google provides a powerful autocorrect 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, the …

Continue reading

Web scraping in Java – Jsoup and selenium

web scraping feature

Web scraping is a great way to retrieve data and save the information. with a simple Java web scraping setup, you can download content using Jsoup and selenium. Download the source code from the GitHub. Table of Content Web scraping and parsing in HTML – Jsoup Download images – Jsoup …

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 parentheses – code

generate valid parentheses

To find number of possible combinations of valid parentheses, we have to know Catalan number. Catalan numbers are a sequence of natural numbers that follow the formula showing below. The first few Catalan numbers for n = 0, 1, 2, 3, 4, 5 … Cn = 1, 1, 2, 5, …

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

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

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

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

Sort hashmap by key

hashmap object as key

Hashmap is key-value pair data structure. The key can be any data type. The most common used data types are string and integer. Sometime, custom defined object can be a key as well. Then you have to specify how to order them. This post introduces how to sort hashmap by …

Continue reading