LET'S TALK TECHNICAL

This blog is intended to help people prepare for the job interviews and improve their analytical skills. We have posted difficult datastructures and algorithm questions and puzzles. Interview experiences section is for the people to post their interview experiences.Views expressed here are of their personal and the blog author doesn't take any responsibility for the same.

-

Followers

Jobs

Saturday, January 19, 2008

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
}

Friday, January 18, 2008

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;

}

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){
i++;
node = node->next;

}
if(i != N)
{
printf("\n Lesser than N elements in list %d \n ",N);
return ;
}
else
{
while(node != NULL)
{
node = node->next;
nelement = nelement->next;
}
}
printf("\n %dth element of link list from end is : %d " ,N,nelement->info);
}

Swap 2 values with XOR ?

#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

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.
pour out cup3, pour cup5 into cup3, cup3 has 2quarters.
fill cup5, pour it in cup3 until cup3 fills
now cup5 has 4quarters.

Popular Posts