Permutation of multiple arrays and iterator – code

permutation of multiple arrays java

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 …

Continue reading

Last man standing with Circular Linked List – Code

last man standing circular linked list java

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 …

Continue reading

Modulo operation and round robin tournament scheduler – code

modulo operation

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 …

Continue reading

Initialize game board in matrix – Code

initialize game board in matrix java

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 …

Continue reading

Build hierarchy from Stack-like log

process log with stack java

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 …

Continue reading

Clean directories with recursion – Code

Clean Directories with recursion java

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 …

Continue reading

Sort squares with optimization – Code

Sort squares java

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

Continue reading

Selection sort (2 solutions) – Code

selection sort

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 …

Continue reading