Skip to main content

Posts

Showing posts from May, 2009

Answer for link list

9. Find whether a list is even or odd. a) Keep counting. b) Use a bool for flag c) Without any variable int isOdd ( struct node *head ) { Head = head->next; While ( head) { If ( head->next == NULL ) Return 1; Head = head->next->next; } } 10. Reverse a link list with/without recursion. Void reverselist( struct node *head ) { Struct node *prev, *current, *temp; Prev = NULL; Current = head->next; While ( current ) { Temp = current->next; Current->next = prev; Prev = current; Current = temp; } Head->next = prev; } 11. Shuffle two link list of same size. If p and q are head pointers. Struct node *newhead = p; While (q ) { Swap(&p->next, &q); P = p->next; } 12. Merge two sorted link list. 13. Find whether two list are intersecting or not. a) Use negation technique. Traverse list 1 and negate elements. Traverse list 2 and return first negated element. b) Hashtable: insert node in hashtable. If found collision return element. c) Length of list 1 = m length

Answer for doubly link list

18. Reverse the doubly link list. For ( p = head; p->next != head; p = p->prev ) Swap (&p->next,&p->prev); 19. Doubly link list a single pointer. While storing the address store address as xor of the previous and next. 20. Reverse the link list with ‘k’ nodes. Take the k+1 the element pointer and put as the next for first element . Reverse the K sized list . 21. Find a list in palindrome or not. a) Find the middle node, reverse link list after that. Start comparing from head and the node after the middle node.

Answer for Stack Q

