Categories
FollowersBlog Archive
Jobs
Find more freelance jobs
|
Saturday, January 19, 2008Reverse each individual word in sentence?
void reversestr(char* str, char* strend)
{ char tmp; while(strend>str) { tmp = *strend; *strend = *str; *str = tmp; str++; strend--; } return; } void reverseSen(char *sentence) { char *wordstart=sentence, *wordstop=sentence; char *pSend=sentence,*pSstart =sentence ; int size=0; while(*pSend != '\0'){ pSend++; size++; } pSend--; // reverese stentence reversestr(pSstart,pSend); while(wordstop < pSend) { //find the word while((*wordstop!='\0')&&(*wordstop!=' ')) wordstop++; wordstop--; //reverese the word reversestr(wordstart,wordstop); wordstart = wordstop +2; wordstop = wordstart; } } Find out if a string is a palindrome?
bool Palindrome(char *str)
{ Find a repeated integer in array of size n?int DuplicatedNumber(int *a, int size) return founddup ? a[i] : -1; // return the duplicated value or -1 if not found Friday, January 18, 2008Giving Two Strings, Find out whether they are Anagrams(made up of same chars) or not?
If(strlen(Str1)!=strlen(Str2)) return FALSE;
sort both Str1,Str2. If(str1==str2) return TRUE; else return FALSE; Game of Russian roulette!!You are tied to a chair and can,t get up.Here is the gun , six chambers all empty. Now I put two bullets in the gun and I put these bullets in the adjecent chambers. I close the barrel and spin it. I put the gun to your head and pull the trigger. Clik and the slot was empty. Now before we start the interview I want to pull the trigger one more time , which one do you prefer , that I spon the barrel first or that I just pull the trigger ? X being a bullet, _ being and empty chamber. The gun rotates from 1 to 6, and the bullets are in adjacent chambers. The setup: _ X X _ _ _ The interviewer just got a blank, so the current position must be 1,4,5, or 6. If it’s 1, the next one is a bullet. If it’s 4-6, the next one is empty. So if the interviewer just fires again, you have a 1/4 chance of getting shot. If, on the other hand, the interviewer spins again, you’re back to the initial 2/6 or 1/3 chance of getting shot. I’ll take the 1/4 chance, myself. WAP to reverse a linked list?void reverse(NODE **head) { } Recusive way call with original = list, parent = NULL Node* reverselinkedlist(Node* original, Node*parent) if(original == NULL) } return reverse; } How would you find the nth to last element in a linked list?
Sol: Have 2 pointers at the beginning of the linked list. Move ptr1 by n locations. Then start moving ptr2(which is at beginning of the list) parallel with ptr1. By the time ptr1 reaches the end of linked list, ptr2 will be at the nth last element.
Code: void nth_last_elment(NODE *ln, int N) { NODE *nelement = ln, *node = ln;int i = 0 ; while(i < N && node != NULL){ } Swap 2 values with subtraction and addition?#define SWAP(a, b) ((&(a) == &(b)) || \ (((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))
If you had an infinite supply of water and a 5 quart and 3 quart pail, how would you measure exactly 4 quarts?fill cup5 , pour it in cup3, cup5 left with 2quarters.
Subscribe to:
Posts (Atom)
|
Popular Posts
|