Map reduce implementation of reservoir sampling is discussed in this blog post.
http://had00b.blogspot.in/2013/07/random-subset-in-mapreduce.html
|
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.
|
Categories
FollowersBlog Archive
Jobs
Find more freelance jobs
|
Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts
Sunday, November 21, 2010How 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 21, 2010Find next number in Sequence
you have a sequence where each number is a multiple of 2 or 5 (so: 2^i * 5^j). Given the beginning of the sequence as 1,2,4,5,8,10,16... and find a algorithm to calculate the next number in the sequence?
Answer: This answer gives u the pattern to make the sequence. Algorithm would look like this. 1. Make set1 with all powers of 2(i.e 2,4,8,...) 2. Make set2 with all powers of 5(i.e 5,25,125 ...) 3. Make set3 with product of numbers from set1 and set2(i.e 10,20,40,50,80,100 etc..) Make sure that third set is sorted. Now display elements from set1 till u find a element less than starting element in set2 or set3. Otherwise display minimum element out of elements from set2 or set3. Tuesday, August 11, 2009Design 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; Sunday, February 17, 2008Design a stack which have O(1) time complexity for finding the minimum element?
We can think of two different solutions -
Sol 1: With additional memory where time complexity of findmin(),Push(),Pop() is O(1) but space complexity is O(n). Along with pushing the elements push the current minimum on to the stack. For pop operation pop 2 elements instead of 1 , it is still O(1). For ex: Let us assume the following numbers which are pushed on to the stack. 4 2 9 3 6 1 Stack state will be something like below 4 4 2 2 9 2 3 2 6 2 1 1. POP(x) = pop(), pop(). Where POP(x) is modified stack pop operation. pop() is standard pop() operation. FINDMIN(x)=pop() PUSH(x)=push() and push( MIN(pop(),elem)). Where PUSH(x) is modified stack push operation. push() is standard push() operation. Here storage needed for stack is 2n where n is number of elements in the stack. There is another optimal solution which i will post later. Saturday, February 2, 2008Given a singly linked list, determine whether it contains a loop or not.1. Start reversing the list. If you reach the head, then there is a loop. p1 = p2 = head; do { p1 = p1->next; p2 = p2->next->next; } while (p1 != p2); 3. Hash all seen nodes and compare the next node with the nodes in the hash. If a node is already present in the hash then there is a loop. Under what circumstances can one delete an element from a singly linked list in constant time?
If the list is circular and there are no references to the nodes in the list from anywhere else! Just copy the contents of the next node and delete the next node. If the list is not circular, we can delete any but the last node using this idea. In that case, mark the last node as dummy!
Remove duplicates from a linked list ?
NODE *DupRem(NODE *head)
{ NODE *p1 = head, *p2, *resultList = NULL, *temp = NULL; while(p1 != NULL) { p2 = p1->link; while(p2 != NULL) { if(p1->data == p2->data) { break; } else { p2 = p2->link; } } if(p1->data==p2->data) { p1 = p1->link; }else { if(resultList == NULL) { resultList = p1; temp = p1; p1 = p1->link; }else { temp->link = p1; temp = temp->link; p1 = p1->link; } } } return resultList; } Given two sorted linked lists, write a function to merge them into one?
list* merge(LIST* list1, LIST* list2){
LIST *it1, *it2, *head; if(!list1) return list2; if(!list2) return list1; head = (list1->value <= list2->value) ? list1 : list2; if(head == list1) { it1 = list1; it2 = list2; } else{ it1 = list2; it2 = list1; } while(it1->next && it2){ if(it1->next->value >= it2->value){ list *temp = it1->next; it1->next = it2; it2 = it2->next; it1->next->next = temp; } else{ it1 = it1->next; } } if(it2) Given a single linked list write a function to swap each pair of nodes by manipulating with pointers (not values).?
Sol:
Original list: head->1->2->3->4->5->NIL should be transformed to head->2->1->4->3->5-> NIL int reverse_pairs(LLIST ** head) Friday, January 18, 2008WAP 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){ }
Subscribe to:
Posts (Atom)
|
Popular Posts
|