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

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

Sort hashmap by value

hashmap sort by value

Hashmap is key-value pair data structure. Most time you sort the hashmap by the key. But sometimes you need to sort the hashmap by value, and the value is custom defined objects. The easy way is to use the built-in APIs provided by languages. Java, JavaScript and Python all provides …

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

Merge two sorted arrays (2 solutions) – Code

merge two arrays

To merge two sorted arrays in one sorted array, you can loop through the elements of both arrays and compare them. Put the smaller one in the output array. When one array reaches the end, move the rest of elements of the other array into the output array. Interview Question: …

Continue reading

Initialize game board in matrix – Code

initialize game board

Initialize game board in matrix is to populate one digit number into a MxN matrix. The digit can be generated randomly. However, no 3 adjacent cells shall be the same, horizontal, vertical, diagonal and L shape. If you find invalid digit, replace with a different one in that cell. Matrix …

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