Close

## Sorting the elements by frequency

Given an array, how do we sort the elements by descending order of their frequency? Also if two elements appear for same number of times, one which appeared first in the input array should be placed first. For example, if A = [7, 6, 1, 7, 1, 3, 6, 6, 2] The output should be…

## Comparator and Comparable interfaces in Java

In Java there are two ways to impose ordering on objects. One is through Comparator interface, and the other is through Comparable interface. Let us see the similarities and differences between them and when should we use them. If we are defining a class, and we want a natural ordering of a collection of the…

## Merging two sorted arrays

Given two sorted arrays A, B of lengths m, n respectively. How to merge them into a single sorted array?. Let us assume that A has sufficient space (m+n) for result array. For example let A = [1, 3, 5, 7, 9], and B = [2, 4, 6, 8, 10]. The function should return A…

## Minimum difference between two sorted arrays

Given two arrays sorted in ascending order, find the absolute minimum difference between any pair of elements |a-b| such that a is from one array and b is from another array. For example consider the following two arrays. array1 = [3, 27, 45, 68, 70, 81, 99] array2 = [9, 16, 25, 35, 70, 84]…

## Finding the kth smallest element in the array

Given an array of numbers, how do we efficiently find the Kth smallest number in it? This problem is also called the selection problem. Widely used in order statistic (Find the smallest, biggest, median, top K elements etc…) For example in the array [6, 1, 9, 8, 3, 5, 4] the third smallest element is…

## Finding two numbers occuring once

Given an array containing all elements twice except two numbers. Write a program to find those two numbers. For example let us consider the following array {2, 3, 2, 1, 4, 1, 4, 5} The answer is {3,5} because they appear only once and all remaining elements appear twice. This question is somewhat similar to earlier…

## Printing the matrix in a spiral order

Given a two dimensional array or a matrix, how do we write a program to print the elements in a spiral order. For example the spiral order for the following matrix is 1  2  3  4  8  12  16  15  14  13  9  5  6  7  11  10 1   2   3   4…

## Reversing a doubly linked list

Given a doubly linked list, how do we reverse it by just re-arranging the pointers? Here is an example of input/output. The following picture depicts how we re-arrange the pointers. Here is the Java implementation.

## Checking if any anagram of a given string is palindrome or not

Given a string, how do we check if any anagram of it can be a palindrome? For example let us consider the string “AAC”. An anagram of it is “ACA” which is a palindrome. We have to write a method which takes a string and outputs true if we can form a palindrome from any…

## Merging k sorted lists

Given K sorted lists, how to merge them into a single sorted list? Let total number of elements in K sorted lists is N. We have to form a single sorted list of length N. For example let us consider the following input lists{4, 19, 27}{1, 6, 50, 73}{9, 45, 59, 67} The output should…