Skip to main content

Posts

Showing posts with the label microsoft interview; programming challenges

Missing integer(s) in the array

You have given two array e.g. A[] and B[]. Difference between two arrays that A[] have two additional numbers than B[]. both the array is randomized and not in order.   We have to find out both the number in array A[] in most effcient way. Dont try to sort and compare as  time complexity is nlogn. Need a faster solution. Assume We have elements in array A are a0, a1, a2, ................an And Array B are b0, b1, b2, b3.....................bn. Now as per above condition all the elements of B  already  present with A. So we can represent like A[] = B[]+ai+aj where ai and aj is the missing number. Step1: XOR all the elements for A[] XOR B[]. So result will ai XOR aj as remaining elements will be removed by XOR.  Step2: Now take any bit set in above result. Assume we have result in variable result. Any bit set in result will be either set in ai or aj. Can not be in both the variable.  Assume set bit is 5th bit. A...

Complete the Tour

Given a circular road and a set of petrol pumps located at distances d1 d2 ... dn away from each other and supplying p1 p2 p3 ... pn amounts of fuel and your vehicle has a mileage of 1km/l. Determine the feasibility of going around the path starting with zero fuel. Find a point from which you can start and complete the cycle given that you have zero initial fuel. This will be O(n) algorithm. Start from a random node say i...check till the last node it reach. Two case: 1: Either it reach to last node. And current node i is result. 2. It stopped at node j. Then start from node j+1. to find where we can reach all the node by above algorithm. No Node from i to j can complete the traversal, because each node will start with 0 petrol. While traversing from i we always reached that node >= 0. So that condition is already checked.

String Cruncher

given a string, remove all repeated consecutive substring with length 1, then delete substring of length 2 and so on... Example : string is “abcabeccced” After removing repeated substring of length 1: “abcababceccced” --> “abcababceced” (2 'c' are removed) After removing repeated substring of length 2: “abcababceced” --> “abcabceced” (substring “ab” is removed) and so on... #include using namespace std; bool check ( char *s, int st1, int st2, int size) { for ( int i = 0; i le size; i++) { if ( s[st1+i] != s[st2+i] ) return false; } return true; } int main() { char s[15] = "abcababceccced"; int length = strlen(s); int maxlen = length/2; int match = 0; for ( int len = 1; len { int j = len; int i = 0 ; while ( j { if (check(s,i,j,len)) { j +=len; match++; } else { if ( match) { int temp = i+len; int k = j; while ( k { s[temp] = s[k]; temp++; k++; } length = leng...