Archive for January 2017
Problem statement Given a sorted integer array without duplicates, return the summary of its ranges for consecutive numbers. For example, given , return . 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 SummaryRanges { private static String summary(int[] a) { String ans = ""; int start = a[0]; ans = ans + start; for (int i = 1; i < a.length; i++) { if (a[i  1] != a[i]  1) { if (a[i  1] != start) { ans = ans + ">" + a[i  1]; } start = a[i]; ans = ans + ", " + start; } } return ans; } public static void main(String[] args) { int[] a = { 0, 1, 2, 4, 5, 7, 9 }; System.out.println(summary(a)); } } 
Problem statement Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of . If the target is not found in the array, return . For example, given and target value 8, return . . . . Read more
Given a string and a string , find the minimum window in which will contain all the characters in in complexity . Sample Input 0

S = "ADOBECODEBANC" T = "ABC" 
Sample Output 0
Solution The first step we are going to take is, count the occurrences of each letter in string . We . . . Read more
Problem statement You have a string, , of lowercase English alphabetic letters. You can perform two types of operations on : Append a lowercase English alphabetic letter to the end of the string. Delete the last character in the string. Performing this operation on an empty string results in an . . . Read more
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two spaceseparated long integers. Input Format A single line of five spaceseparated integers. Constraints Each . . . Read more
There are two kangaroos on an xaxis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location and moves at a rate of meters per jump. The second kangaroo starts at location and moves at a rate of meters per jump. Given the . . . Read more
The Knuth shuffle algorithm is an algorithm for randomly shuffling the elements of an array arr of size given a function that generates a uniformly distributed random number in time. The algorithm produces an unbiased permutation. That is, every permutation is equally likely.

for i in range(n1,0,1): j = integer chosen uniformly randomly from [0,i] swap arr[i] with arr[j] 
Proof that the algorithm is unbiased . . . Read more
Problem statement We call an quadruple of positive integers, , beautiful if the following condition is true: Note: is the bitwise XOR operator. Given and , count the number of beautiful quadruples of the form where the following constraints hold: When you count the number of beautiful quadruples, you should . . . Read more
Problem statement Given a set of hotels and its guests reviews, sort the hotels based on a list of words specified by a user. The criteria to sort the hotels should be how many times the words specified by the user is mentioned in the hotel reviews. Input The first . . . Read more
Problem statement Given a list of numbers, e.g.:

25626 25757 24367 24267 16 100 2 7277 
Output a delta encoding for the sequence. In a delta encoding, the first element is reproduced as is. Each subsequent element is represented as the numeric difference from the element before it. E.g. for the sequence above, the delta encoding would . . . Read more