22. No of permutations with n numbers. (1/(n+1)2ncn). a) No of permutations are 1/(n+1) (2n)cn. b) No of stack permutation possible. c) No of binary trees possible with n nodes. d) No of ways to parenthesise with n data element. 23. A string with a’s and b’s and with one special character ‘x’. Find out whether its palindrome or not. a) Take two pointer one from start and another from end. Start comparing till pointers don’t cross each other. 24. Implement queue by stack with enque and deque of order O(1); Pop all the element and push in second stack2. The top2 will point to the beginning of Deque and top1 will be at end. Maintain size1 and size2 for the bottom removing with both the cases. Add at beg: push in stack2, size1 and size2 incremented. Add at end: push in stack1, size1 and size2 incremented. Delete at beg: pop in stack2 and decrement size1 and size2. Delete at end: pop in stack2 and decrement size1 and size2. 25. Convert infix expression to postfix. While ( ch ) { If ( ch is

Answers for MISC questions

39. Find first non repeating character in a string. a) Bruteforce: TC: o(n2) SC: o(1) b) use another array or use hashtable 40. Find whether two arrays of same size have same values. Go through first array, hash every element and increment the count if repeated. Go through second and decrement count for every element and at the lost every index should be having zero. 41. Given a minheap find the max element. Sol) max element will always be at leaf, take the last element in heap and find its parent which will be always the last non leaf node. Find the max element after the last non leaf node and last node. 42. Convert an array to heap. a) Sort the array and sorted array is always min heap. b) Add the first element in heap and then second. Now particulate for heap property. Keep adding element and managing heap property. c) Bottom up heap building ( I really did not understand this concept. Please google). 43. Kth element in a Min heap. a) Bruteforce: Call delete min () k times. TC (nl

Answers for Binary Tree Questions

26. Find height of a tree with or without recursion. Int FindHeight ( struct node *root) { If ( !root) return 0; Return ( 1+max (findHeight(root->left),findheight(root->right))); } Note: For a tree height and depth is same, but for an individual node its same. Root’s height is 1 while depth is 0. Reader: Can any one post the algorithm for finding tree height without recursion. 27. Find the levels in tree and no of node at every level. Use BFS. Void levelorder( struct binarynode * root) { Enque(root); While ( !emptyQ()) { Temp = deque(q); // Process node If ( temp->left) enque(q,temp->left); If ( temp->right) enque(q,temp->right); } } Use the BFS and put the NULL after every deque give you a null. 28. No of leaves on tree. Int countleaves( struct binarynode * root) { If ( !root) return 0; If ( !root->left && !root->right) return 1; Return ( countleaves(root->left) + countleaves(root->right)); } Can anyone write the algorit

Trie

62. Find largest word in dictionary. 63. display all word in dictionary with starting char “ab”. 64. Find a element in an array with indefinite length. 65. Find the size of indefinite length array. Note all elements are initialized with @. 66. Find which rows is having maximum zeroes. 1 1 1 1 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 Once a rows got a zero there wont be any more 1’s. 67. In a matrix where each row and each column is sorted, find a number in o(1). 68. Find a element in bitonic array. Bitonic array first increase and decrese or vice versa. 69. Find a element in a rotated sorted array. 70. Find whether X is majority element in an array.

Misc

39. Find first non repeating character in a string. 40. Find whether two arrays of same size have same values. 41. Given a minheap find the max element. 42. Convert an array to heap. 43. Kth element in a Min heap. 44. implement a stack using heap. 45. Implement queue using heap. 46. Find union of k sorted list. 47. Merge the two heap. 48. Fibonocci with memorization/ Dynamic programming. 49. Find no of BST can be formed with n nodes. 50. Find no of legeal path between x and y which does not include ‘+’ A - + - - + - - - - - + - + + - - - - - - B 51. Find the largest common subsequence between two given strings. 52. Find the shortest common super sequence. 53. In a string, find the longest substring which is palindrome. 54. Find the maximum sum subarray without selecting two consecutive number. 55. optimal matrix chain multiplication. 56. Generate binary sequence of constant ‘N’. 57. Given an array generate all the possibilities of a

Binary Tree

26. Find height of a tree with or without recursion. 27. Find the levels in tree and no of node at every level. 28. No of leaves on tree. 29. Find the diameter of tree. Longest distance between two same level nodes. 30. Write pre/in/post order traversal with and without recursion. 31. Serialization of binary tree. Storing data/object in a file and then retrieving it back. 32. Find the least common ancestor for any two given nodes from tree. 33. Threaded binary tree and link inversion tree. 34. Find whether a given tree is BST or not. 35. Write code for inorder successor. 36. Find the kth smallest element of BST. 37. Convert BST to doubly link list. 38. Is the given BST is AVL or not?

Binary Tree

26. Find height of a tree with or without recursion. 27. Find the levels in tree and no of node at every level. 28. No of leaves on tree. 29. Find the diameter of tree. Longest distance between two same level nodes. 30. Write pre/in/post order traversal with and without recursion. 31. Serialization of binary tree. Storing data/object in a file and then retrieving it back. 32. Find the least common ancestor for any two given nodes from tree. 33. Threaded binary tree and link inversion tree. 34. Find whether a given tree is BST or not. 35. Write code for inorder successor. 36. Find the kth smallest element of BST. 37. Convert BST to doubly link list. 38. Is the given BST is AVL or not?

Stack

22. No of permutations with n numbers. (1/(n+1)2ncn). 23. A string with a’s and b’s and with one special character ‘x’. Find out whether its palindrome or not. 24. Implement queue by stack with enque and deque of order O(1); 25. Convert infix expression to postfix.

Doubly Link List

18. Reverse the doubly link list. 19. Doubly link list a single pointer. 20. Reverse the link list with ‘k’ nodes. 21. Find a list in palindrome or not.

Link List

9. Find whether a list is even or odd. 10. Reverse a link list with/without recursion. 11. Shuffle two link list of same size. 12. Merge two sorted link list. 13. Find whether two list are intersecting or not. 14. Detect a loop in link list. 15. Remove loop from link list. 16. Find the length of loop. 17. Find the kth element from end.

Array

1. Given a arrary return an element not in array. 2. Previous problem with number will be in range of 0- (n-1). 3. Find the number repeating most number of times in a n size array with no in the range of 0-(n-1). 4. Find whether a[i]+a[j]=x in a sorted and unsorted array. 5. Maximum of two number without if statement. 6. Print 1-100 without loop, if, goto etc. 7. Find the only non-repeating number in a array. 8. Find the two number which is non-repeating in a array I am posting questions here only. I will shortly update with answer to them. Till then keep trying.