Archive for November 2016
Problem statement You are given sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick. Suppose we have six sticks of the following lengths:
Then, in . . . Read more
Problem statement A peak element is an element that is greater than its neighbors. Given an input array where , find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may . . . Read more
Problem statement Swap two numbers and without using a third variable. Solution

public class SwapNumbers { public static void main(String[] args) { int a = 6; int b = 2; a = a + b; b = a  b; a = a  b; } } 
Problem statement Given an array A with integers. Rearrange array such that A[0] <= A[1] >= A[2] <= A[3] >= A[4] <= A[5]. Do that in time. Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

public class RearrangeArray { private static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } public static void main(String[] args) { int[] a = { 30, 2, 10, 0, 1, 1, 2, 3, 0 }; int n = a.length; for (int i = 0; i < n  1; i++) { if (i % 2 == 0) { if (a[i] > a[i + 1]) { swap(a, i, i + 1); } } else { if (a[i] < a[i + 1]) { swap(a, i, i + 1); } } } } } 
Problem statement Given an array of times in HH:MM format, find the minimum difference in minutes. The time is circular in nature. For example 23:52 and 00:08 have a difference of 16. Also 00:00 should be handled the same as 24:00. Solution The first key is to convert the string . . . Read more
Problem statement Consider the SimpleUTF8 format that consists of characters of the following length: – 1 byte which have the format 0XXXXXXX – 2 bytes which have the format 110XXXXX 10XXXXXX – 3 bytes which have the format 1110XXXX 10XXXXXX 10XXXXXX You will receive a byte[] str. Your task is . . . Read more
Problem statement In a party of people, only one person is known to everyone (celebrity). That celebrity doesn’t know anyone in the party. Find the celebrity if such a person exist. Input Format The first line of the input will contain ( the number of people) and . Each of . . . Read more
Problem statement How would enumerate all the prime numbers up to a given range? Solution There way we are going to solve it is using a wellknown approach called Sieve of Eratosthenes. The approach is as follows: Assume . 1. First generate a list of integers from 2 to 30. . . . Read more
Problem statement Given two unsorted arrays, one with event start times and one with end times, find out if any two events overlap. Solution The key to the solution is to sort the appointments according to their start time in decreasing order, while keeping the correlation between the start and . . . Read more
Problem statement Write a function that takes the ordinal number of a column in a spreadsheet and returns the label of that column.

1 > A 2 > B 26 > Z 27 > AA ... 703 > AAA ... 
Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

public class Ordinal2SpreadsheetNumber { public static String convert(int n) { if (n>=1 && n<=26) { return ""+(char)(n + 'A'  1); } else { return convert(n/26) + (char)(n % 26 + 'A'  1); } } public static void main(String[] args) { System.out.println(convert(1)); System.out.println(convert(2)); System.out.println(convert(3)); System.out.println(convert(26)); System.out.println(convert(27)); System.out.println(convert(28)); System.out.println(convert(703)); } } 