# Data Structures and Algorithms | Set 31

Following questions have been asked in GATE CS 2013 exam.

**1) What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.**

int f(int &x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; }

(A) 3024

(B) 6561

(C) 55440

(D) 161051

Answer (B)

Since c is passed by value and x is passed by reference, all functions will have same copy of x, but different copies of c.

f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4

Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561.

`#include <stdio.h>` ` ` `int` `f(` `int` `&x, ` `int` `c)` `{` ` ` `c = c - 1;` ` ` `if` `(c == 0) ` `return` `1;` ` ` `x = x + 1;` ` ` `return` `f(x, c) * x;` `}` `int` `main()` `{` ` ` `int` `p = 5;` ` ` `printf` `(` `"%d"` `, f(p, p));` `}` |

**1) The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?**

(A) 10, 20, 15, 23, 25, 35, 42, 39, 30

(B) 15, 10, 25, 23, 20, 42, 35, 39, 30

(C) 15, 20, 10, 23, 25, 42, 35, 39, 30

(D) 15, 10, 23, 25, 20, 35, 42, 39, 30

Ans (D)

The following is the constructed tree

30 / \ 20 39 / \ / \ 10 25 35 42 \ / 15 23

**3) Consider the following function**

int unknown(int n) { int i, j, k = 0; for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = j * 2) k = k + n/2; return k; }

**What is the returned value of the above function?**

(A) Θ(n^2)

(B) Θ(n^2Logn)

(C) Θ(n^3)

(D) Θ(n^3Logn)

Answer (B)

The outer loop runs n/2 or Θ(n) times. The inner loop runs Θ(Logn) times (Note that j is divide by 2 in every iteration). So the statement "k = k + n/2;" runs Θ(nLogn) times. The statement increases value of k by n/2. So the value of k becomes n/2*Θ(nLogn) which is Θ(n^2Logn)

**4) The number of elements that can be sorted in Θ(logn) time using heap sort is**

(A) Θ(1)

(B) Θ(sqrt(logn))

(C) Θ(Log n/(Log Log n))

(d) Θ(Log n)

Answer (C)

Time complexity of Heap Sort is Θ(mLogm) for m input elements. For m = Θ(Log n/(Log Log n)), the value of Θ(m * Logm) will be Θ( [Log n/(Log Log n)] * [Log (Log n/(Log Log n))] ) which will be Θ( [Log n/(Log Log n)] * [ Log Log n - Log Log Log n] ) which is Θ(Log n)

**5) The procedure given below is required to find and replace certain characters inside an input character string supplied in array A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in array newc. Array A has a fixed length of five characters, while arrays oldc and newc contain three characters each. However, the procedure is flawed**

void find_and_replace(char *A, char *oldc, char *newc) { for (int i = 0; i < 5; i++) for (int j = 0; j < 3; j++) if (A[i] == oldc[j]) A[i] = newc[j]; }

**The procedure is tested with the following four test cases(1) oldc = "abc", newc = "dab"(2) oldc = "cde", newc = "bcd"(3) oldc = "bca", newc = "cda"(4) oldc = "abc", newc = "bac"The tester now tests the program on all input strings of length five consisting of characters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’ with duplicates allowed. If the tester carries out this testing with the four test cases given above, how many test cases will be able to capture the flaw?**

(A) Only one

(B) Only two

(C) Only three

(D) All four

Answer (B)

The test cases 3 and 4 are the only cases that capture the flaw. The code doesn't work properly when an old character is replaced by a new character and the new character is again replaced by another new character. This doesn't happen in test cases (1) and (2), it happens only in cases (3) and (4).

**6) If array A is made to hold the string “abcde”, which of the above four test cases will be successful in exposing the flaw in this procedure?**

(A) None

(B) 2 only

(C) 3 and 4 only

(D) 4 only

Answer (C)

`#include <stdio.h>` `#include <string.h>` ` ` `void` `find_and_replace(` `char` `*A, ` `char` `*oldc, ` `char` `*newc) {` ` ` `for` `(` `int` `i = 0; i < 5; i++)` ` ` `for` `(` `int` `j = 0; j < 3; j++)` ` ` `if` `(A[i] == oldc[j]) A[i] = newc[j];` `}` ` ` `int` `main()` `{` ` ` `char` `*oldc1 = ` `"abc"` `, *newc1 = ` `"dab"` `;` ` ` `char` `*oldc2 = ` `"cde"` `, *newc2 = ` `"bcd"` `;` ` ` `char` `*oldc3 = ` `"bca"` `, *newc3 = ` `"cda"` `;` ` ` `char` `*oldc4 = ` `"abc"` `, *newc4 = ` `"bac"` `;` ` ` ` ` `char` `test[] = ` `"abcde"` `;` ` ` ` ` `printf` `(` `"Test 2\n"` `);` ` ` `printf` `(` `"%s\n"` `, test);` ` ` `find_and_replace(test, oldc2, newc2);` ` ` `printf` `(` `"%s\n"` `, test);` ` ` ` ` `printf` `(` `"\nTest 3\n"` `);` ` ` `strcpy` `(test, ` `"abcde"` `);` ` ` `printf` `(` `"%s\n"` `, test);` ` ` `find_and_replace(test, oldc3, newc3);` ` ` `printf` `(` `"%s\n"` `, test);` ` ` ` ` `printf` `(` `"\nTest 4\n"` `);` ` ` `strcpy` `(test, ` `"abcde"` `);` ` ` `printf` `(` `"%s\n"` `, test);` ` ` `find_and_replace(test, oldc4, newc4);` ` ` `printf` `(` `"%s\n"` `, test);` `}` |

Output:

Test 2 abcde abbcd Test 3 abcde addde Test 4 abcde aacde

Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in **GATE Test Series Course**.

Learn all **GATE CS concepts with Free Live Classes** on our youtube channel.