Monthly Archives: March 2012

Array stable partition

Given an array of positive and negative integers, re-arrange it so that you have positives on one end and negatives on the other. BUT retain the original order of appearance. do it in-placeĀ  e.g. 1, 7, -5, 9, -12, 15 … Continue reading

Posted in Uncategorized | Tagged | 1 Comment

Sort an integer array in a special way

Problem:Given an integer array, sort the integer array such that the concatenated integer of the result array is max. Example: [4, 94, 9, 14, 1] will be sorted to [9,94,4,14,1] where the result integer is 9944141 Solution: This is an … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

Maximum non-conflict jobs

Problem: You have a list of jobs that need to be scheduled. For each job, you know when it should start and when it should end. You need to come up with an algorithm to schedule the maximum number of … Continue reading

Posted in Uncategorized | Leave a comment

Intervals merge problem

Insert Interval: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

The smallest triangle problem

Problem: Given 3 unsorted integer array, find out the minimum triangle between the three arrays. That is, find out arr1[i], arr2[j] and arr3[k] such that dist = |arr1[i]-arr2[j]| + |arr2[j]-arr3[k]| + |arr1[i]-arr3[k]| is a minimum. Solution: Let us use the … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

The strange integer

Problem: Given is an unsorted integer array where all but one integer appear 3 times, find out the integer that only appears once. Example: {4,5,6,4,5,6,4,5,6,7} =>7 Some extension questions: an integer array have elements that all but one appear 5 … Continue reading

Posted in Uncategorized | Leave a comment

The robber’s problem

Question: There is a row of houses in which each house contains some amount of money. Write an algorithm that loots the maximum amount of money.The only restriction is that you cannot loot two houses that are directly next to … Continue reading

Posted in Uncategorized | 2 Comments

Maximun distance in array

Problem: Given an array A[], find (i, j) such that A[i] <= A[j] and (j – i) is maximum. Example: { 3, 5, 4, 8, 5, 4, 3, 1, 5, 4, 2, 1, 1, 0 }, return distance = 9 … Continue reading

Posted in Uncategorized | Leave a comment

String permutation and combination

The permutation and the combination of the string. Given (1,2,3) Its combinations are: (1), (1,2), (1,2,3), (1,3), (2), (2,3), (3) Its permutations are: (1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1) It has no tricks, the only challenge is to write neat codes. I provide some sample … Continue reading

Posted in Uncategorized | Tagged , | 1 Comment

Print the products of 2 and 5 in increasing order

Problem: Print the numbers of the form 2^i*5^j in increasing order. Example: 1, 2, 4, 5, 8, 10, 16, 20,….. How to do it efficiently? The most intuitive way is to create a very long bitmap, and then maintain two … Continue reading

Posted in Uncategorized | Tagged , | 2 Comments