What is Functional Interface?
A functional interface is any interface which contains only one abstract method. A functional interface can have one or more default and static methods.
Also, there is an annotation in Java @FunctionalInterface which is used to annotate the functional interfaces. It is not a mandate for functional interfaces to carry this annotation.
There are many predefined functional interfaces in Java which belong to the package java.util.function
What is the significance of these interfaces?
These interfaces as we know declare a method which can be implemented and us...

Read More
## Understanding Binary Search Algorithm

Introduction
The problem of searching is very trivial. It goes like this:
There is an array of elements A[1..N] and we need to find the index of a given element in the array. In case the element is not present in the array, we can choose to return a specific value (say -1) which suggests that the element doesn't exist in the array.
Binary Search is a very efficient and interesting searching algorithm. Regular searching algorithms take running time proportional to the size of the input. They are called linear time algorithm O(N) to be asymptotically true.
Binary Search on the other ha...

Read More
## Introduction to Lambda Expressions – Java8

What are Lambda Expressions?
Lambda expressions are another way of writing inline unnamed code block which performs a simple function. Not a direct analogy but if you recall anonymous classes in Java they are a way of writing inline classes. On similar lines Lambda expressions are a way to write method body inline.
They can be considered as anonymous methods, there is no need to formally define these methods but the code in the lambda expression is the body of the method.
Lambda Expression Example
If I want to write a print method which prints the content of a List of strings which is pa...

Read More
## Finding Missing Number in an Increasing Sequence

Problem Statement
You are given an array which contains an increasing sequence of numbers from 1 to N. There is one number missing in the sequence. The task at hand is find the missing number in minimum running time.
Input: An array A of numbers in increasing order, with one number missing.
Output: The missing integer
Constraint: The sequence is strictly increasing. The algorithm should finish in minimum time.
Approach for Finding Missing Number
At first, it looks like a simple problem which can be solved in O(N) time. Indeed it is. Here are the steps:
Start traversing from the...

Read More
## Data Structure for Efficient Search

Problem Statement
Given one million 10 digit unique integers, design an efficient data structure which has lowest time complexity for searches. You are given ample amount of space and the time for building the data structure and populating it is not judged.
The only criteria is to offer efficient searches.
Thought Process for Efficient Search
The numbers are 10 digit unique integers and we need a data structure which can offer search in near constant time. The problem is similar to finding if a word exists in a dictionary. Its just that the Strings are replaced by the 10 digit numbers.
...

Read More
## Explaining the ThreadLocal

Introduction to ThreadLocal
Java supports multi threading and provides many high level APIs to avoid running into the issues associated with multi threading. One of the concepts we discussed previously is called synchronization. You can read about synchronization in this blog post.
Here are few scenarios I would like to draw your attention:
Thread Safety - classic case for using ThreadLocal
Not all the classes in the Java library are thread safe, also the user defined classes are mostly not thread safe. At many places on the web you will find posts related ThreadLocal with one example o...

Read More
## Splitting and Merging B Tree Nodes

This post is to be read in conjunction with another post Introduction to B Trees. Here we learn that in certain operations the B Tree properties might get disturbed and it will need a fix. Splitting and Merging B Tree Nodes are the only operations which can re-establish the properties of the B Tree.
How does a B Tree get unbalanced?
The tree is not just to read and search data. There are operations which update the tree either by deleting a key, inserting a new key or just updating the values stored against the key.
The Insert and Delete operations tend to modify the structure of the tree...

Read More