Author: dharam

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

Finding K nearest distances

Problem Statement You are given millions of two dimensional point and a utility method to calculate their distances from the origin. Write a code to return the nearest K unique distances from the origin. If there are more than one point at the same distance, the distance must be just returned once. For e.g. If there are five points (1,1), (2,1), (1,2), (2,2) and (3,3) and the value of K is 3, then we need to return the following: 1.414 - distance of (1,1) from origin. 2.236 - distance of (2,1) or (1,2) from origin as both will be same. 2.828 - distance of (2,2) from origin Test ...
Read More

Find maximum element in array of alternate sequences

Problem Statement Given an array of elements which is first increasing and then decreasing, find maximum element in array of alternate sequences. Here is how an alternate sequence may look like : { 1, 3, 4, 7, 9, 6, 4, 2, 1 } Test Cases Here are few test scenarios which we must take into account before jumping into the code. input array is null input is empty array input has 1 element input has 2 element - increasing sequence. input has 2 element - decreasing sequence. input has N elements with proper increasing-decreasing sequence input has increasing sequence input...
Read More

Explain the Significance of Synchronized Keyword

Here is another favorite interview question for Java Interviews - Explain the significance of synchronized keyword. The wordings of the question may vary but the essence remain same. Before understanding synchronized keyword we must understand few things about Java's multi threading. Defining a Thread A thread is an Operating System concept. A process under execution is called a thread or we can also say that a thread is a light weight process. Now the Operating System may manage the processor in such a way that it appears that a processor can in parallel accomplish multiple tasks. Hen...
Read More