Re: [algogeeks] Re: Google Interview Question: In how many intervals does a number exists
Can you tell the 'size' of your array 'f' if the intervals are [0, 10], [10, 9223372036854775808] ? Programmers should realize their critical importance and responsibility in a world gone digital. They are in many ways similar to the priests and monks of Europe's Dark Ages; they are the only ones with the training and insight to read and interpret the scripture of this age. On Mon, Jun 10, 2013 at 10:12 PM, sunny sharadsin...@gmail.com wrote: yeah interval tree and binary indexed tree is a one solution which will give you answer in log(n) time for each query ,but if i got all the interval at the beigning of time i can get solution in O(1) time by O(n ) preprocessing take array f initialize with zero,now for each interval(l,r) do f[l]++ and f[r+1]-- once you are done wi all queries take prefix sum value of each index will tell you in how many interval it was On Sunday, June 9, 2013 12:50:46 PM UTC+5:30, Monish Gupta wrote: There are n Intervals. Given a set of m numbers, tell in how many intervals does each number exists. Example: 4 Intervals: [2,3] [3,4] [1,4] [3,10]. Numbers {2,4,11} For 2 - 1 as 2 lies only in 1 interval [2,3] For 4 - 3 as 4 lies in 3 intervals For 11 - 0 as 11 lies in none of the given 4 intervals. It can be easily done in O(m*n) by traversing n intervals for each number in the given set of m numbers. How would improve this? Note: I could not fully recall, but I have seen very similar question in codechef but could not find the same. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com.
Re: [algogeeks] Re: Google Interview Question
This is a simple merge, so what is the trick? Did you forget something? On Mon, Aug 1, 2011 at 3:19 PM, Gary Drocella gdroc...@gmail.com wrote: Here is O(n) alg... Does Waste Memory Though :) just don't have an array over 4G, and you should be good. proc Merge_Partition(A) B = {}; index = 0; count0 = 0; count1 = (n/2); while index to A.length B[index++] = A[count0++]; B[index++] = A[count1++]; end while return B end proc On Aug 1, 1:30 pm, Manmeet Singh mans.aus...@gmail.com wrote: Your code does not works proper;y for all cases On Mon, Aug 1, 2011 at 10:42 PM, Rohit jalan jalanha...@gmail.com wrote: Here is the recursive algo: Rearrange(A,p,q) 1. if p is not equal to q do the following 2. r ← (p+q)/2 3. Exchange A[(p+r)/2..r] ←→ A[(p+q)/2 +1 ..(r+q)/2]. 4. Rearrange(A,p,r) 5. Rearrange(A,r+1,q) 6. return On Mon, Aug 1, 2011 at 1:45 PM, Abhishek Gupta gupta.abh...@gmail.comwrote: A is an array of size 2n such that first n elements are integers in any order and last n elements are characters. i.e. A={i1 i2 i3 in c1 c2 c3... cn} then we have to rearrange the elements such that final array is A ={ i1 c1 i2 c2 .. in cn} Example : input : A ={ 5,1,4,d,r,a}; output : A= {5,d,1,r,4,a}; -- Abhishek Gupta MCA NIT Calicut Kerela -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards : ROHIT JALAN B.E. Graduate, Computer Science Department, RVCE, Bangalore -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- --- Douglas Gameiro Diniz Engenheiro de Computação - 2003 - UNICAMP Mobile: (19) 92158777 Gtalk: dgdiniz Msn: thedougdi...@hotmail.com -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@Diniz I guess they asked to do in inplace ( with no extra array ) On Mon, Aug 1, 2011 at 2:41 PM, Douglas Diniz dgdi...@gmail.com wrote: This is a simple merge, so what is the trick? Did you forget something? On Mon, Aug 1, 2011 at 3:19 PM, Gary Drocella gdroc...@gmail.com wrote: Here is O(n) alg... Does Waste Memory Though :) just don't have an array over 4G, and you should be good. proc Merge_Partition(A) B = {}; index = 0; count0 = 0; count1 = (n/2); while index to A.length B[index++] = A[count0++]; B[index++] = A[count1++]; end while return B end proc On Aug 1, 1:30 pm, Manmeet Singh mans.aus...@gmail.com wrote: Your code does not works proper;y for all cases On Mon, Aug 1, 2011 at 10:42 PM, Rohit jalan jalanha...@gmail.com wrote: Here is the recursive algo: Rearrange(A,p,q) 1. if p is not equal to q do the following 2. r ← (p+q)/2 3. Exchange A[(p+r)/2..r] ←→ A[(p+q)/2 +1 ..(r+q)/2]. 4. Rearrange(A,p,r) 5. Rearrange(A,r+1,q) 6. return On Mon, Aug 1, 2011 at 1:45 PM, Abhishek Gupta gupta.abh...@gmail.comwrote: A is an array of size 2n such that first n elements are integers in any order and last n elements are characters. i.e. A={i1 i2 i3 in c1 c2 c3... cn} then we have to rearrange the elements such that final array is A ={ i1 c1 i2 c2 .. in cn} Example : input : A ={ 5,1,4,d,r,a}; output : A= {5,d,1,r,4,a}; -- Abhishek Gupta MCA NIT Calicut Kerela -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards : ROHIT JALAN B.E. Graduate, Computer Science Department, RVCE, Bangalore -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- --- Douglas Gameiro Diniz Engenheiro de Computação - 2003 - UNICAMP Mobile: (19) 92158777 Gtalk: dgdiniz Msn: thedougdi...@hotmail.com -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google interview question
@Dave awesome..! On Sat, Jul 16, 2011 at 7:15 PM, Dave dave_and_da...@juno.com wrote: @Anand: Assuming that the file contains unsigned 32-bit integers. Set an integer array a[65536] to zero, read through the file and tally the numbers based on their low-order 16 bits: a[j0x]++. Since 4.3 billion exceeds 2^32, by the pigeonhole principle, there will be at least one tally, say a[k], that has a value greater than 65536. Set the array to zero again. Read through the file again. Ignore all of the numbers whose low-order 16 bits are not k, and tally numbers based on their upper 16 bits: a[(j16)0x]++. Again by the pigeonhole principle, there will be at least one number that exceeds 1. Now you know the high-order 16 bits and the low-order 16 bits of a number that occurs at least twice. You can quit the second pass as soon as you have your first tally equalling 2. Dave On Jul 15, 8:28 pm, Anand Shastri anand.shastr...@gmail.com wrote: Given a file containing 4,300,000,000 integers, how can you *find **one* that *appears* at *least **twice* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
A B are the concatenation of A and B. Set the following order relation between the numeric strings A = B iff A B = B A Wladimir Araujo Tavares *Federal University of Ceará * On Sat, May 28, 2011 at 1:54 PM, sunny agrawal sunny816.i...@gmail.comwrote: @Logic King No, My algo will take only O(nlgn) where n is no of elements. what i mean by editing the comparision function cmp function of sort of algorithm.h sort(a,a+n, cmp); where cmp is the comparision function defined in my prev. post it will take equal no. of comparision as in sorting. just now overhead of comparision is increased. instead of just or here it is order of no of digits in a no, which is atmost 10 for int or 20 for long ints. so O(20*n*lgn) -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@vishal,Sanjeev.. for the inputs 18,187.. apply ur method.. 18 -- 188 187-- 187 18187 - ur method 18718 - actual @Sunny... i agree that your algorithm takes the *O(N logN)* time.. but again.. the problem is it* doesn't get* the exact solution. Do we really have a polynomial solution for this one? I think this falls under the NP category. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@sravanreddy001 i don't find any cases for which my algo fails and its O(nlgn) i may be missing something can you tell any case where it fails On Sun, May 29, 2011 at 10:15 PM, sravanreddy001 sravanreddy...@gmail.comwrote: @vishal,Sanjeev.. for the inputs 18,187.. apply ur method.. 18 -- 188 187-- 187 18187 - ur method 18718 - actual @Sunny... i agree that your algorithm takes the *O(N logN)* time.. but again.. the problem is it* doesn't get* the exact solution. Do we really have a polynomial solution for this one? I think this falls under the NP category. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
Here is a Java impl... public class LargestPossibleNumber { static class LPNComparator implements ComparatorString { @Override public int compare(String s1, String s2) { int l1 = s1.length(); // new element int l2 = s2.length(); // existing element if (l1 == l2) { for (int i1 = 0, i2 = 0; i1 l1 i2 l2; i1++, i2++) { char c1 = s1.charAt(i1); char c2 = s2.charAt(i2); if (c1 != c2) { return c1 - c2; } } return 0; } else if (l1 l2) { // padding StringBuilder s = new StringBuilder(s1); for (int i = 0; i l2 - l1; i++) { s.append(s1.charAt(l1 - 1)); } s1 = s.toString(); for (int i1 = 0, i2 = 0; i2 l2; i1++, i2++) { char c1 = s1.charAt(i1); char c2 = s2.charAt(i2); if (c1 != c2) { return c1 - c2; } } return 1; } else { // l1 l2 // padding StringBuilder s = new StringBuilder(s2); for (int i = 0; i l1 - l2; i++) { s.append(s2.charAt(l2 - 1)); } s2 = s.toString(); for (int i1 = 0, i2 = 0; i2 l1; i1++, i2++) { char c1 = s1.charAt(i1); char c2 = s2.charAt(i2); if (c1 != c2) { return c1 - c2; } } return -1; } } } public static String getLNP(TreeSetString set) { IteratorString iter = set.iterator(); StringBuilder sBuilder = new StringBuilder(); while (iter.hasNext()) { String element = iter.next(); sBuilder.insert(0, element); } return sBuilder.toString(); } public static void main(String args[]) { TreeSetString set = new TreeSetString(new LPNComparator()); set.add(9); set.add(10); System.out.println(getLNP(set)); //910 set.clear();set.add(2);set.add(3);set.add(5);set.add(78); System.out.println(getLNP(set)); //78532 set.clear();set.add(10);set.add(100); System.out.println(getLNP(set)); //10100 * set.clear();set.add(9);set.add(100); System.out.println(getLNP(set)); //9100 set.clear();set.add(2);set.add(3);set.add(9);set.add(78); System.out.println(getLNP(set)); //97832 set.clear();set.add(101);set.add(10); System.out.println(getLNP(set)); //10110 set.clear();set.add(97);set.add(8);set.add(9); System.out.println(getLNP(set)); //9978 * set.clear();set.add(8);set.add(87);set.add(89); System.out.println(getLNP(set)); // 89887 * } } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@Logic King No, My algo will take only O(nlgn) where n is no of elements. what i mean by editing the comparision function cmp function of sort of algorithm.h sort(a,a+n, cmp); where cmp is the comparision function defined in my prev. post it will take equal no. of comparision as in sorting. just now overhead of comparision is increased. instead of just or here it is order of no of digits in a no, which is atmost 10 for int or 20 for long ints. so O(20*n*lgn) -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@sunny it will work fine if you have 2 numbers only...but what about the list...3..4 or 5..or morethen the possible number of combinations will be 'N!'...where n is the number of digits...the code will work quite slowly for larger 'n'. On Fri, May 27, 2011 at 3:33 PM, Dave dave_and_da...@juno.com wrote: @Shubham: Try 8, 89, 7. Your algorithm gives 8897, but the right answer is 8987. Dave On May 27, 1:11 pm, shubham shubh2...@gmail.com wrote: check whether these steps work: step 1: sort the given numbers in the decreasing order based on their first digit. step 2: if two numbers come out to be equal in the above case both of their next digit exist then sort on the basis of their next digit, otherwise the number whose next digit doesnot exist should be placed before the other number. step 3: concatenate these numbers. e.g. (0,1,10,100) sorting it gives: 1,10,100,0 = 1101000 (97,8,9) sorting gives: 9,97,8 = 9978 correct me if i'm wrong. Shubham -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: GOOGLE INTERVIEW QUESTION
take “aabab” for example, the result is aba, b,a; however, the right result is aa,bab On Wed, May 11, 2011 at 10:57 AM, shubham shubh2...@gmail.com wrote: check this one out: #includeiostream #includecstdio #includevector #includecstring using namespace std; int check_palin(string str,int *start) { int pos=-1,ret,size=str.size()-1; char last_char=str[size]; while(possize) { ret=0;int i; pos=str.find(last_char,pos+1); for(i=0;i=(size-pos);i++) if(str[i+pos]!=str[size-i]) break; if(i==size-pos+1){(*start)=pos;return (size-pos+1);} } } int main() { string arr; vectorstring palin,str; cinarr;str.push_back(arr); while(arr!=) { int s=0,e=0,max=0,start=0,end=0,len; string tmp=; for(int i=0;iarr.size();i++) { tmp+=arr[i]; len=check_palin(tmp,s); if(lenmax){max=len;start=s;} } tmp=arr.substr(start,max); palin.push_back(tmp);str.pop_back(); tmp=arr.substr(0,start);if(tmp!=) str.push_back(tmp); tmp=arr.substr(start+max);if(tmp!=) str.push_back(tmp); if(str.size())arr=str[str.size()-1];else arr=; } for(int i=0;ipalin.size();i++) coutpalin[i] ; return 0; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards Anders -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@bittu I would like to discuss one thing regarding your approach , How you managed to put forward your 1st statement that is of Synchronization . On Fri, Jan 7, 2011 at 1:18 PM, Pedro Rezende web...@gmail.com wrote: Hi all! And what could be the best way to test / debug issues like these? 2011/1/7 vaibhav agrawal agrvaib...@gmail.com @Douglas, nicely put!!! On Fri, Jan 7, 2011 at 8:37 PM, Douglas Diniz dgdi...@gmail.com wrote: Some examples, supposing you do always the same thing: 1-) You have a program that use some random number, and based on the number the program do different things, and this different things crash the program at different places. 2-) you have a program that connect with a external server. Depending on the links status you could crash in different places. 3-) You have a program that talk with another program (or external server) through a protocol, and the protocol could do different things even if you do the same thing several times. 4-) Your program has timeouts that could expire based on the system usage, crashing the program in different places. 5-) Your program read some system variable and do different things. 6-) etc On Fri, Jan 7, 2011 at 12:09 PM, juver++ avpostni...@gmail.com wrote: The application is single threaded :) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Lalit Kishore Sharma IIIT Allahabad (Amethi Capmus) 6th Sem -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
@Douglas, nicely put!!! On Fri, Jan 7, 2011 at 8:37 PM, Douglas Diniz dgdi...@gmail.com wrote: Some examples, supposing you do always the same thing: 1-) You have a program that use some random number, and based on the number the program do different things, and this different things crash the program at different places. 2-) you have a program that connect with a external server. Depending on the links status you could crash in different places. 3-) You have a program that talk with another program (or external server) through a protocol, and the protocol could do different things even if you do the same thing several times. 4-) Your program has timeouts that could expire based on the system usage, crashing the program in different places. 5-) Your program read some system variable and do different things. 6-) etc On Fri, Jan 7, 2011 at 12:09 PM, juver++ avpostni...@gmail.com wrote: The application is single threaded :) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
Hi all! And what could be the best way to test / debug issues like these? 2011/1/7 vaibhav agrawal agrvaib...@gmail.com @Douglas, nicely put!!! On Fri, Jan 7, 2011 at 8:37 PM, Douglas Diniz dgdi...@gmail.com wrote: Some examples, supposing you do always the same thing: 1-) You have a program that use some random number, and based on the number the program do different things, and this different things crash the program at different places. 2-) you have a program that connect with a external server. Depending on the links status you could crash in different places. 3-) You have a program that talk with another program (or external server) through a protocol, and the protocol could do different things even if you do the same thing several times. 4-) Your program has timeouts that could expire based on the system usage, crashing the program in different places. 5-) Your program read some system variable and do different things. 6-) etc On Fri, Jan 7, 2011 at 12:09 PM, juver++ avpostni...@gmail.com wrote: The application is single threaded :) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
The description on internal nodes indicates this: The value of an AND gate node is given by the logical AND of its TWO children's values. The value of an OR gate likewise is given by the logical OR of its TWO children's values. On 2010-12-28 13:35, suhash wrote: Your approach is for a binary tree, but the problem statement does not say anything about it. On Dec 28, 10:27 am, pacific pacificpacific4...@gmail.com wrote: here is my approach : Starting from the root node , if root node need to have a 1 ... if root is an and gate : flips = minflips for left child to have value 1 + minflips for the right child to have value 1 else flips = minimum of ( minflips for left child to have value 1 , minflips for right child to have value 1) For a leaf node , return 0 if the leaf has the value needed else return INFINITY.Also if at any internal node if it is not possible return INFINITY. On Tue, Dec 28, 2010 at 10:06 AM, Anandanandut2...@gmail.com wrote: @Terence. Could please elaborate your answer. Bottom up level order traversal helps to get the final root value but how to use it to find minimum flips needed to Obtain the desired root value. On Fri, Dec 24, 2010 at 1:56 AM, Terencetechnic@gmail.com wrote: Using the same level order traversal (bottom up), calculating the minimum flips to turn each internal node to given value (0/1). On 2010-12-24 17:19, bittu wrote: Boolean tree problem: Each leaf node has a boolean value associated with it, 1 or 0. In addition, each interior node has either an AND or an OR gate associated with it. The value of an AND gate node is given by the logical AND of its two children's values. The value of an OR gate likewise is given by the logical OR of its two children's values. The value of all of the leaf nodes will be given as input so that the value of all nodes can be calculated up the tree. It's easy to find the actual value at the root using level order traversal and a stack(internal if used recursion). Given V as the desired result i.e we want the value calculated at the root to be V(0 or 1) what is the minimum number of gates flip i.e. AND to OR or OR to AND be required at internal nodes to achieve that? Also for each internal node you have boolean associated which tells whether the node can be flipped or not. You are not supposed to flip a non flippable internal node. Regards Shashank Mani Narayan BIT Mesra -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
Let cst[i][j] store the cost to flip node i to given gate j (0-'AND', 1-'OR'). Then: cst[i][j] = 0,if j==gate[i]; cst[i][j] = 1,if j!=gate[i] and ok[i]; cst[i][j] = INFINITY, if j!=gate[i] and !ok[i]; 1. To get value 1: 1.1 flip current gate to AND, and change all children to 1 1.2 flip current gate to OR, and change any child to 1. 2. To get value 0: 1.1 flip current gate to AND, and change any child to 0 1.2 flip current gate to OR, and change all children to 0. So for internal node i: dp[i][0] = min(cst[i][0]+sum{dp[x][1] for each child x of i}, cst[i][1]+max{dp[x][1] for each child x of i}); dp[i][1] = min(cst[i][0]+max{dp[x][0] for each child x of i}, cst[i][1]+sum{dp[x][0] for each child x of i}); for leaf i: dp[i][j] = (value[i]==j ? 0 : INFINITY) On 2010-12-28 13:32, suhash wrote: This problem can be solved using dp in O(n), where 'n' is the number of nodes in the tree. Definitions: Let gate[i] be a boolean denoting whether the gate at node 'i' is 'AND' or 'OR'. (0-'AND' 1-'OR') Let dp[i][j] store the minimum no. of swaps required to get a value of 'j' (0 or 1), for the subtree rooted at 'i'. Let ok[i] be a boolean which denotes whether a flip operation can be performed at the i'th node or not. Let i1,i2,i3.ik be the children of node 'i' Now we have 2 cases: case 1: ok[i] = 0 (means no flipping possible at node 'i') In this, we have many cases: case 1.1: gate[i]=0 (there is an AND gate at node 'i'), and j=1 this means all children should have a value 1. hence dp[i][j]=dp[i1][j]+dp[i2][j] +.dp[ik][j] case 1.2: gate[i]=0 (there is an AND gate at node 'i'), and j=0 i will discuss this case in the end. case 1.3: gate[i]=1 (there is an OR gate at node 'i'), and j=1 this one too, for the end case 1.4: gate[i]=1 (there is an OR gate at node 'i'), and j=0 this means all children should have a value 0. hence dp[i][j]=dp[i1][j]+dp[i2][j] +.dp[ik][j] case 2: ok[i] = 1 (means flipping is possible at node 'i') We have 2 cases in this: case 2.1: we choose not to flip gate at node 'i'. This reduces to the 4 cases above(case 1.1, 1.2, 1.3, 1.4) case 2.2: we choose to flip gate 'i'. Again it is similar to cases discussed above, except replacing 'AND' by 'OR' and vice versa and dp[i][j]=1 + dp[i1][j]+dp[i2][j] +.dp[ik][j] Note: 1)Boundary cases for leaf nodes. 2)Top down is easier. 3)If it is impossible to get a value 'j' for subtree rooted at 'i', then dp[i][j]=INF(some large value) 4)Answer is dp[root][required_value(o or 1)]. If this quantity is INF, then it is impossible to achieve this. Now, lets discuss case 1.2: We have an 'AND' gate and we want a result of 0. So, atleast one of the children of node 'i' should be 0. Now create 2 arrays x,y each of size 'k' x[1]=dp[i1][0], y[1]=dp[i1][1] x[2]=dp[i2][0], y[2]=dp[i2][1] . . . x[k]=dp[ik][0], y[k]=dp[ik][1] Now, let v=min(x[1],x[2],x[3],.x[k]), and 'h' be the index of the minimum element(x[h] is minimum) Then, dp[i][j]=v+sigma(f!=h)[min(x[f],y[f])] The other cases are similar to this! I can write a code and send if you have doubts. On Dec 28, 9:36 am, Anandanandut2...@gmail.com wrote: @Terence. Could please elaborate your answer. Bottom up level order traversal helps to get the final root value but how to use it to find minimum flips needed to Obtain the desired root value. On Fri, Dec 24, 2010 at 1:56 AM, Terencetechnic@gmail.com wrote: Using the same level order traversal (bottom up), calculating the minimum flips to turn each internal node to given value (0/1). On 2010-12-24 17:19, bittu wrote: Boolean tree problem: Each leaf node has a boolean value associated with it, 1 or 0. In addition, each interior node has either an AND or an OR gate associated with it. The value of an AND gate node is given by the logical AND of its two children's values. The value of an OR gate likewise is given by the logical OR of its two children's values. The value of all of the leaf nodes will be given as input so that the value of all nodes can be calculated up the tree. It's easy to find the actual value at the root using level order traversal and a stack(internal if used recursion). Given V as the desired result i.e we want the value calculated at the root to be V(0 or 1) what is the minimum number of gates flip i.e. AND to OR or OR to AND be required at internal nodes to achieve that? Also for each internal node you have boolean associated which tells whether the node can be flipped or not. You are not supposed to flip a non flippable internal node. Regards Shashank Mani Narayan BIT Mesra -- You received this message because
Re: [algogeeks] Re: Google Interview Question
On Fri, Sep 17, 2010 at 3:36 PM, Krunal Modi krunalam...@gmail.com wrote: Your solutions are pretty impressive. Which place(country) are you from ? where are you studying (or done :) ) ? Keep it up... Good Wishes.. --Krunal On Sep 14, 9:29 pm, Gene gene.ress...@gmail.com wrote: You can approach this the same way you'd do it by hand. Build up the string of brackets left to right. For each position, you have a decision of either ( or ) bracket except for two constraints: (1) if you've already decided to use n left brackets, then you can't use a another left bracket and (2) if you've already used as many right as left brackets, then you can't use another right one. This suggests the following alorithm. Showing what happens on the stack is a silly activity. #include stdio.h // Buffer for strings of (). char buf[1000]; // Continue the printing of bracket strings. // need is the number of ('s still needed in our string. // open is tne number of ('s already used _without_ a matching ). // tail is the buffer location to place the next ) or (. void cont(int need, int open, int tail) { // If nothing needed or open, we're done. Print. if (need == 0 open == 0) { printf(%s\n, buf); return; } // If still a need for (, add a ( and continue. if (need 0) { buf[tail] = '('; cont(need - 1, open + 1, tail + 1); } // If still an open (, add a ) and continue. if (open 0) { buf[tail] = ')'; cont(need, open - 1, tail + 1); } } void Brackets(int n) { cont(n, 0, 0); } int main(void) { Brackets(3); return 0; } On Sep 14, 10:57 am, bittu shashank7andr...@gmail.com wrote: Write a function Brackets(int n) that prints all combinations of well- formed brackets. For Brackets(3) the output would be ((())) (()()) (()) () ()(()) ()()() with explaination dat is at every call what is contant of stack during pushing and popping -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. #includeiostream.h #includeconio.h #includemath.h void addb(int n,int cnt,int i,char *a) { if(n==0) { { while(cnt!=0){ a[i++]=')'; cnt--; } //if(cnt==0) { // printf(%s\n,a); couta\n; } return; } } else { a[i]='('; addb(n-1,cnt+1,i+1,a); if(cnt!=0) { a[i]=')'; addb(n,cnt-1,i+1,a); } } } void main() { clrscr(); char *a; int n; coutn = ; cinn; a=new char[n*2+1]; a[n*2]='\0'; addb(n,0,0,a); getch(); } -- Pratik Kathalkar CoEP BTech IT 8149198343 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question-Snake and Ladder Design
@bittu, we are here to discuss the way to solve it. Posting a code here will not do anything good. Anil Kumar S. R. http://sranil.googlepages.com/ The best way to succeed in this world is to act on the advice you give to others. On 14 September 2010 13:33, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d\n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question-Snake and Ladder Design
Hi On 14 September 2010 13:33, bittu shashank7andr...@gmail.com wrote: #includestdlib.h #includestdio.h #includemath.h #includeconio.h ///O(N^2) solution Does solution exits in O(n) or (nlogn)..? reply me sum1 git dis.. //i will post analysis of dsi program later int turn, square; long game, totalgames; int seed; int chutehit[10], ladderhit[9]; float RunningTurnTotal; float average; char reply; void ChuteStats() {printf(Chute and Ladder Statistics:\n\n); printf(Chute0: %d Ladder0: %d\n, chutehit[0], ladderhit[0]); printf(Chute1: %d Ladder1: %d\n, chutehit[1], ladderhit[1]); printf(Chute2: %d Ladder2: %d\n, chutehit[2], ladderhit[2]); printf(Chute3: %d Ladder3: %d\n, chutehit[3], ladderhit[3]); printf(Chute4: %d Ladder4: %d\n, chutehit[4], ladderhit[4]); printf(Chute5: %d Ladder5: %d\n, chutehit[5], ladderhit[5]); printf(Chute6: %d Ladder6: %d\n, chutehit[6], ladderhit[6]); printf(Chute7: %d Ladder7: %d\n, chutehit[7], ladderhit[7]); printf(Chute8: %d Ladder8: %d\n, chutehit[8], ladderhit[8]); printf(Chute9: %d\n, chutehit[9]); } int main() { printf(Welcome to the Chutes and Ladders simulation \n); printf(...\n); srand(1); //printf(How many games would you like me to run? __ ); //scanf(%i,totalgames); ///printf(\n You have chosen to run %i games... thank you! \n, totalgames); totalgames+=2; RunningTurnTotal=0.0; game=1; do{ turn=0; square=0; /** Reset game **/ do /** Begin game loop **/ { ++turn; RunningTurnTotal = RunningTurnTotal + 1; square = square + 1 + rand()%6; /** Spin and move **/ printf(square =%d \n,square); if (square == 1) {square=23; ++ladderhit[0];} /** Ladders? **/ if (square == 4) {square=14; ++ladderhit[1];} if (square == 9) {square=31; ++ladderhit[2];} if (square == 21) {square=42; ++ladderhit[3];} if (square == 28) {square=84; ++ladderhit[4];} if (square == 36) {square=44; ++ladderhit[5];} if (square == 51) {square=67; ++ladderhit[6];} if (square == 71) {square=91; ++ladderhit[7];} if (square == 80) {square=100;++ladderhit[8];}/// so when 80 comes raech to our goal exit if (square == 98) {square=78; ++chutehit[0];} /** Chutes ? **/ if (square == 95) {square=75; ++chutehit[1];} if (square == 93) {square=73; ++chutehit[2];} if (square == 87) {square=24; ++chutehit[3];} if (square == 62) {square=19; ++chutehit[4];} if (square == 64) {square=60; ++chutehit[5];} if (square == 56) {square=53; ++chutehit[6];} if (square == 49) {square=11; ++chutehit[7];} if (square == 48) {square=26; ++chutehit[8];} if (square == 16) {square=6; ++chutehit[9];} } while (square100);//terminate if random no. is 100 printf(\n\n Game over after %d turns\n, turn); printf(\nSimulation complete... beginning statistical analysis...\n\n); printf(Total number of games played: %d \n, game); printf(Total number of turns: %f \n, RunningTurnTotal); average = RunningTurnTotal / game; printf(Avg number of turns per game: %f \n, average); printf(\n); ChuteStats(); printf(\n); ++game; printf(\n\n Would you like to run the simulation again? (1=Yes)...); scanf(%i,reply); if(reply==1)//e.g. reply==1 totalgames+=1; else exit(0);// exit } while (gametotalgames); getch(); } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. Can you please write an algo for your program ? -- Siddharth Srivastava When you have learned to snatch the error code from the trap frame, it will be time for you to leave. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Google Interview Question
but addition also should be in array On Sun, Aug 22, 2010 at 3:05 AM, arpit agarwal erarpitagar...@gmail.comwrote: Just find out the max and 2nd max in n + log(n) -2 steps and add them. there is no need for sorting as such -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.