Search Bhai

 

Visitors

Reverse 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)

{
int end = strlen(str)-1;
for (int i = 0; i <>
if (*(str+i) != *(str+end))
return false;
return true;
}

Find a repeated integer in array of size n?

int DuplicatedNumber(int *a, int size)
{
map hash;
bool founddup = false;
for(int i=0; i 0)
{
founddup= true;
break;
}
else
hash[a[i]]++;
}

return founddup ? a[i] : -1; // return the duplicated value or -1 if not found
}

Giving 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 _ _ _
1 2 3 4 5 6

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) {
if (!*head) return;
NODE *cur = *head;
NODE *prev = NULL;
NODE *next = NULL;
while (cur) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
*head = prev;
return;

}



Recusive way call with original = list, parent = NULL

Node* reverselinkedlist(Node* original, Node*parent)
{
Node* reverse;

if(original == NULL)
{
reverse = parent;

}
else
{
reverse = reverselinkedlist(original->link,original);
original->link = parent;
}

return reverse;

}

Your Ad Here

Xcel-Online

Rent A Coder