Data Structures and Algorithms | Set 29
Following questions have been asked in GATE 2012 exam.
1) The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n discs is
(A) T(n) = 2T(n – 2) + 2
(B) T(n) = 2T(n – 1) + n
(C) T(n) = 2T(n/2) + 1
(D) T(n) = 2T(n – 1) + 1
Answer (D)
Following are the steps to follow to solve Tower of Hanoi problem recursively.
Let the three pegs be A, B and C. The goal is to move n pegs from A to C.
To move n discs from peg A to peg C:
move n-1 discs from A to B. This leaves disc n alone on peg A
move disc n from A to C
move n?1 discs from B to C so they sit on disc n
The recurrence function T(n) for time complexity of the above recursive solution can be written as following.
T(n) = 2T(n-1) + 1
2) Consider the directed graph shown in the figure below. There are multiple shortest paths between vertices S and T. Which one will be reported by Dijstra?s shortest path algorithm? Assume that, in any iteration, the shortest path to a vertex v is updated only when a strictly shorter path to v is discovered.

(A) SDT
(B) SBDT
(C) SACDT
(D) SACET
Answer (D)
3) Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements. Assume that the insertion and deletion operation are carried out using REAR and FRONT as array index variables, respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are
(A) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
(B) Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR
(C) Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT
(D) Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT
Answer (A)
See this for details.
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.
You may also like following posts
Normally in an interview you get some qunistoes on linked-lists, arrays, binary trees and such and knowing the differences between them.I practical uses, ArrayList, Collection, Dictionary, HashMap, TreeMap object are much more practical.You should definately understand string parsing, such as counting vowels in a sentence or being able to write your own find/replace substring algorithm.At the advanced level you should be intimately familar with stacks, heaps, trees, recursions, different ways to implement sorting algorithm, and way one is better then another for your given use, and data structure or cominational structure would be good given a certain situation.