Data Structures and Algorithms | Set 3 - GeeksforGeeks

05 Jul, 2018
Following questions have asked in GATE CS exam.
1. Suppose you are given an array s[1...n] and a procedure reverse (s,i,j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence
do, where 1 < k <= n:
  reverse (s, 1, k);
  reverse (s, k + 1, n);
  reverse (s, 1, n);

(GATE CS 2000)(a) Rotates s left by k positions(b) Leaves s unchanged(c) Reverses all elements of s(d) None of the above
Answer: (a)Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. Please see this post for details.If we rotate an array n times for k = 1 to n, we get the same array back.
2. The best data structure to check whether an arithmetic expression has balanced parentheses is a (GATE CS 2004)a) queueb) stackc) treed) list
Answer(b)There are three types of parentheses [ ] { } (). Below is an arbit c code segment which has parentheses of all three types.
void func(int c, int a[]){   return  ((c +2)  + arr[(c-2)]) ; }
Stack is a straightforward choice for checking if left and right parentheses are balanced. Here is a algorithm to do the same.
/*Return 1 if expression has balanced parentheses */bool areParenthesesBalanced(expression ){    for each character in expression   {      if(character == ’(’ || character == ’{’ || character == ’[’)         push(stack, character);      if(character == ’)’ || character == ’}’ || character == ’]’)       {         if(isEmpty(stack))             return 0; /*We are seeing a right parenthesis                         without a left pair*/           /* Pop the top element from stack, if it is not a pair             bracket of character then there is a mismatch.              This will happen for expressions like {(}) */         else if (! isMatchingPair(pop(stack), character) )            return 0;         }   }       if(isEmpty(stack))     return 1; /*balanced*/   else       return 0;  /*not balanced*/   } /* End of function to check parentheses */  /* Returns 1 if character1 and character2 are matching left   and right parentheses */bool isMatchingPair(character1, character2){   if(character1 == ‘(‘ && character2 == ‘)’)     return 1;   else If(character1 == ‘{‘ && character2 == ‘}’)     return 1;   else If(character1 == ‘[‘ && character2 == ‘]’)     return 1;   else      return 0;}
3. Level order traversal of a rooted tree can be done by starting from the root and performing (GATE CS 2004)a) preorder traversalb) in-order traversalc) depth first searchd) breadth first search
Answer(d)See this post for details
4. Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true?i. 9679, 1989, 4199 hash to the same valueii. 1471, 6171 has to the same valueiii. All elements hash to the same valueiv. Each element hashes to a different value(GATE CS 2004)
a) i onlyb) ii onlyc) i and ii onlyd) iii or iv
Answer (c)
5. Postorder traversal of a given binary search tree, T produces the following sequence of keys10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29Which one of the following sequences of keys can be the result of an in-order traversal of the tree T? (GATE CS 2005)a) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Answer (a)Inorder traversal of a BST always gives elements in increasing order. Among all four options, a) is the only increasing order sequence.
GATE-CS-2004
GATE-CS-DS-&-Algo
GATE CS
MCQ
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Page Replacement Algorithms in Operating Systems
Differences between TCP and UDP
Data encryption standard (DES) | Set 1
Introduction of Operating System - Set 1
Types of Network Topology
Data Structures and Algorithms | Set 21
Practice questions on Height balanced/AVL Tree
Operating Systems | Set 1
Computer Networks | Set 1
Computer Networks | Set 2