## Sort hashmap 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 …

## Word break using memoization – Code 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 …

## Word ladder using bidirectional BFS – time complexity explained 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, …

## Print stack trace as hierarchy – code In an input string, there are “start” and “end” pairs. Every “start” has a corresponding “stop” message, in a stack-like fashion. We are going to parse it and print a hierarchy view of the stack trace. So that we can visualize the hierarchy structures of the data. To build hierarchy …

## Initialize game board in matrix – Code 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 …

## Clean directories using recursion – code 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. …

## Sort squares of a sorted array in one pass 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 …

## Merge two sorted arrays (2 solutions) – Code 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: …