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

Explain Exception Handling in Java

This is one of the hottest Java Interview Questions for entry level and mid level software developers in Java. The question is mostly asked as "Explain Exception Handling in Java" but the wordings may change. Defining an Exception An exception indicates a problem that a reasonable application may wish to recover from. On a more formal note, "When a program violates the semantic constraints of the Java Programming Language, the JVM signals error to the program as an Exception. Many programming languages abruptly terminates programs in such scenarios but Java has a cleaner way of handling t...
Read More

Introduction to B Trees

This post is just an introduction to B Trees and here we will discuss one of the complicated data structures, B Trees. I would advice the reader to get familiar with the Tree data structures, and Balanced Trees. For e.g. : Binary Search Trees, AVL Trees, Red Black Trees and 2-3-4 Trees. If you are not familiar with any/few of the above mentioned data structures, then please do not worry, I will try to explain everything here from scratch and hopefully you might not need any external support. But in case you are trying to learn the above topics, please look into the Additional Read section a...
Read More

Maximum Element Sliding Window

A very interesting problem indeed! Often asked in Interviews in various forms and we will discuss one of these forms here in this post. Problem Statement - Maximum Element Sliding Window Given a stream or array of elements (preferably integers). Find the elements with maximum value in a window of length K where the window slides by one step to the right every time. What is a Sliding Window? Given an array A, a sliding window of length K is a range of fixed size K in the array such that the range slides to the right by one index. Here is a diagram to explain how sliding window will look...
Read More

What are the advantages of Java Programming Language?

The most basic question asked to a beginner in a Java Interview is "What are the advantages of Java Programming Language?". Here is a detailed answer to this question. Java is a relatively high level language. It provides lot of features out of the box, which makes it easy to learn for today’s developers and focus on the real business implementation rather than worrying about the system level infrastructure management. Few of the advantages of Java Programming Language are: Platform Independence : Java is platform independent, which means that if a program is written and compiled in Ja...
Read More