Close

## Alternative arrangement of numbers in the array

Given an array of 2n numbers of the following format {a1, a2, a3,…an, b1, b2, b3, …bn} We have to write a program to transform this into the following format. {a1, b1, a2, b2, a3, b3, …, an, bn} This can be done using the following algorithm. We iterate over the array for (n-1) times. In…

## Finding the nth node from the end in a linked list

Given a linked list, how do efficiently find the nth node from the end? For example  in the following linked list, 3rd node from the end is ‘6’. 2 -> 1 -> 6 -> 5 -> 9 One obvious method is to find the length of the linked list (len) by going through the list.…

## Finding a number in a sorted array with the least difference from given number

Given a sorted array and a value, we have to find the closest value (with least difference). For example let the input array be {-2, 4, 7, 11, 17} and the input number is 10, the output should be 11. This problem is actually a variation of binary search where in we have to find…

## Moving zeroes to the end of array

Given an array of elements which contains some zeros in between, we have to write a program to move all zeros to the end without disturbing the original order of the elements.For example if the input is {0, 6, 2, 0, 0, 3, 1, 0, 4, 5, 0}, the output should be {6, 2, 3,…

## Printing last K lines from a file

Given a large file containing many lines of text, How do we write an efficient program to print last k lines from that file. One obvious approach is to read the entire file first to find the number of lines (n). In the second iteration skip first (n-k) lines, and print the remaining k lines. …

## Finding the majority element of an array

Given an array of  N elements, the majority element is defined as the one which occurs more than N/2 times. One simple approach is to find the mode (which appears for most number of times) and check if its frequency is greater than N/2. Three strategies for finding the mode are discussed in my previous…

## Reversing the words in the string

Given a string, how do we reverse the words inside the string? For example if the input string is “this is a test” the output should be “test a is this”. The algorithm is simple. This can be done in two steps. The first step is to reverse the entire string. In the second step…

## Inserting an element into a sorted linked list

Given a sorted linked list, how do we insert data into the list in proper order? This is nothing but a step in insertion sort. Simply iterate through the elements to find the position for given number and insert at that position. Here is the Java code to do that. I have used the LinkedList…

## Removing a given character from the string

Given a string and a character, Write a function to remove all instances of the character and return the modified string. One easy method is to iterate through each character of the string, Once a restricted character is seen, we move all the characters appearing after it by one position. This approach involves a lot…

## How does a counting sort work?

Counting sort as its name indicates sort a list of integers by counting the occurrence each number appearing in the list. This is possible when the range of integers (particularly small) is given. Unlike comparison based sorting algorithms which run in O( n log n) time in the worst case, Counting sort runs O(n) time.…