Find the number which is odd number of times? [Interview]
Dated: Wed Dec 28 22:44:42 IST 2005
14 int findodd(int *n,int len)
15 {
16 int num=0;
17 for(int i=0;i<len;i++)
18 num^=n[i];
19 return num;
20 }
How would u sort of 0s and 1s in just single pass? [Interview]
Dated: Wed Dec 28 22:27:42 IST 2005
1 void sort_01 (int *n,int len)
2 {
3 int i=0,j=len-1;
4 while(1){
5 while((!n[i])&&(i<len)) i++;
6 while((n[j])&&(j>-1)) j--;
7 if(i>=j) break;
8 n[i]=~n[i]&1;
9 n[j]=~n[j]&1;
10 i++;j--;
11 }
12 }
Find all possible splits for any given number n. [Interview]
Dated: Wed Dec 28 21:33:48 IST 2005
Init : int array[5]={0,0,0,0,0};
Function Call : split(array,5,0);
1 void split(int *n,int count,int len)
2 {
3 if(count==1) {
4 int i;
5 for(i=0;i<len;i++)
6 printf("%d+",n[i]);
7 printf("%d\n",n[i]+1);
8 return;
9 }
10 else {
11 n[len]+=1;
12 split(n,count-1,len);
13 split(n,count-1,len+1);
14 n[len]-=1;
15 }
16 return ;
17 }
WAP to check a integer (as binary string) is pallindrome? [Interview]
Dated: Wed Dec 28 18:27:43 IST 2005
1 bool panlindrome(unsigned long i)
2 {
3 if(!(i&1)) return false;
4 unsigned long t=i,rev=1;
5 t>>=1;
6 while(t) {
7 rev<<=1;
8 if(t&1) rev |=1;
9 t >>=1;
10 }
11 if(rev==i) return true;
12 return false;
13 }
Find the number of ones in an integer? [Interview]
Dated: Wed Dec 28 18:17:14 IST 2005
1 unsigned noofone(unsigned long i)
2 {
3 int count=0;
4 while(i) i=i&(~(i&(~i+1))),count++;
5 return count;
6 }
WAP to convert a given integer to octal number? [Interview]
Dated: Wed Dec 28 17:45:04 IST 2005
1 #include<stdio.h>
2 void int2octal (unsigned long i)
3 {
4 if(i) {
5 unsigned long o = i&7;
6 i>>=3;
7 int2octal(i);
8 printf("%d",o);
9 }
10 else printf("0");
11 }
How to reverse a singly-linked list? [Interview]
Dated: Tue Dec 27 06:49:35 IST 2005
1 node* reverse_list(node *list)
2 {
3 node *rev=NULL;
4 while(list !=NULL) {
5 node *t = list->next;
6 list->next = rev;
7 rev = list;
8 list = t;
9 }
10 return rev;
11 }
3 comments:
nice turorial
good post
nice post
Post a Comment