Java custom Comparators and examples

Java User defined comparators

Java custom Comparators are important to sort the user defined classes. Here we give examples on how to implement user defined Comparators and use them. They are in String, PriorityQueue and TreeMap. 1. Java custom Comparators for String When sorting strings, it is sorted by Alphabetical order by default. However …

Continue reading

BST tree structure conversion – Code

Convert Binary search tree structure java

Binary search tree (BST) is binary tree, in which the left subtree contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose keys are greater than or equal to the node’s key value. Both subtrees are also binary search trees. We can …

Continue reading

Find X sum to target – Code

Find x sum to target

In array, there are frequently asked questions “Two sum”, “three sum”, “find subarry with given sum”. Here is a twist question, find a number X Not in the array, but can make subarray sum equals to or close to the target. To solve “Find X sum to target”, we will …

Continue reading

Permutation of multiple arrays and iterator – code

perm k arrays

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 in Java

last man standing 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. This problem can be solved with linked list and recursion Here we use circular linked list to solve Josephus problem. First, we build …

Continue reading

Modulo operation and circular array

modulo operatoin

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

HashMap object as key and sort – Code

hashmap key

HashMap is key-value data structure. From the key you can find the value by O(1) complexity. it is the most used data structure in Java collection. HashMap is easy to use by following three steps, create the HashMap object, call put() and get(). But if you need to use object …

Continue reading

Initialize game board in matrix – Code

initialize game board

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

Find 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

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