tag:blogger.com,1999:blog-82361609943594935262018-03-06T05:11:00.225+05:30Interview questions on Algorithms, Programmingtnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.comBlogger49125tag:blogger.com,1999:blog-8236160994359493526.post-84291928599153559042015-03-01T10:28:00.000+05:302015-03-01T10:28:00.642+05:30Remove one Recursion in Inorder Traversal<pre>function inorder(Node root)<br />{<br /> if(root == NULL)<br /> return;<br /> inorder(root->left);<br /> print root->node;<br /> inorder(root->right);<br />}<br /><br />After removing one recursion<br /><br />function inorder(Node root)<br />{<br /> if(root == NULL)<br /> return;<br /> while(root->right != NULL) {<br /> inorder(root->left);<br /> print root->node;<br /> root = root->right;<br /> }<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-51795324471805308532014-04-28T23:30:00.000+05:302014-04-28T23:30:02.279+05:30Petrol Bunks in Circle<p align="justify">There is a circular road, and there are n petrol bunks at different places on that road. The petrol bunk i has enough fuel by which one can travel f_i distance. The distance from the petrol bunk i to i+1 is d_i. <br /><br />You have a car with empty fuel tank, and you need to find a place from where you can start and travel by that road and reach the same place. <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><b>Solution:</b><br /><br />The distance from first bunk to second is D1, and second to third is D2 and so on.<br />The distance that can be travelled by the fuel in the first bunk is F1, and from the second bunk, it is F2 and so on.<br /><br />If F1 < D1, then we cannot start from the first bunk.<br />If F1 + F2 < D1 + D2, then we cannot start from the first or second bunks.<br />Similarly, if F1 + F2 + F3 < D1 + D2 + D3, then we cannot start from the first, second or third bunks.<br /><br />Wherever we reach the fuel to be less than the distance, we have to skip all those bunks, and have to move to the next bunk and repeat the same thing.<br /><br /><br /><b>Code:</b><br /></p><pre>#include<<stdio.h><br /><br />int findStart(int distance[], int capacity[], int n)<br />{<br /> int c=0, d=0, st=0, i;<br /> for(i=0; i<n; i++) {<br /> c+=capacity[i];<br /> d+=distance[i];<br /> if(c<d) {<br /> st = i+1;<br /> c=0;<br /> d=0;<br /> }<br /> }<br /> if(st != 0) {<br /> for(i=0; i<st; i++) {<br /> c+=capacity[i];<br /> d+=distance[i];<br /> if(c<d) {<br /> return -1;<br /> }<br /> } <br /> }<br /> return st;<br />}<br /><br />main()<br />{<br /> int distance[] = {10,20,30,40,50,60};<br /> int capacity[] = {11,21,10,50,51,71};<br /> int result = findStart(distance, capacity, 6);<br /> printf("%d \n", result);<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-7848431711679703392014-04-27T23:28:00.000+05:302014-04-27T23:28:00.260+05:30Finding Odd Coin<div dir="ltr" style="text-align: left;" trbidi="on"><div align="justify"><b>Simple Problem</b><br /><br />There are 12 coins, and one coin has less weight. We have to find the odd coin by measuring only three times.<br /><br /><b>Complex Problem</b><br /><br />There are 12 coins, and one coin is of different weight (more or less). We have to find the odd coin by measuring only three times.<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><b>Solutions</b><br /><br /><b>Simple Problem</b><br /><br />Put 6 coins on one side and other 6 coins on another side. <br />Take the 6 coins of less weight, and put 3 coins on one side and another 3 coins on another side.<br />Take the 3 coins of less weight. Put one coin on one side and another coin on another side.<br /><br />Whichever is of less weight, that is the odd coin. If both are equal, the third coin is of odd coin.<br /><br /><br /><b>Complex Problem</b><br /><br /><b>Incomplete Solution</b><br /><br />Split the 12 coins into three parts, A, B and C, and name them like A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3 and C4.<br /></div><ul><li>Put 4 coins of A and 4 coins of B.<br /><ul><li>If they are equal, put 2 coins of A on one side, and put C1 and C2 on the other side, and measure the weight.<br /><ul><li>If they are not equal, Put A1 and C1 and measure the weight. <br /><ul><li>If they are not equal, C1 is the odd coin. Otherwise, C2 is the odd coin. </li></ul></li></ul><ul><li>If they are equal, put 1 coin of A and C3 and measure the weight.<br /><ul><li>If they are equal, C4 is the odd coin. Otherwise, C3 is the odd coin.</li></ul></li></ul></li></ul><ul><li>If they are not equal, remember which one is of less weight. Put C1, C2, C3, C4 on one side and A1, A2, B1, B2 on another side.<br /><ul><li>If they are not equal, check whether AB is of more weight or less weight. If it is more weight, then in the previous step, whichever one has more weight that has the odd coin. Same thing applies, if it is of less weight. Let's say, set A has the odd coin. Measure C1 and A1.<br /><ul><li>If they are equal, A2 is the odd coin. Otherwise, A1 is the odd coin.</li></ul></li></ul><ul><li>If they are equal, <span style="color: #800000;"><b>Cannot find the solution</b></span></li></ul></li></ul></li></ul></div><br /><b>Complete Solution</b><br /><br />Split the 12 coins into three parts, A, B and C, and name them like A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3 and C4.<br /><ul><li>Put A1, A2, A3 and A4 on one side and B1, B2, B3 and B4 on the other side.<br /><ul><li>If both are equal, put A1, A2, A3 on one side and C1, C2 and C3 on another side.<br /><ul><li>If both are equal, C4 is the odd coin. By weighing with any other coin, we can find whether it is heavier or lighter.<br /><li>If A is heavier, it means one of C1, C2 and C3 is the lighter odd coin. Weigh C1 and C2. Whichever is of less weight, that is the odd coin. If both are equal, C3 is the lighter odd coin.<br /><li>If C is heavier, it means one of C1, C2 and C3 is the heavier odd coin. Weigh C1 and C2. Whichever is of more weight, that is the odd coin. If both are equal, C3 is the heavier odd coin. <br /></ul><li>If A is heavier, put B1, B2, B3 and A4 on one side and C1, C2, C3 and B4 on another side.<br /><ul><li>If both are equal, One of A1, A2, A3 is heavier. Weigh A1 and A2. Whichever is heavier, that is the odd coin. If both are equal, A3 is the odd coin.<br /><li>If B1, B2, B3 and A4 is still heavier. It means, either A4 is heavier or B4 is lighter. Weigh A4 with any other coin (other than B4). If it is equal, then B4 is the odd coin of less weight. Otherwise, A4 is the odd coin of more weight.<br /><li>If B1, B2, B3 and A4 is lighter, then one of B1, B2 and B3 is the lighter odd coin. Weigh B1 and B2. Whichever is lighter, that is the odd coin. If both are equal, B3 is the lighter odd coin. <br /></ul><li>If B is heavier, do same as above. Put C1, C2, C3 and A4 on one side and A1, A2, A3 and B4 on another side and proceed the same way.<br /></ul></ul>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-53766273334476266352014-04-26T23:27:00.000+05:302014-04-26T23:27:00.721+05:30No Child After a Girl ChildGovernment introduced a rule saying, once a couple gets a girl child, they should not have any more children. What would be the ratio of boys to girls after n years?<br /><br /><br /><br /><br />It would be 1:1 only [If we assume, for any birth the ratio of boy to girl is 1:1]. <br /><br />Let's suppose, if there 1000 couples. If we take 1:1 in the births, then 500 would give birth to boys and 500 would give birth to girls. <br /><br />The couple who gave birth to girls will not conceive anymore. The 500 couple who gave birth to boys would give birth to 250 boys and 250 girls in the next delivery.<br /><br />The couple who gave birth to 250 girls would stop conceiving, and the remaining 250 couple would try again.<br /><br />They would give birth to 125 boys and 125 girls.<br /><br />The same thing is repeated and the ratio would be 1:1 at the end.<br />tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-4302972988043224542014-04-25T23:27:00.000+05:302014-04-25T23:27:00.356+05:30Converting a Tree into an Array<pre>Call traverse(root, array, 0);<br /><br />int traverse(Node root, int[] a, int index)<br />{<br /> if(node == NULL)<br /> return index;<br /> int x = traverse(root->left, a, index);<br /> a[x] = root->data;<br /> x = traverse(root->right, a, x+1);<br />}<br /><br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-2087884735871536602014-04-24T23:27:00.000+05:302014-04-24T23:27:00.641+05:30Copy a Linked List in C<pre>Node Copy(Node first)<br />{<br /> if(first == NULL)<br /> return NULL;<br /> Node copy = (Node)malloc(sizeof(Node));<br /> Node temp = copy;<br /><br /> while(first->next != NULL) {<br /> temp->data = first->data;<br /> temp->next = (Node)malloc(sizeof(Node));<br /> temp = temp->next;<br /> first = first->next;<br /> }<br /> temp->data = first->data;<br /> return copy; <br />}<br /><br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-22824438081022510712014-04-23T23:26:00.000+05:302014-04-23T23:26:00.068+05:30No.of Nodes in a Tree<pre>int Count(Node root)<br />{<br /> if(root == NULL)<br /> return 0;<br /> return 1 + Count(root->left) + Count(root->right);<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-63787685655035240332014-04-22T23:26:00.000+05:302014-04-22T23:26:00.098+05:30Delete all the nodes in a Tree in C<p align="justify"><br /><pre>void Clear(Node root)<br />{<br /> if(root == NULL)<br /> return;<br /> Clear(root->left);<br /> Clear(root->right);<br /> free(root);<br />}<br /></pre></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-12127637884297847222014-04-21T23:25:00.000+05:302014-04-21T23:25:00.879+05:30Find Colored CouplesIn an array of colors, we need to find out the no.of colored couples of the same color that are next to each other. If you find a colored couple, then you can remove that couple, and check whether any more couples can be formed by that or not. <br /><br /><b>Example</b><br /><br />R G B B G R Y W Y<br /><br />In the above, there are two B's next to each other. That is one couple, and we can remove that. After removing both the B's, the colors would be like <br /><br />R G G R Y W Y<br /><br />Now, there is another pair of G's in this. After removing that, the colors would be like below.<br /><br />R R Y W Y<br /><br />after removing R, it would be like below.<br /><br />Y W Y.<br /><br />At this time, there are no colored couples that are next to each other. In this case, there are three couples in the given array.<br /><br /><br /><b>Answer:</b><br /><br /><b>Stack:</b><br /><br />Stack is the simplest approach to solve this problem. If we can modify the input array, then we can use the input array itself as stack to solve this problem. <br /><br />We take the first element, and push it to the stack. <br /><br />If the stack contains any elements, then we take the top element and take the next element in the input array and compare the elements. If both are same, then we found one couple. We pop the element, and we move forward.<br /><br />If the elements are not same, then we just push that element to the stack and proceed.<br /><br />With this, by the time we reach the end, we would find the total no. of paired couples.<br /><br /><br /><b>Doubly Linked List:</b><br /><br />If the input elements is given in the form of Doubly Linked List, and if we can modify the input, then from that also, we can easily find the count. <br /><br />We will have two pointers. Initially, first pointer would point to first element, and second pointer would point to second element. If both are different, then both pointers would move one step forward. If they are same, then the first pointer would move backward, and second pointer would move forward. We also would clear the two colors, and adjust the pointers in the doubly linked list. We repeat the loop.<br /><br /><br /><b>Code for Stack:</b><br /><br /><pre>#include<stdio.h><br />#include<string.h><br />#include<stdlib.h><br />class color<br />{<br /> private:<br /> char *array;<br /> int length;<br /><br /> public:<br /><br /> void init(char *c) <br /> {<br /> array = c;<br /> length = 0;<br /> };<br /> <br /> void push(char x)<br /> {<br /> array[length] = x;<br /> length++;<br /> };<br /><br /> char pop() { return array[--length]; };<br /><br /> char top() { return array[length-1]; };<br /><br /> int exists() { return length; };<br />};<br /><br />main(int argc, char **argv)<br />{<br /> char a[1000];<br /> int n, i, j, c=0;<br /> if(argc != 2) {<br /> printf("Usage: colorpairs colorstring\n");<br /> return -1;<br /> }<br /> strcpy(a, argv[1]);<br /> n = strlen(a);<br /><br /> class color clr;<br /> clr.init(a);<br /> clr.push(a[0]);<br /> for(int k=1; k<n; k++) {<br /> if(clr.exists() && clr.top() == a[k]) {<br /> clr.pop();<br /> c++;<br /> } else {<br /> clr.push(a[k]);<br /> }<br /> }<br /> printf("Count: %d\n", c);<br /> return 0;<br />}<br /></pre><br /><b>Code for Doubly Linked List:</b><br /><br /><pre>findCount(Node *head)<br />{<br /> if(head == NULL || head->next == NULL)<br /> return 0;<br /><br /> Node *first = head;<br /> Node *second = head->next;<br /> Node *free1, *free2;<br /> int count = 0;<br /><br /> while(second != NULL) {<br /> if(first->value == second->value) {<br /> count++;<br /> free1 = first;<br /> free2 = second;<br /><br /> if(second->next == NULL) <br /> break;<br /><br /> if(first->prev == NULL) {<br /> first = second->next;<br /> second = second->next->next;<br /> } else {<br /> first = first->prev;<br /> second = second->next;<br /> }<br /><br /> first->next = second;<br /> second->prev = first; <br /><br /> free(free1);<br /> free(free2);<br /> } else {<br /> first = first->next;<br /> second = second->next;<br /> }<br /> }<br /> return count;<br />}<br /><br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-76960798313275429542014-04-20T23:25:00.000+05:302014-04-20T23:25:00.711+05:30Check Whether the tree is Balanced Tree or Not<p align="justify">A tree is a balanced tree, if the heights of the left and right sub trees differ by at max 1 at all the nodes.<br /><br /><b>The code which checks only at the root level</b><br /><pre>bool IsTreeBalanced(Node root)<br />{<br /> if(root == NULL)<br /> return true;<br /> int lh = GetDepth(root -> left);<br /> int rh = GetDepth(root -> right);<br /> if(lh - rh > 1 || rh - lh > 1)<br /> return false;<br /> return true;<br />}<br /><br />int GetDepth(Node root) <br />{<br /> if(root == NULL)<br /> return 0;<br /> int lh = GetDepth(root->left);<br /> int rh = GetDepth(root->right);<br /> return (lh > rh ? lh + 1 : rh + 1);<br />}<br /></pre><br /><br /><b>The code which checks at all the levels, but, not optimized</b><br /><pre>bool IsTreeBalanced(Node root)<br />{<br /> if(root == NULL)<br /> return true;<br /> int lh = GetDepth(root -> left);<br /> int rh = GetDepth(root -> right);<br /> if(lh - rh > 1 || rh - lh > 1 || ! IsTreeBalanced(root->left) || ! IsTreeBalanced(root->right))<br /> return false;<br /> return true;<br />}<br /><br />int GetDepth(Node root) <br />{<br /> if(root == NULL)<br /> return 0;<br /> int lh = GetDepth(root->left);<br /> int rh = GetDepth(root->right);<br /> return (lh > rh ? lh + 1 : rh + 1);<br />}<br /></pre><br /><b>The code which checks at all the levels, and optimized little</b><br /><br />This code does not give the accurate height. If at few levels itself, if it finds that, it is not balanced, it would just return. <br /><br />In C#, we can use out parameters. In C, C++, we can use pointers, and in java, we need to have some kind of holder class. <br /><br /><pre>bool IsTreeBalanced(Node root, out int height)<br />{<br /> if(root == NULL) {<br /> height = 0;<br /> return true;<br /> }<br /> int lh, rh;<br /> if(! IsTreeBalanced(root -> left, out lh)) {<br /> height = lh + 1;<br /> return false;<br /> }<br /> if(! IsTreeBalanced(root -> right, out rh)) {<br /> height = (lh > rh ? lh + 1 : rh + 1);<br /> return false;<br /> }<br /><br /> height = (lh > rh ? lh + 1 : rh + 1);<br /><br /> if(lh - rh > 1 || rh - lh > 1)<br /> return false;<br /> return true;<br />}<br /></pre><br /><b>Very strict Balanced Tree: (In practice, not advisable)</b><br /><br />Where the the path length of the nearest empty child is within one from the path to the farthest empty child.<br /><br /><pre>bool IsTreeBalanced(Node root)<br />{<br /> int max = GetMaxMinDepth(root, min);<br /> if(max - min > 1)<br /> return false;<br /> return true;<br />}<br /><br />int GetMaxMinDepth(Node root, out int minDepth) <br />{<br /> if(root == NULL) {<br /> minDepth = 0;<br /> return 0;<br /> }<br /> int lmin, rmin;<br /> int lmax = GetMaxMinDepth(root->left, out lmin);<br /> int rmax = GetMaxMinDepth(root->right, out rmin);<br /> minDepth = lmin < rmin ? lmin + 1 : rmin + 1;<br /> return (lmax > rmax ? lmax + 1 : rmax + 1);<br />}<br /></pre></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-67053698859672942502014-04-19T23:24:00.000+05:302014-04-19T23:24:00.596+05:30In Binary Tree, Find Sum of Nodes of Alternate Levels<div dir="ltr" style="text-align: left;" trbidi="on">Given a binary tree, find sum of all the nodes of alternate levels. <br /><br />Root is at level 0.<br />All the children of root are at level 1.<br />All the grand children of root are at level 2.<br />and so on.<br /><br />Given a level n, find some of all the nodes that are at alternate levels 0, 2, 4, and so on.<br /><br /><pre>int Sum(Node root, level n)<br />{<br /> if(root==NULL)<br /> return 0;<br /> int d = (n%2 == 0 ? root->data : 0);<br /> return d + Sum(root->left, n+1) + Sum(root->right, n+1);<br />}<br /></pre></div>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-88686914767760421252014-04-17T10:29:00.000+05:302014-04-17T10:29:02.006+05:30In a Binary Tree, find sum of all the nodes below certain levelGiven a binary tree, find sum of all the nodes below certain level. <br /><br />Root is at level 0.<br />All the children of root are at level 1.<br />All the grand children of root are at level 2.<br />and so on.<br /><br />Given a level n, find some of all the nodes that are at level n or more.<br /><br /><pre>int Sum(Node root, level n)<br />{<br /> if(root==NULL)<br /> return 0;<br /> if(n>0)<br /> return Sum(root->left, n-1) + Sum(root->right, n-1);<br /> return root->data + Sum(root->left, n-1) + Sum(root->right, n-1);<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-63343084833297020422014-04-16T10:23:00.000+05:302014-04-16T10:23:00.278+05:30In A Binary Tree, Find sum of all nodes upto certain levelGiven a binary tree, find sum of all the nodes upto certain level. <br /><br />Root is at level 0.<br />All the children of root are at level 1.<br />All the grand children of root are at level 2.<br />and so on.<br /><br />Given a level n, find some of all the nodes that are at level n or less.<br /><br /><pre>int Sum(Node root, level n)<br />{<br /> if(root==NULL)<br /> return 0;<br /> if(n==0)<br /> return root->data;<br /> return root->data + Sum(root->left, n-1) + Sum(root->right, n-1);<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-49333986896651769182014-04-15T18:10:00.005+05:302014-04-15T18:10:00.392+05:30In a Binary Tree, Find Sum of all the nodesGiven a binary tree, find the sum of all the nodes.<br /><br /><pre>int SumOfNodes(Node root)<br />{<br /> if(root==NULL)<br /> return 0;<br /> return root->data + SumOfNodes(root->left) + SumOfNodes(root->right);<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-15165653212364634302014-04-14T17:42:00.000+05:302014-04-14T17:42:00.814+05:30Find whether two strings are anagrams of each other or not<p align="justify">Given a two strings, find whether they are anagrams of each other or not.<br /><br />Anagram means, rearranging of the characters in the string.<br /><br />Ex: <br />mary and army are anagrams.<br />abba and abab are anagrams<br />abba and abbb are not anagrams<br /><br /><br /><br /><b>Answer:</b><br /><br />Assumption: The characters are ASCII characters<br /><br /><pre>AreAnagrams(char a[], char b[])<br />{<br /> int count[128];<br /> int n=strlen(a);<br /><br /> if(n!=strlen(b))<br /> return false;<br /><br /> for(int i=0; i<127; i++)<br /> count[i]=0;<br /> <br /> for(int i=0; i<n; i++)<br /> count[a[i]]++;<br /> for(int i=0; i<n; i++)<br /> count[b[i]]--;<br /><br /> for(int i=0; i<127; i++)<br /> if(count[i]!=0)<br /> return false;<br /><br /> return true;<br />}<br /></pre></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-60045433380317874072014-04-13T16:18:00.001+05:302014-04-13T16:18:00.796+05:30Efficiently Right Shift a String<p align="justify">You are given a string and a number k. You need to right shift the string k times. What is the efficient way to solve this?<br /><br />Example:<br />the string is "Democracy".<br />If k is 2, then the output should be "cyDemocra".<br />If k is 3, then the output should be "acyDemocr"<br /><br /><br /><br /><br /><br /><br /><b>Answer 1:</b><br /><br />O(nk) time complexity, O(1) space complexity.<br /><br />where n is the length of the string.<br /><br />Right shift the string k times.<br /><br /><pre>main()<br />{<br /> char s[] = "Some string ....";<br /> int n = streln(s);<br /><br /> for(int i=0; i<k; i++)<br /> rightShift(s, n);<br />}<br /><br />rightShift(char s[], int length)<br />{<br /> char temp = s[length-1];<br /> for(int i=length-1; i>0; i--)<br /> s[i]=s[i-1];<br /> s[0] = temp;<br />}<br /></pre><br /><b>Answer 2:</b><br /><br />O(n) time complexity, and O(k) space complexity<br /><br /><pre>char s[] = "Some string ....";<br />int n = streln(s);<br />char *exsp = (char*)calloc(k, sizeof(char));<br /><br />for(int i=0; i<k; i++)<br /> exsp[i]=s[n-k+i];<br /><br />for(int i=n-1; i>=k; i--)<br /> s[i]=s[i-k];<br /><br />for(int i=0; i<k; i++)<br /> s[i]=exsp[i];<br /></pre><br /><b>Answer 3:</b><br /><br />O(n) time complexity, O(1) space complexity<br /><br />Reverse the entire array.<br />Reverse the array from 0 to k-1, and from k-1 to n-1.<br /><br /><pre>Reverse(char s[], int start, int end)<br />{<br /> for(int i=start, j=end; i<j; i++, j--)<br /> {<br /> int t=s[i];<br /> s[i]=s[j];<br /> s[j]=t;<br /> }<br />}<br /><br />Reverse(s, 0, n-1);<br />Reverse(s, 0, k-1);<br />Reverse(s, k, n-1);<br /></pre></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com2tag:blogger.com,1999:blog-8236160994359493526.post-82027473372777780272011-09-28T11:13:00.000+05:302011-09-28T11:13:02.734+05:30Constructing Tree From Inorder and PreorderConstructTree(preorder, 0, preOrder.Length-1, inorder, 0, inorder.Length-1); <pre><br />Node ConstructTree(int[] preorder, int ps, int pe, int[] inorder, int is, int ie)<br />{<br /> if(ps < pe || is < ie)<br /> return NULL;<br /> Node root = new Node();<br /> root->data = preorder[ps];<br /> int mid = is;<br /> while(inorder[mid] != preorder[ps]) <br /> mid++;<br /> root->left = ConstructTree(preorder, ps+1, ps+mid-is, inorder, is, mid-1);<br /> root->right = ConstructTree(preorder, ps+mid-is+1, pe, inorder, mid+1, ie);<br /> return root;<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-21928572999669069302011-04-27T21:50:00.000+05:302011-04-27T21:50:13.925+05:30Princess, Flowers and Snake With Incorrect Name Plates<p align="justify">There are three rooms, and there are Princess, Flowers and Snake in those rooms. The doors of all the rooms have incorrect nameplates. i.e., the nameplate for the princess' room is not Princess. Similarly, the nameplate for the Flowers' room is not Flowers. You need to find the room of the Princess without going to the room of Snake. How do you find?<br /><br /><br /><br /><b>Answer:</b><br /><br />Go to the room which has the nameplate Snake. That will not have Snake. If the room has princess, you are done. If the room has flowers, then go to the room which has nameplate flowers. Princess would be there in that room. [Since, Princess cannot be there in the room which has nameplate Princess.]<br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com2tag:blogger.com,1999:blog-8236160994359493526.post-12197170412944061642011-04-27T21:37:00.000+05:302011-04-27T21:37:43.765+05:30One Person Always Lies, Another always Tells Truth<p align="justify">You have to go to a place and there are two paths. You don't know which path leads to your destination. There are two watchmen at the entrance. One person always tells truth, and another one always tells lies. You can ask only one question to one person. What question would you ask to find the way to your destination?<br /><br /><br /><br /><b>Answer:</b><br />If I ask the other person, the way to the destination, what would he tell?<br /><br />Whatever he says, that is not the correct path. Take the other path. <br /><br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-72506660742928145722011-04-27T21:30:00.000+05:302011-04-27T21:30:10.231+05:30Four People crossing the river with different speeds<p align="justify">There is a dark tunnel and four people wants to cross the tunnel. In the tunnel, only two people can travel at a time, and there is only one torch light. Without the torch light, they cannot travel through the tunnel. The four people can travel with different speeds. The first person takes 1 hour to travel through the tunnel. Second person takes 2 hours, third person takes 5 hours, and fourth person takes 10 hours. How can they travel in the least amount of time?<br /><br /><br /><br /><b>Solution:</b><br /><br />1 & 2 cross the tunnel<br /><br />1 returns<br /><br />5 & 10 cross the tunnel<br /><br />2 returns<br /><br />1 & 2 cross the tunnel<br /><br />Total Time: 17 hours<br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-45383973185303875282011-02-13T12:55:00.000+05:302011-02-13T12:55:23.907+05:30Three Cannibals and Three Humans Crossing River<p align="justify">There is a small river. On one side of the river, there are three cannibals, and on another side three humans are there. Boat is with the cannibals. If there are more no.of cannibals than the humans, then the cannibals will eat the humans. If the no.of cannibals is equal or less than the no.of humans, then they cannot do anything. How do the humans move to the other side of the river safely?<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><b>Answer:</b><br /><br />3 Cannibals - 3 Humans<br /><br />One Cannibal crosses the river<br /><br />2 Cannibals - 3 Humans, 1 Cannibal<br /><br />2 Humans cross the river<br /><br />2 Cannibals, 2 Humans - 1 Human, 1 Cannibal<br /><br />1 Human and 1 Cannibal cross the river.<br /><br />1 Cannibal, 1 Human - 2 Humans, 2 Cannibals<br /><br />2 Humans cross the river<br /><br />1 Cannibal, 3 Humans - 2 Cannibals<br /><br />1 Cannibal crosses the river<br /><br />3 Humans - 3 Cannibals.<br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-64148611788202063022011-02-13T12:43:00.000+05:302011-02-13T12:43:35.035+05:30Find the Distance Travelled By the Prince<p align="justify">A Prince is going to a temple in a forest by his horse which travels at a speed of 60 km per hour. When he reached the entrance of the forest, he saw a girl who is going to the temple at a speed of 6 kms per hour. He visits the temple and returns to see the girl immediately. After he sees the girl, he goes again to the temple. He roams between the temple and girl, till the girl reaches the temple after one hour. How much distance the prince travelled by roaming between the girl and the temple?<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><b>Answer:</b><br /><br />If we try to calculate how many no.of times, the prince has visited the temple or girl etc., the problem becomes complicated. The simple to way to solve the problem is, how long the prince has travelled, and from that calculate the distance. The prince has travelled as long as the girl was walking towards the temple. The girl took one hour to reach the temple. For that one hour, the prince was travelling. So, he travelled total 60 kms.<br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-12471209571307179832011-02-13T12:20:00.000+05:302011-02-13T12:20:20.065+05:30Finding the Poison Bottle<p align="justify">A king has arranged a celebration with many visitors to his kingdom. In the celebration, they arranged drinks in 1000 bottles. Just one hour before the celebration, he came to know that, one person has included a poison drink of the same color in a bottle of the same shape. If anyone drinks poison, then nothing happens to that person for one hour. After one hour, suddenly that person would die.<br /><br />The king has few criminals who are going to be hanged the next day. With the help of them, he can find which bottle has poison. How many minimum no.of criminals he needs to find out the poisoned bottle in one hour.<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><b>Answer:</b><br />He needs 10 criminals.<br /><br />Every bottle is given a no from 0 to 1000. Convert each number to binary. The binary number would contain 10 bits. For the first criminal, give little drink from all the bottles for which the first bit is 1. Similarly, for the second criminal, give drink from all the bottle for which the second bit is 1. Do the same thing for all the ten criminals. <br /><br />After one hour, form a binary number with the criminals. If the first criminal dies, then make the first bit 1. If the second criminal does not die, then make the second bit 0. Do the same for all the criminals. Convert the binary number to the corresponding decimal number. That bottle has the poison. <br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com1tag:blogger.com,1999:blog-8236160994359493526.post-11203097995282541982010-09-10T22:34:00.002+05:302010-09-10T22:44:21.963+05:30Sending a parcel by unsecured courier<p align="justify"><br />You have a gift item which you want to send by courier from Hyderabad to Chennai. You have only one box. You have a lock and key, and the receiver also has a lock and key. If you send the gift item without locking in the box, then the courier person will take it. How do you send the gift item to the receiver safely?<br /><br /><br /><b>Answer</b><br /><br />Put the gift item in the box and lock it, and keep the key with you. Send that box to the receiver by courier. The receiver puts his/her lock and keeps the key with him/her. He/She returns the box to you by courier, and you will remove your lock, and send the box again by courier to him/her. He/She removes his/her lock and takes the gift item.<br /></p>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0tag:blogger.com,1999:blog-8236160994359493526.post-80691545039549234072010-08-29T16:57:00.000+05:302010-08-29T16:57:54.001+05:30Print an Integer by using only putchar<pre><br />#include<stdio.h><br />main(int argc, char **argv)<br />{<br /> int i, n, rev, lz = 0;<br /> if(argc > 1) {<br /> n = atoi(argv[1]);<br /> } else {<br /> printf("Enter the number : ");<br /> scanf("%d", &n);<br /> }<br /> rev = 0;<br /> lz = 0;<br /> while(n != 0) {<br /> if(rev == 0 && n%10 == 0) {<br /> lz++;<br /> }<br /> rev = rev*10 + n%10;<br /> n = n/10;<br /> }<br /> while(rev != 0) {<br /> putchar(rev%10 + '0');<br /> rev = rev/10;<br /> }<br /> while(lz--) {<br /> printf("0");<br /> }<br /> printf("\n");<br />}<br /></pre>tnsatishhttp://www.blogger.com/profile/11487701501347042727noreply@blogger.com0