Shortest path from source to destination in matrix – Code

Shortest path in matrix java

Shortest path in matrix is to find the shortest distance from the the source to the destination. As you know, graph can be represented as adjacent matrix. Therefore, we can use the Breadth First Search algorithm in graph to solve this problem. BFS starts from the source node. It explores …

Continue reading

Find K closest points to origin (2 solutions) – code

Find K closest points to center java

What is K closest points problem? Find K closest points is to find closest points to the pointer(0,0) (it is called center or origin). The input k is to specify how many points you should return. Here we provide 2 solutions. The first one uses PriorityQueue. ProrityQueue is commonly used …

Continue reading

Find all subset of string in dictionary – code

Find all subset of string in dictionary java

Find all subset of string in dictionary is to find the subset of an input string that exists in dictionary. The dictionary contains one million words. For this question, the trick is to find subset, not substring. What’s the difference between substring and subset? The substring is contiguous sequence of …

Continue reading

Convert hierarchy to nested objects – code

Build status hierarchy object java

There are online tools that convert JSON file to objects. The same idea applies to “convert hierarchy to nested objects”. The syntax of nested object is obj.children[1].children[2]. “obj” is the top level object. Each nested object’s level maps with the level in the hierarchy list. You can access any object …

Continue reading

Bi-directional BFS and examples – Code

BiDirectional BFS

A bi-directional search is to start search from two ends simultaneously. The drive of this is to make the search efficient. BFS is short for Breadth First Search. The search starts from the source node. Then explores all its adjacent nodes before going to the next level adjacent nodes. The …

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

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

What are the missing data structures in java collections and implementations?

Java node data structures diagram

Java collections provides APIs for many data structures such as ArrayList, Linkedlist, HashMap. They are very handy for us to use. But there are missing data structures in Java collections that are widely used, but not provided. The developers have to write their own implementations for these data structures. What …

Continue reading