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

Showing posts with label algorithms coding. Show all posts
Showing posts with label algorithms coding. Show all posts

Sunday, November 21, 2010

How to select k sample nodes from a unknown sized linked list.?

http://en.wikipedia.org/wiki/Reservoir_sampling can be applied here. Is there any better solution for this problem?

Map reduce implementation of reservoir sampling is discussed in this blog post.
http://had00b.blogspot.in/2013/07/random-subset-in-mapreduce.html

Sunday, March 28, 2010

Finding number of on bits (1 bits) given an unsigned integer?

Sol 1:
Simple Iterative solution.(Very slow)

int countbits (unsigned int number) {

int count = 0;

while (number) {

count += number & 0x1u;

number >>= 1;

}

return count;

}

Sol 2:

Faster Solution for 32 bit machines.
compute 16bits then add then to return full number of ones in 32 bit number.

static char ones_in_16bit_num [0x1u << 16] ;
int countbits (unsigned int number)
{
return ones_in_16bit_num [number & 0xffffu] + ones_in_16bit_num [(number >> 16) & 0xffffu] ;

}

Tuesday, August 11, 2009

Design a stack which have O(1) time complexity for finding the minimum element?

This problem can be solved using simple encoding technique. We should use the minimum element to get the next minimum element.Here is a solution which uses O(1) space.PUSH(elem) Operation: In this process we are keeping minimum element on the top of the stack at any given time. And encoding(i.e elem-min) the actual data with minimum and storing. Whenever there is a change in cur minimum data stored in the stack will be negative. This property will be used while poping the elements to find the next minimum. We can define it as shown below. if stack is empty. Store the element twice Else CurMin = POP(); Take difference between elem and Curmin and push it on to stack. Push the least of elem and CurMin on to stack.
POP() Operation: POP process is bit tricky. If minimum is not changing (i.e elements are positive on stack) then return the element + curMin(sitting on stack). If you see a negative stack element then it means there was a shift of curMin (or minimum element is the one which is being popped). In this case we take the current minimum and return it as popped element. We find the new minimum (as current minimum is popped out) by computing curMin-element and push it on top of stack as minimum element. We can summarize it as shown below.curMin = pop();element = pop(); If element is negative then return curMin and push curMin-element on to stack. Else push curMin on to stack and return element + curMin.
FindMin():
This is trivial operation as we are storing minimum element on the top of the stack at any given time. So we pop it and return to the caller. FindMin() steps will be...min=pop();push(min);return min;

Thursday, March 5, 2009

codechef

Introduction to CodeChef
CodeChef.com is India’s first, non-commercial, multi-platform online programming competition. CodeChef features monthly contests, practice problems and discussion boards, all geared towards helping students and professionals improve their software development skills. Our judging system accepts solutions in over 35 different programming languages (like Haskell, Ruby, Python, PHP, Perl, C, C++, C#, Java, Pascal… ) allowing users to test their skills against their peers as well as experiment with new technologies.

About CodeChef

CodeChef was created by Directi as a way to continuously challenge and engage the developer community. The site’s goals are to provide a platform for practice, competition and improvement, as well as enable developers to benchmark their skills against their peers. The first online contest begins March 1st through March 15th, and prizes include an Asus Eee PC, a Nokia 5800 and an iPod Touch.

The CodeChef Community

Despite launching only a month ago, CodeChef has over 1200 registered users, who submit hundreds of solutions each day. We’re able to stay connected with the developer community through our Blog, Forums, Twitter and our newly created Facebook Group. We have also announced a set of unique initiatives: CodeChef Campus Chapters and the User group outreach program. We have some exciting new features in the works including a comprehensive ranking system and Facebook Connect integration.

Tuesday, February 24, 2009

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true)
(given s1 = ABCD, and s2 = ACBD , return false)

Sol:
This is most frequently asked question. It can be solved with simple logic as given below.

1 . Check if both s1, s2 are of same length.
2. if( strstr(strcat(s1,s1), s2) !=NULL) return TRUE
else
return FALSE

Example:
if s1 = ABCD, the concatination will make it ABCDABCD and hence any rotation will fall within the concatenated string. s2 is italicized in conctenized in above concatenated string.

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

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;

}

Popular Posts