## Permutation of multiple arrays and iterator – code Permutation of multiple arrays and iterator has two tasks. First is the permutation of multiple arrays and output as one single array. 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 recursion. The …

## Last man standing with Circular Linked List – Code The “Last man standing” is also called “Josephus problem”. Give a number n people standing in a circle, eliminate every kth one until the last one remain. We usually circular linked list to solve Josephus problem. First, we build a Circular Linked List by adding n nodes. Then use a …

## Modulo operation and round robin tournament scheduler – code Modulo is the operation that returns the remainder of one number divided by another. It is a arithmetical operator, represented as % in Java. For example, 6 % 4 is 2. In this post we are not using it as math operator, instead focus on the use of modulo operation …

## HashMap object as key and sort – Code HashMap is easy to use by following three steps, create the HashMap object, call put() and get(). But if you need to use object as key, you need extra work – override hashCode() and equals(). Also you might have to implement comparable if you need to sort them. HashMap object …

## Initialize game board in matrix – Code Initialize game board in matrix is to populate one digit number into a NxN 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. What …

## Build hierarchy from Stack-like log When the input data is a flat list, you may notice sometimes they contains the data consists of “start” and “end” pairs. Every “start” has a corresponding “stop” message, which is in a stack-like fashion. When analyzing this kind of data, it will be helpful to build hierarchy from stack-like …

## Clean directories with recursion – Code Clean directories with recursion is to uses Java File class and methods to remove files in a file system. What is recursion? Recursion is a technique that a function or an algorithm calls itself. It is powerful tool to solve math, and data structures problems. It solves sub and repetitive …

## Sort squares with optimization – Code Sort squares is to sort the square of elements in a sorted array. Sounds easy, right? Since it uses sorting algorithms, the time complexity is O(n^2). Now you are asked to optimize it. The complexity should be O(n). How to optimize? We can use the “merge two sorted arrays” approach. …

## Selection sort (2 solutions) – Code Selection Sort is one of three well-known simple sorting algorithms (ie bubble sort, insertion sort and selection sort). It is intuitive. Starting from the first element of the array, find the smallest one in each iteration, and put it at the front. As other two simple sorting algorithms, we can …

## Merge two sorted arrays (2 solutions) – Code Merge two sorted arrays is to merge two sorted array in one sorted array. The technique uses while loop to compare the elements in two arrays and put the smaller one in the result array. How can we improve it? We can improve by merge in space. As initial state, …