Vous êtes sur la page 1sur 226

# Tree and graph traversals(5 Questions)

## Connected components(5 Questions)

Spanning trees(5 Questions)
Shortest paths(5 Questions)

http://geeksquiz.com/algorithms/graph-traversals/
Question 1
Which of the following algorithms can be used to most efficiently determine the presence of a cycle in
a given graph ?
A
Depth First Search
B
C
Prim's Minimum Spanning Tree Algorithm
D
Kruskal' Minimum Spanning Tree Algorithm

Discuss it

Question 2
Traversal of a graph is different from tree because
A
There can be a loop in graph so we must maintain a visited flag
for every vertex
B
DFS of a graph uses stack, but inorrder traversal of a tree is
recursive
C
BFS of a graph uses queue, but a time efficient BFS of a tree is
recursive.
D
All of the above

Discuss it

Question 3
What are the appropriate data structures for following algorithms?
2) Depth First Search
3) Prim's Minimum Spanning Tree
4) Kruskal' Minimum Spanning Tree
A
1) Stack
2) Queue
3) Priority Queue
4) Union Find
B
1) Queue
2) Stack
3) Priority Queue
4) Union Find
C
1) Stack
2) Queue
3) Union Find
4) Priority Queue
D
1) Priority Queue
2) Queue
3) Stack
4) Union Find

Discuss it

Question 4
The Breadth First Search algorithm has been implemented using the queue data structure. One
possible order of visiting the nodes of the following graph is

A
MNOPQR
B
NQMPOR
C
QMNPRO
D
QMNPOR

Discuss it

Question 5
Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-
first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting
u in the traversal. Which of the following statements is always true? (GATE CS 2000)
A
{u,v} must be an edge in G, and u is a descendant of v in T
B
{u,v} must be an edge in G, and v is a descendant of u in T
C
If {u,v} is not an edge in G then u is a leaf in T
D
If {u,v} is not an edge in G then u and v must have the same
parent in T

Discuss it

Question 6
Consider the following graph
Among the following sequences
I) a b e g h f
II) a b f e h g
III) a b f h g e
IV) a f g h b e Which are depth first traversals of the above graph? (GATE CS 2003)
A
I, II and IV only
B
I and IV only
C
II, III and IV only
D
I, III and IV only

Discuss it

Question 7
Make is a utility that automatically builds executable programs and libraries from source code by
reading files called makefiles which specify how to derive the target program. Which of the following
standard graph algorithms is used by Make.
A
Strongly Connected Components
B
Topological Sorting
C
D
Dijkstra's Shortest Path

Discuss it

Question 8
Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can be used to find
if there is path from s to t?
A
Only BFS
B
Only DFS
C
Both BFS and DFS
D
Neither BFS nor DFS

Discuss it

Question 9
Which of the following condition is sufficient to detect cycle in a directed graph?
A
There is an edge from currently being visited node to an
B
There is an edge from currently being visited node to an
ancestor of currently visited node in DFS forest.
C
Every node is seen twice in DFS.
D
None of the bove

Discuss it

Question 10
Is following statement true/false If a DFS of a directed graph contains a back edge, any other DFS of
the same graph will also contain at least one back edge.
Source:http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-s2009-sol.pdf
A
True
B
False

Discuss it

Question 11
Is following statement true/false? A DFS of a directed graph always produces the same number of
tree edges, i.e., independent of the order in which vertices are considered for DFS.
(Sourcehttp://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2008-sol.pdf)
A
True
B
False

Discuss it

Question 12
If the DFS finishing time f[u] > f[v] for two vertices u and v in a directed graph G, and u and v are in
the same DFS tree in the DFS forest, then u is an ancestor of v in the depth first tree.
(Source http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2007-sol.pdf)
A
True
B
False

Discuss it

Question 13
Consider the DAG with Consider V = {1, 2, 3, 4, 5, 6}, shown below. Which of the following is NOT a
topological ordering?
A
1 2 3 4 5 6
B
1 3 2 4 5 6
C
1 3 2 4 6 5
D
3 2 4 1 6 5

Discuss it

Question 14
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time
on Depth First Search of G? Assume that the graph is represented using adjacency matrix.
A
O(n)
B
O(m+n)
C
O(n
2
)
D
O(mn)

Discuss it

Question 15
Consider the tree arcs of a BFS traversal from a source node W in an unweighted, connected,
undirected graph. The tree T formed by the tree arcs is a data structure for computing.
A
the shortest path between every pair of vertices.
B
the shortest path from W to every vertex in the graph.
C
the shortest paths from W to only those nodes that are leaves
of T.
D
the longest path in the graph

Discuss it

Question 16
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume
that a recursive call to visit a vertex is made only after first checking that the vertex has not been
visited earlier. Then the maximum possible recursion depth (including the initial call) is _________.

A
17
B
18
C
19
D
20

Discuss it

Question 17
Let T be a depth first search tree in an undirected graph G. Vertices u and n are leaves of this tree T.
The degrees of both u and n in G are at least 2. which one of the following statements is true?
A
There must exist a vertex w adjacent to both u and n in G
B
There must exist a vertex w whose removal disconnects u and
n in G
C
There must exist a cycle in G containing u and n
D
There must exist a cycle in G containing u and all its
neighbours in G.

http://geeksquiz.com/algorithms/graph-shortest-paths/

Graph Shortest Paths
Question 1
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.

Graph
A
SDT
B
SBDT
C
SACDT
D
SACET

Discuss it

Question 2
To implement Dijkstras shortest path algorithm on unweighted graphs so that it runs in linear time,
the data structure to be used is:
A
Queue
B
Stack
C
Heap
D
B-Tree

Discuss it

Question 3
Dijkstras single source shortest path algorithm when run from vertex a in the below graph, computes
the correct shortest path distance to

A
only vertex a
B
only vertices a, e, f, g, h
C
only vertices a, b, c, d
D
all the vertices

Discuss it

Question 4
In an unweighted, undirected connected graph, the shortest path from a node S to every other node is
computed most efficiently, in terms of time complexity by
A
Dijkstras algorithm starting from S.
B
Warshalls algorithm
C
Performing a DFS starting from S.
D
Performing a BFS starting from S.

Discuss it

Question 5
Suppose we run Dijkstras single source shortest-path algorithm on the following edge weighted
directed graph with vertex P as the source. In what order do the nodes get included into the set of
vertices for which the shortest path distances are finalized? (GATE CS 2004)

A
P, Q, R, S, T, U
B
P, Q, R, U, S, T
C
P, Q, R, U, T, S
D
P, Q, T, R, U, S

Discuss it

Question 6
Suppose we run Dijkstras single source shortest-path algorithm on the following edge weighted
directed graph with vertex P as the source. In what order do the nodes get included into the set of
vertices for which the shortest path distances are finalized? (GATE CS 2004)

A
P, Q, R, S, T, U
B
P, Q, R, U, S, T
C
P, Q, R, U, T, S
D
P, Q, T, R, U, S

Discuss it

Question 7
What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete
graph of n vertices? (A) (B) (C) (D)
A
A
B
B
C
C
D
D

Discuss it

Question 8
In a weighted graph, assume that the shortest path from a source 's' to a destination 't' is correctly
calculated using a shortest path algorithm. Is the following statement true? If we increase weight of
every edge by 1, the shortest path always remains same.
A
Yes
B
No

Discuss it

Question 9
Following statement is true or false?
If we make following changes to Dijkstra, then it can be used to
find
the longest simple path, assume that the graph is acyclic.

1) Initialize all distances as minus infinite instead of plus
infinite.

2) Modify the relax condition in Dijkstra's algorithm to update
distance
of an adjacent v of the currently considered vertex u only
if "dist[u]+graph[u][v] > dist[v]". In shortest path algo,
the sign is opposite.
A
True
B
False

Discuss it

Question 10
Which of the following algorithm can be used to efficiently calculate single source shortest paths in a
Directed Acyclic Graph?
A
Dijkstra
B
Bellman-Ford
C
Topological Sort
D
Strongly Connected Component

Discuss it

Question 11
Given a directed graph where weight of every edge is same, we can efficiently find shortest path from
a given source to destination using?
A
B
Dijkstra's Shortest Path Algorithm
C
Neither Breadth First Traversal nor Dijkstra's algorithm can be
used
D
Depth First Search

Discuss it

Question 12
Is the following statement valid about shortest paths? Given a graph, suppose we have calculated
shortest path from a source to all other vertices. If we modify the graph such that weights of all edges
is becomes double of the original weight, then the shortest path remains same only the total weight of
path changes.
A
True
B
False

Discuss it

Question 13
Match the following

Group A Group B
a) Dijkstra's single shortest path algo p) Dynamic
Programming
b) Bellmen Ford's single shortest path algo q)
Backtracking
c) Floyd Warshell's all pair shortest path algo. r) Greedy
Algorithm
A
a-r, b-q, c-p
B
a-p, b-p, c-p
C
a-r, b-p, c-p
D
a-p, b-r, c-q

Discuss it

Question 14
Is the following statement valid?.

Given a weighted graph where weights of all edges are unique (no two edge have same weights),
there is always a unique shortest path from a source to destination in such a graph.
A
True
B
False

Discuss it

Question 15
Is the following statement valid?.

Given a graph where all edges have positive weights, the shortest paths produced by Dijsktra and
Bellman Ford algorithm may be different but path weight would always be same.
A
True
B
False

Discuss it

Question 16
Which of the following statement(s)is / are correct regarding Bellman-Ford shortest path algorithm?
P: Always finds a negative weighted cycle, if one exist s.
Q: Finds whether any negative weighted cycle is reachable
from the source.
A
P Only
B
Q Only
C
Both P and Q
D
Neither P nor Q

Discuss it

http://geeksquiz.com/algorithms/graph-minimum-spanning-tree/

Graph Minimum Spanning Tree
Question 1
An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,.vn. Two nodes vi , vj are
connected if and only if 0 < |i j| <= 2. Each edge (vi, vj ) is assigned a weight i + j. A sample graph
with n = 4 is shown below. What will be the cost of the minimum spanning tree (MST) of such a graph
with n nodes? (GATE CS 2011)
A
1/12(11n^2 5n)
B
n^2 n + 1
C
6n 11
D
2n + 1

Discuss it

Question 2
The length of the path from v5 to v6 in the MST of previous question with n = 10 is
A
11
B
25
C
31
D
41

Discuss it

Question 3
Consider a complete undirected graph with vertex set {0, 1, 2, 3, 4}. Entry Wij in the matrix W below is
the weight of the edge {i, j}. What is the minimum possible weight of a spanning tree T in this graph
such that vertex 0 is a leaf node in the tree T? (GATE CS 2010)
A
7
B
8
C
9
D
10

Discuss it

Question 4
In the graph given in above question question, what is the minimum possible weight of a path P from
vertex 1 to vertex 2 in this graph such that P contains at most 3 edges?
A
7
B
8
C
9
D
10

Discuss it

Question 5
An undirected graph G has n nodes. Its adjacency matrix is given by an n n square matrix whose (i)
diagonal elements are 0s and (ii) non-diagonal elements are 1s. which one of the following is TRUE?
A
Graph G has no minimum spanning tree (MST)
B
Graph G has a unique MST of cost n-1
C
Graph G has multiple distinct MSTs, each of cost n-1
D
Graph G has multiple spanning trees of different costs

Discuss it

Question 6
Consider the following
graph:
Which one of the following cannot be the sequence of edges added, in that order, to a minimum
spanning tree using Kruskals algorithm?
A
(ab),(df),(bf),(dc),(de)
B
(ab),(df),(dc),(bf),(de)
C
(df),(ab),(dc),(bf),(de)
D
(df),(ab),(bf),(de),(dc)

Discuss it

Question 7
Let G be an undirected connected graph with distinct edge weight. Let emax be the edge with
maximum weight and emin the edge with minimum weight. Which of the following statements is false?
(GATE CS 2000)
A
Every minimum spanning tree of G must contain emin
B
If emax is in a minimum spanning tree, then its removal must
disconnect G
C
No minimum spanning tree contains emax
D
G has a unique minimum spanning tree

Discuss it

Question 8
Consider a weighted complete graph G on the vertex set {v1,v2 ,v} such that the weight of the edge
(v,,v) is 2|i-j|. The weight of a minimum spanning tree of G is: (GATE CS 2006)
A
n 1
B
2n 2
C
nC2
D
2

Discuss it

Question 9
Let G be a weighted graph with edge weights greater than one and G'be the graph constructed by
squaring the weights of edges in G. Let T and T' be the minimum spanning trees of G and G',
respectively, with total weights t and t'. Which of the following statements is TRUE?
A
T' = T with total weight t' = t
2

B
T' = T with total weight t' < t
2

C
T' != T but total weight t' = t
2

D
None of the above

Discuss it

Question 10
Consider the following graph: Which one of the
following is NOT the sequence of edges added to the minimum spanning tree using Kruskal's
algorithm?
A
(b,e)(e,f)(a,c)(b,c)(f,g)(c,d)
B
(b,e)(e,f)(a,c)(f,g)(b,c)(c,d)
C
(b,e)(a,c)(e,f)(b,c)(f,g)(c,d)
D
(b,e)(e,f)(b,c)(a,c)(f,g)(c,d)

Discuss it

Question 11
The number of distinct minimum spanning trees for the weighted graph below is
____
A
4
B
5
C
6
D
7

Discuss it

http://www.geeksforgeeks.org/interesting-shortest-path-questions-set-1/

ome interesting shortest path questions | Set 1
Question 1: Given a directed weighted graph. You are also given
the shortest path from a source vertex s to a destination vertex
t. If weight of every edge is increased by 10 units, does the
shortest path remain same in the modified graph?
The shortest path may change. The reason is, there may be different
number of edges in different paths from s to t. For example, let
shortest path be of weight 15 and has 5 edges. Let there be another
path with 2 edges and total weight 25. The weight of the shortest path
is increased by 5*10 and becomes 15 + 50. Weight of the other path
is increased by 2*10 and becomes 25 + 20. So the shortest path
changes to the other path with weight as 45.
Question 2: This is similar to above question. Does the shortest
path change when weights of all edges are multiplied by 10?
If we multiply all edge weights by 10, the shortest path doesnt
change. The reason is simple, weights of all paths from s to t get
multiplied by same amount. The number of edges on a path doesnt
matter. It is like changing unit of weights.
Question 3: Given a directed graph where every edge has weight
as either 1 or 2, find the shortest path from a given source vertex
s to a given destination vertex t. Expected time complexity is
O(V+E).
If we apply Dijkstras shortest path algorithm, we can get a shortest
path in O(E + VLogV) time. How to do it in O(V+E) time? The idea is
to use BFS . One important observation about BFS is, the path used
in BFS always has least number of edges between any two vertices.
So if all edges are of same weight, we can use BFS to find the
shortest path. For this problem, we can modify the graph and split all
edges of weight 2 into two edges of weight 1 each. In the modified
graph, we can use BFS to find the shortest path. How is this approach
O(V+E)? In worst case, all edges are of weight 2 and we need to do
O(E) operations to split all edges, so the time complexity becomes
O(E) + O(V+E) which is O(V+E).
Question 4: Given a directed acyclic weighted graph, how to find
the shortest path from a source s to a destination t in O(V+E)
time?
See: Shortest Path in Directed Acyclic Graph
More Questions See following links for more questions.
http://algs4.cs.princeton.edu/44sp/
http://geeksquiz.com/graph-shortest-paths/

1 A subnet has been assigned a subnet mask of 255.255.255.192.
What is the maximum number of hosts that can belong to this
subnet?
A) 14
B) 30
C) 62
D) 126

2 Which one of the following is NOT shared by the threads of the
same process ?
A) Stack
C) File Descriptor Table
D) Message Queue

3 Let G be a weighted undirected graph and e be an edge with
maximum weight in G. Suppose there is a minimum weight spanning
tree in G containing the edge e. Which of the following statements is
always TRUE?
A) There exists a cutset in G having all edges of maximum weight.
B) There exists a cycle in G having all edges of maximum weight.
C) Edge e cannot be contained in a cycle.
D) All edges in G have the same weight.

4 A software organization has been assessed at SEI CMM Level 4.
Which of the following does the organization need to practice beside
Process Change Management and Technology Change Management in
order to achieve Level 5 ?
A) Defect Detection
B) Defect Prevention
C) Defect Isolation
D) Defect Propagation

5 Suppose that two parties A and B wish to setup a common secret
key (D-H key) between themselves using the Diffle-Hellman key
exchange technique. They agree on 7 as the modulus and 3 as the
primitive root. Party A chooses 2 and party B chooses 5 as their
respective secrets. Their D-H key is
A) 3
B) 4
C) 5
D) 6

6 Consider the following message M = 1010001101. The cyclic
redundancy check (CRC) for this message using the divisor
polynomial x5 + x4 + x2 + 1 is :
A) 01110
B) 01011
C) 10101
D) 10110

7 Which of the following statements is FALSE regarding a bridge
A) Bridge is a layer 2 device
B) Bridge reduces collision domain
C) Bridge is used to connect two or more LAN segments

8 What is the availability of a software with the following reliability
figures?
Mean Time Between Failure (MTBF) = 25 days
Mean Time To Repair (MTTR) = 6 hours
A) 1%
B) 24%
C) 99%
D) 99.009%

9 A channel has a bit rate of 4 kbps and one-way propagation delay
of 20 ms. The channel uses stop and wait protocol. The transmission
time of the acknowledgement frame is negligible. To get a channel
efficiency of at least 50%, the minimum frame size should be
A) 80 bytes
B) 80 bits
C) 160 bytes
D) 160 bits

10 In a depth-first traversal of a graph G with n vertices, k edges are
marked as tree edges. The number of connected components in G is
A) k
B) k+1
C) n-k-l
D) n-k

11 Which of the following statements is TRUE about CSMA/CD
A) IEEE 802.11 wireless LAN runs CSMA/CD protocol
B) Ethernet is not based on CSMA/CD protocol
C) CSMA/CD is not suitable for a high propagation delay network like satellite
network
D) There is no contention in a CSMA/CD network

12 Which one of the following regular expressions is NOT equivalent
to the regular expression (a + b + c)* ?
A) (a* + b* + c*)*
B) (a*b*c*)*
C) ((ab)* + c*)*
D) (a*b* + c*)*

http://www.geeksforgeeks.org/find-number-of-islands/

Given a boolean 2D matrix, find the number of islands.
This is an variation of the standard problem: Counting number of connected components in a
undirected graph.
Before we go to the problem, let us understand what is a connected component. A connected
component of an undirected graph is a subgraph in which every two vertices are connected to each
other by a path(s), and which is connected to no other vertices outside the subgraph.
For example, the graph shown below has three connected components.

A graph where all vertices are connected with each other, has exactly one connected component,
consisting of the whole graph. Such graph with only one connected component is called as Strongly
Connected Graph.
The problem can be easily solved by applying DFS() on each component. In each DFS() call, a
component or a sub-graph is visited. We will call DFS on the next un-visited component. The number
of calls to DFS() gives the number of connected components. BFS can also be used.
What is an island?
A group of connected 1s forms an island. For example, the below matrix contains 5 islands
{1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}
A cell in 2D matrix can be connected to 8 neighbors. So, unlike standard DFS(), where we recursively
call for all adjacent vertices, here we can recursive call for 8 neighbors only. We keep track of the
visited 1s so that they are not visited again.
// Program to count islands in boolean 2D matrix

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define ROW 5
#define COL 5

// A function to check if a given cell (row, col) can be included in DFS
int isSafe(int M[][COL], int row, int col, bool visited[][COL])
{
return (row >= 0) && (row < ROW) && // row number is in range
(col >= 0) && (col < COL) && // column number is in range
(M[row][col] && !visited[row][col]); // value is 1 and not
yet visited
}

// A utility function to do DFS for a 2D boolean matrix. It only
considers
// the 8 neighbors as adjacent vertices
void DFS(int M[][COL], int row, int col, bool visited[][COL])
{
// These arrays are used to get row and column numbers of 8
neighbors
// of a given cell
static int rowNbr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
static int colNbr[] = {-1, 0, 1, -1, 1, -1, 0, 1};

// Mark this cell as visited
visited[row][col] = true;

// Recur for all connected neighbours
for (int k = 0; k < 8; ++k)
if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited) )
DFS(M, row + rowNbr[k], col + colNbr[k], visited);
}

// The main function that returns count of islands in a given boolean
// 2D matrix
int countIslands(int M[][COL])
{
// Make a bool array to mark visited cells.
// Initially all cells are unvisited
bool visited[ROW][COL];
memset(visited, 0, sizeof(visited));

// Initialize count as 0 and travese through the all cells of
// given matrix
int count = 0;
for (int i = 0; i < ROW; ++i)
for (int j = 0; j < COL; ++j)
if (M[i][j] && !visited[i][j]) // If a cell with value 1 is
not
{ // visited yet, then new
island found
DFS(M, i, j, visited); // Visit all cells in this
island.
++count; // and increment island count
}

return count;
}

// Driver program to test above function
int main()
{
int M[][COL]= { {1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}
};

printf("Number of islands is: %d\n", countIslands(M));

return 0;
}
Output:
Number of islands is: 5
Time complexity: O(ROW x COL)
Reference:
http://en.wikipedia.org/wiki/Connected_component_%28graph_theory%29
topic discussed above.
http://girdhargopalbansal.blogspot.in/2013/05/gate-questions-for-data-structures-and.html

GATE Questions for Data Structures and
Algorithms
Following questions have been asked in GATE CS exam

1. Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder,
inorder and preorder traversal. Respectively, of a complete binary tree. Which of the
following is always true? (GATE CS 2000)
(a) LASTIN = LASTPOST
(b) LASTIN = LASTPRE
(c) LASTPRE = LASTPOST
(d) None of the above
It is given that the given tree is complete binary tree. For a complete binary tree, the last visited node
will always be same for inorder and preorder traversal. None of the above is true even for a complete
binary tree.
The option (a) is incorrect because the last node visited in Inorder traversal is right child and last node
visited in Postorder traversal is root.
The option (c) is incorrect because the last node visited in Preorder traversal is right child and last
node visited in Postorder traversal is root.
For option (b), see the following counter example.
1
/ \
2 3
/ \ /
4 5 6

Inorder traversal is 4 2 5 1 6 3
Preorder traversal is 1 2 4 5 3 6

2. The most appropriate matching for the following pairs
X: depth first search 1: heap
Z: sorting 3: stack
is (GATE CS 2000):
(a) X1 Y2 Z-3
(b) X3 Y1 Z-2
(c) X3 Y2 Z-1
(d) X2 Y3 Z-1
Stack is used for Depth first Search
Queue is used for Breadth First Search
Heap is used for sorting

3. Consider the following nested representation of binary trees: (X Y Z) indicates Y and
Z are the left and right sub stress, respectively, of node X. Note that Y and Z may be
NULL, or further nested. Which of the following represents a valid binary tree?
(a) (1 2 (4 5 6 7))
(b) (1 (2 3 4) 5 6) 7)
(c) (1 (2 3 4)(5 6 7))
(d) (1 (2 3 NULL) (4 5))

4. Let s be a sorted array of n integers. Let t(n) denote the time taken for the most
efficient algorithm to determined if there are two elements with sum less than 1000 in
s. which of the following statements is true? (GATE CS 2000)
a) t (n) is 0 (1)
b) n < t (n) < n
c) n log 2 n < t (n) <
d) t (n) =
Let array be sorted in ascending order, if sum of first two elements is less than 1000 then there are
two elements with sum less than 1000 otherwise not. For array sorted in descending order we need to
check last two elements. For an array data structure, number of operations are fixed in both the cases
and not dependent on n, complexity is O(1)

5. B+ trees are preferred to binary trees in databases because (GATE CS 2000)
(a) Disk capacities are greater than memory capacities
(b) Disk access is much slower than memory access
(c) Disk data transfer rates are much less than memory data transfer rates
(d) Disks are more reliable than memory
Disk access is slow and B+ Tree provide search in less number of disk hits. This is primarily because
unlike binary seach trees, B+ trees have very high fanout (typically on the order of 100 or more),
which reduces the number of I/O operations required to find an element in the tree.

1. Consider the function f defined below.
struct item
{
int data;
struct item * next;
};
int f(struct item *p)
{
return (
(p == NULL) ||
(p->next == NULL) ||
(( P->data &lt;= p->next->data) &amp;&amp; f(p->next))
);
}
For a given linked list p, the function f returns 1 if and only if (GATE CS 2003)
a) the list is empty or has exactly one element
b) the elements in the list are sorted in non-decreasing order of data value
c) the elements in the list are sorted in non-increasing order of data value
d) not all elements in the list have the same data value.
The function f() works as follows
1) If linked list is empty return 1
2) Else If linked list has only one element return 1
3) Else if node->data is smaller than equal to node->next->data and same thing holds for rest of the
list then return 1
4) Else return 0

2. Consider the label sequences obtained by the following pairs of traversals on a
labeled binary tree. Which of these pairs identify a tree uniquely (GATE CS 2004)?
i) preorder and postorder
ii) inorder and postorder
iii) preorder and inorder
iv) level order and postorder
a) (i) only
b) (ii), (iii)
c) (iii) only
d) (iv) only

3. The following numbers are inserted into an empty binary search tree in the given
order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the
maximum distance of a leaf node from the root)? (GATE CS 2004)
a) 2
b) 3
c) 4
d) 6
Constructed binary search tree will be..
10
/ \
1 15
\ / \
3 12 16
\
5

4. A data structure is required for storing a set of integers such that each of the
following operations can be done in (log n) time, where n is the number of elements in
the set.
o Delection of the smallest element
o Insertion of an element if it is not already present in the set
Which of the following data structures can be used for this purpose?
(a) A heap can be used but not a balanced binary search tree
(b) A balanced binary search tree can be used but not a heap
(c) Both balanced binary search tree and heap can be used
(d) Neither balanced binary search tree nor heap can be used
A self-balancing balancing binary search tree containing n items allows the lookup, insertion, and
removal of an item in O(log n) worst-case time. Since its a BST, we can easily find out minimum
element in O(nlogn).
Since Heap is a balanced binary tree (or almost complete binary tree), insertion complexity for heap is
O(logn). Also complexity to get minimum in a min heap is O(logn) because removal of root node
causes a call to heapify (after removing the first element from the array) to maintain the heap tree
property. But a heap cannot be used for the above purpose as the question says insert an element if
it is not already present. For a heap, we cannot find out in O(logn) if an element is present or not.
Thanks to game for providing the correct solution.

5. A circularly linked list is used to represent a Queue. A single variable p is used to
access the Queue. To which node should p point such that both the operations enQueue
and deQueue can be performed in constant time? (GATE 2004)

a) rear node
b) front node
c) not possible with a single pointer
d) node next to front
Answer is not (b) front node, as we can not get rear from front in O(1), but if p is rear we can
implement both enQueue and deQueue in O(1) because from rear we can get front in O(1). Below are
sample functions. Note that these functions are just sample are not working. Code to handle base
cases is missing.
/* p is pointer to address of rear (double pointer). This function adds
new
node after rear and updates rear which is *p to point to new node */
void enQueue(struct node **p, struct node *new_node)
{
/* Missing code to handle base cases like *p is NULL */

new_node->next = (*p)->next;
(*p)->next = new_node;
(*p) = new_node /* new is now rear */
/* Note that p is again front and p->next is rear */
}
/* p is pointer to rear. This function removes the front element and
returns the new front */
struct node *deQueue(struct node *p)
{
/* Missing code to handle base cases like p is NULL,
p->next is NULL,... etc */
struct node *temp = p->next->next;
p->next = p->next->next;
return temp;
/* Note that p is again front and p->next is rear */
}

1. Consider the following C program segment
struct CellNode
{
struct CelINode *leftchild;
int element;
struct CelINode *rightChild;
}
int Dosomething(struct CelINode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if (ptr->leftChild != NULL)
value = 1 + DoSomething(ptr->leftChild);
if (ptr->rightChild != NULL)
value = max(value, 1 + DoSomething(ptr->rightChild));
}
return (value);
}
The value returned by the function DoSomething when a pointer to the root of a non-
empty tree is passed as argument is (GATE CS 2004)
a) The number of leaf nodes in the tree
b) The number of nodes in the tree
c) The number of internal nodes in the tree
d) The height of the tree
Explanation: DoSomething() returns max(height of left child + 1, height of left child + 1). So given that
pointer to root of tree is passed to DoSomething(), it will return height of the tree. Note that this
implementation follows the convention where height of a single node is 0.
2. Suppose we run Dijkstras single source shortest-path algorithm on the following
edge weighted directed graph with vertex P as the source. In what order do the nodes
get included into the set of vertices for which the shortest path distances are finalized?
(GATE CS 2004)
a) P, Q, R, S, T, U
b) P, Q, R, U, S, T
c) P, Q, R, U, T, S
d) P, Q, T, R, U, S

3. Suppose each set is represented as a linked list with elements in arbitrary order.
Which of the operations among union, intersection, membership, cardinality will be the
slowest? (GATE CS 2004)
a) union only
b) intersection, membership
c) membership, cardinality
d) union, intersection
Cardinality and membership are definably not the slowest one. For cardinality, just count the number
of nodes in a list. For membership, just traverse the list and look for a match
For getting intersection of L1 and L2, search for each element of L1 in L2 and print the elements we
find in L2.
There can be many ways for getting union of L1 and L2. One of them is as follows
a) Print all the nodes of L1 and print only those which are not present in L2.
b) Print nodes of L2.
All of these methods will require more operations than intersection as we have to process intersection
node plus other nodes.

4. The time complexity of the following C function is (assume n > 0 (GATE CS 2004)
int recursive (mt n)
{
if (n == 1)
return (1);
else
return (recursive (n-1) + recursive (n-1));
}
a) 0(n)
b) 0(nlogn)
c) 0(n^2)
d) 0(2^n)
Explanation:
Recursive expression for the above program will be.
T(n) = 2T(n-1) + c
T(1) = c1.
Let us solve it.
T(n) = 2(2T(n-2) + c) + c = 4T(n-2) + 3c
T(n) = 8T(n-3) + 6c + c = 8T(n-3) + 7c
T(n) = 16T(n-4) + 14c + c = 16T(n-4) + 15c
............................................................
.............................................................
T(n) = (2^(n-1))T(1) + (2^(n-1) - 1)c

T(n) = O(2^n)

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
Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. 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) queue
b) stack
c) tree
d) list
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 traversal
b) in-order traversal
c) depth first search

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 value
ii. 1471, 6171 has to the same value
iii. All elements hash to the same value
iv. Each element hashes to a different value
(GATE CS 2004)
a) i only
b) ii only
c) i and ii only
d) iii or iv

5. Postorder traversal of a given binary search tree, T produces the following sequence
of keys
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
Which 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, 95
b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Inorder traversal of a BST always gives elements in increasing order. Among all four options, a) is the
only increasing order sequence.

1. Consider the following C function.

float f(float x, int y)
{
float p, s; int i;
for (s=1, p=1, i=1; i < y; i ++)
{
p*= x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best approximates (GATE CS
2003)
a) x^y
b) e^x
c) ln(1 + x)
d) x^x
The function f() is implementation of Taylors Series to calculates e^x
e^x = 1 + x + x^2/2! + x^3/3! + ---
More is the value of y more precise value of e^x will be returned by f()
References:
http://en.wikipedia.org/wiki/E_%28mathematical_constant%29

2. In the worst case, the number of comparisons needed to search a singly linked list of
length n for a given element is (GATE CS 2002)
a) log 2 n
b) n/2
c) log 2 n 1
d) n
In the worst case, the element to be searched has to be compared with all elements of linked list.
3. The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a
Max Heap. The resultant Max Heap is.

4. Consider the following three claims
I (n + k)^m = (n^m), where k and m are constants
II 2^(n + 1) = 0(2^n)
III 2^(2n + 1) = 0(2^n)
Which of these claims are correct? (GATE CS 2003)
(a) I and II
(b) I and III
(c) II and III
(d) I, II and III
(I) (n+m)^k = n^k + c1*n^(k-1) + ... k^m = (n^k)
(II) 2^(n+1) = 2*2^n = O(2^n)

5. A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow
from opposite ends of the array. Variables top1 and top2 (topl< top 2) point to the
location of the topmost element in each of the stacks. If the space is to be used
efficiently, the condition for stack full is (GATE CS 2004)
a) (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1)
b) top1 + top2 = MAXSIZE
c) (top1= MAXSIZE/2) or (top2 = MAXSIZE)
d) top1= top2 -1
If we are to use space efficiently then size of the any stack can be more than MAXSIZE/2.
Both stacks will grow from both ends and if any of the stack top reaches near to the other top then
stacks are full. So the condition will be top1 = top2 -1 (given that top1 < top2)

1. The usual (n^2) implementation of Insertion Sort to sort an array uses linear search
to identify the position where an element is to be inserted into the already sorted part of
the array. If, instead, we use binary search to identify the position, the worst case
running time will (GATE CS 2003)
(a) remain (n^2)
(b) become (n(log n)^2)
(c) become (n log n)
(d) become (n)
If we use binary search then there will be comparisons in the worst case, which is (n log
n) ( If you want to know how can be equal to (n log n), then see this for proof). But the
algorithm as a whole will still have a running time of (n^2) on average because of the series of swaps
required for each insertion.
Reference:
http://en.wikipedia.org/wiki/Insertion_sort

2. The tightest lower bound on the number of comparisons, in the worst case, for
comparison-based sorting is of the order of
a) n
b) n^2
c) nlogn
d) n(log^2)n
The number of comparisons that a comparison sort algorithm requires increases in proportion to
nlog(n), where n is the number of elements to sort. This bound is asymptotically tight:
Given a list of distinct numbers (we can assume this because this is a worst-case analysis), there are n
factorial permutations exactly one of which is the list in sorted order. The sort algorithm must gain
enough information from the comparisons to identify the correct permutations. If the algorithm
always completes after at most f(n) steps, it cannot distinguish more than 2^f(n) cases because the
keys are distinct and each comparison has only two possible outcomes. Therefore,
2^f(n) n!, or equivalently f(n) [Tex]\log_2[/Tex](n!).
References:
http://en.wikipedia.org/wiki/Comparison_sort

3. The problem 3-SAT and 2-SAT are
a) both in P
b) both NP complete
c) NP-complete and in P respectively
d) undecidable and NP-complete respectively
The Boolean satisfiability problem (SAT) is a decision problem, whose instance is a Boolean
expression written using only AND, OR, NOT, variables, and parentheses. The problem is: given the
expression, is there some assignment of TRUE and FALSE values to the variables that will make the
entire expression true? A formula of propositional logic is said to be satisfiable if logical values can be
assigned to its variables in a way that makes the formula true.
3-SAT and 2-SAT are special cases of k-satisfiability (k-SAT) or simply satisfiability (SAT), when each
clause contains exactly k = 3 and k = 2 literals respectively.
2-SAT is P while 3-SAT is NP Complete. (See this for explanation)
References:
http://en.wikipedia.org/wiki/Boolean_satisfiability_problem

4. Consider the following graph

Among the following sequences
I) a b e g h f
II) a b f e h g
III) a b f h g e
IV) a f g h b e
Which are depth first traversals of the above graph? (GATE CS 2003)
a) I, II and IV only
b) I and IV only
c) II, III and IV only
d) I, III and IV only

1. In a binary max heap containing n numbers, the smallest element can be found in
time (GATE CS 2006)
(A) 0(n)
(B) O(logn)
(C) 0(loglogn)
(D) 0(1)
In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf
nodes for the minimum value. Worst case complexity will be O(n)
12
/ \
/ \
8 7
/ \ / \
/ \ / \
2 3 4 5
2. A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1
instead of 0. the root is stored at X[1]. For a node stored at X[i], the left child, if any, is
stored in X[2i] and the right child, if any, in X[2i+1]. To be able to store any binary tree
on n vertices the minimum size of X should be. (GATE CS 2006)
(A) log2n
(B) n
(C) 2n + 1
(D) 2^n 1
For a right skewed binary tree, number of nodes will be 2^n 1. For example, in below binary tree,
node A will be stored at index 1, B at index 3, C at index 7 and D at index 15.
A
\
\
B
\
\
C
\
\
D
3. Which one of the following in place sorting algorithms needs the minimum number
of swaps? (GATE CS 2006)
(A) Quick sort
(B) Insertion sort
(C) Selection sort
(D) Heap sort
For selection sort, number of swaps required is minimum ( (n) ).
4. An element in an array X is called a leader if it is greater than all elements to the right
of it in X. The best algorithm to find all leaders in an array (GATE CS 2006)
(A) Solves it in linear time using a left to right pass of the array
(B) Solves it in linear time using a right to left pass of the array
(C) Solves it using divide and conquer in time 8(nlogn)
(D) Solves it in time 8(n2)
Please see this post for explanation.
5. Consider a weighted complete graph G on the vertex set {v1,v2 ,v} such that the
weight of the edge (v,,v) is 2|i-j|. The weight of a minimum spanning tree of G is: (GATE
CS 2006)
(A) n 1
(B) 2n 2
(C) nC2
(D) 2
Minimum spanning tree of such a graph is
v1
\
v2
\
v3
\
v4
.
.
.
vn

Weight of the minimum spanning tree
= 2|2 1| + 2|3 2| + 2|4 3| + 2|5 4| . + 2| n (n-1) |
= 2n 2

1. Consider the following functions

Which of the following is true? (GATE CS 2000)
(a) h(n) is 0(f(n))
(b) h(n) is 0(g(n))
(c) g(n) is not 0(f(n))
(d) f(n) is 0(g(n))
g(n) = 2^ = n^
f(n) and g(n) are of same asymptotic order and following statements are true.
f(n) = O(g(n))
g(n) = O(f(n)).
(a) and (b) are false because n! is of asymptotically higher order than n^ .

2. Let G be an undirected connected graph with distinct edge weight. Let emax be the
edge with maximum weight and emin the edge with minimum weight. Which of the
following statements is false? (GATE CS 2000)
(a) Every minimum spanning tree of G must contain emin
(b) If emax is in a minimum spanning tree, then its removal must disconnect G
(c) No minimum spanning tree contains emax
(d) G has a unique minimum spanning tree
(a) and (b) are always true.
(c) is false because (b) is true.
(d) is true because all edge weights are distinct for G.

3. Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the
resulting depth-first search tree. Let u be a vertex in G and let v be the first new
(unvisited) vertex visited after visiting u in the traversal. Which of the following
statements is always true? (GATE CS 2000)
(a) {u,v} must be an edge in G, and u is a descendant of v in T
(b) {u,v} must be an edge in G, and v is a descendant of u in T
(c) If {u,v} is not an edge in G then u is a leaf in T
(d) If {u,v} is not an edge in G then u and v must have the same parent in T

4. Consider an undirected unweighted graph G. Let a breadth-first traversal of G be
done starting from a node r. Let d(r, u) and d(r, v) be the lengths of the shortest paths
from r to u and v respectively, in G. lf u is visited before v during the breadth-first
traversal, which of the following statements is correct? (GATE CS 2001)
a) d(r, u) < d (r, v)
b) d(r, u) > d(r, v)
c) d(r, u) <= d (r, v)
d) None of the above
d(r, u) and d(r, v) will be equal when u and v are at same level, otherwise d(r, u) will be less than d(r,
v)

5. How many undirected graphs (not necessarily connected) can be constructed out of a
given set V= {V 1, V 2,V n} of n vertices ? (GATE CS 2001)
a) n(n-l)/2
b) 2^n
c) n!
d) 2^(n(n-1)/2)
In an undirected graph, there can be maximum n(n-1)/2 edges. We can choose to have (or not have)
any of the n(n-1)/2 edges. So, total number of undirected graphs with n vertices is 2^(n(n-1)/2).

1 In a heap with n elements with the smallest element at the root, the 7th smallest
element can be found in time (GATE CS 2003)
a) (n log n)
b) (n)
c) (log n)
d) (1)
Given a Min-heap, to get the 7th smallest element, we need to call Extract-Min 7 times which means
(7logn)( = (logn)) operations

2. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an
initially empty binary search tree. The binary search tree uses the usual ordering on
natural numbers. What is the in-order traversal sequence of the resultant tree? (GATE
CS 2003)
a) 7 5 1 0 3 2 4 6 8 9
b) 0 2 4 3 1 6 5 9 8 7
c) 0 1 2 3 4 5 6 7 8 9
d) 9 8 6 4 2 3 0 1 5 7
In-order traversal of a BST gives elements in increasing order. So answer c is correct without any
doubt.

3. Let S be a stack of size n >= 1. Starting with the empty stack, suppose we push the first
n natural numbers in sequence, and then perform n pop operations. Assume that Push
and Pop operation take X seconds each, and Y seconds elapse between the end of one
such stack operation and the start of the next operation. For m >= 1, define the stack-
life of m as the time elapsed from the end of Push(m) to the start of the pop operation
that removes m from S. The average stack-life of an element of this stack is (GATE CS
2003)
a) n(X+ Y)
b) 3Y + 2X
c) n(X + Y)-X
d) Y + 2X
We can easily arrive at the result by taking few examples.

1. The height of a binary tree is the maximum number of edges in any root to leaf path.
The maximum number of nodes in a binary tree of height h is:
(A) 2^h -1
(B) 2^(h-1) 1
(C) 2^(h+1) -1
(D) 2*(h+1)
Maximum number of nodes will be there for a complete tree.
Number of nodes in a complete tree of height h = 1 + 2 + 2^2 + 2*3 + . 2^h = 2^(h+1) 1
2: The maximum number of binary trees that can be formed with three unlabeled nodes
is:
(A) 1
(B) 5
(C) 4
(D) 3
O
/ \
O O
(i)

O
/
O
/
O
(ii)

O
/
O
\
O
(iii)

O
\
O
\
O
(iv)

O
\
O
/
O
(v)
Note that nodes are unlabeled. If the nodes are labeled, we get more number of trees.
3. Which of the following sorting algorithms has the lowest worst-case complexity?
(A) Merge sort
(B) Bubble sort
(C) Quick sort
(D) Selection sort
Worst case complexities for the above sorting algorithms are as follows:
Merge Sort nLogn
Bubble Sort n^2
Quick Sort n^2
Selection Sort n^2
4. The following postfix expression with single digit operands is evaluated using a
stack:
8 2 3 ^ / 2 3 * + 5 1 * -
Note that ^ is the exponentiation operator. The top two elements of the stack after the
first * is evaluated are:
(A) 6, 1
(B) 5, 7
(C) 3, 2
(D) 1, 5
The algorithm for evaluating any postfix expression is fairly straightforward:
1. While there are input tokens left
o Read the next token from input.
o If the token is a value
+ Push it onto the stack.
o Otherwise, the token is an operator
(operator here includes both operators, and functions).
* It is known a priori that the operator takes n arguments.
* If there are fewer than n values on the stack
(Error) The user has not input sufficient values in the expression.
* Else, Pop the top n values from the stack.
* Evaluate the operator, with the values as arguments.
* Push the returned results, if any, back onto the stack.
2. If there is only one value in the stack
o That value is the result of the calculation.
3. If there are more values in the stack
o (Error) The user input has too many values.
Source for
algorithm: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_postfix_algorithm
Let us run the above algorithm for the given expression.
First three tokens are values, so they are simply pushed. After pushing 8, 2 and 3, the stack is as
follows
8, 2, 3
When ^ is read, top two are popped and power(2^3) is calculated
8, 8
When / is read, top two are popped and division(8/8) is performed
1
Next two tokens are values, so they are simply pushed. After pushing 2 and 3, the stack is as follows
1, 2, 3
When * comes, top two are popped and multiplication is performed.
1, 6

5. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g,
respectively. The postorder traversal of the binary tree is:
(A) d e b f g c a
(B) e d b g f c a
(C) e d b f g c a
(D) d e f g b c a
Below is the given tree.
a
/ \
/ \
b c
/ \ / \
/ \ / \
d e f g

1. Consider a hash table of size seven, with starting index
zero, and a hash function (3x + 4)mod7. Assuming the hash table is
initially empty, which of the following is the contents of the table
when the sequence 1, 3, 8, 10 is inserted into the table using closed
hashing? Note that _ denotes an empty location in the table.

(A) 8, _, _, _, _, _, 10

(B) 1, 8, 10, _, _, _, 3

(C) 1, _, _, _, _, _,3

(D) 1, 10, 8, _, _, _, 3
Please see http://lcm.csa.iisc.ernet.in/dsa/node38.html for closed hashing and probing.
Let us put values 1, 3, 8, 10 in the hash of size 7.
Initially, hash table is empty
- - - - - - - 0 1 2 3 4 5 6 The value of function (3x + 4)mod 7 for 1 is 0, so let us put the value at 0
1 - - - - - - 0 1 2 3 4 5 6 The value of function (3x + 4)mod 7 for 3 is 6, so let us put the value at 6
1 - - - - - 3 0 1 2 3 4 5 6 The value of function (3x + 4)mod 7 for 8 is 0, but 0 is already occupied, let us
put the value(8) at next available space(1)
1 8 - - - - 3 0 1 2 3 4 5 6 The value of function (3x + 4)mod 7 for 10 is 6, but 6 is already occupied, let us
put the value(10) at next available space(2)
1 8 10 - - - 3 0 1 2 3 4 5 6

2. In an unweighted, undirected connected graph, the shortest path from a node S to
every other node is computed most efficiently, in terms of time complexity by
(A) Dijkstras algorithm starting from S.
(B) Warshalls algorithm
(C) Performing a DFS starting from S.
(D) Performing a BFS starting from S.
* Time Comlexity of the Dijkstras algorithm is O(|V|^2 + E) * Time Comlexity of the Warshalls
algorithm is O(|V|^3) * DFS cannot be used for finding shortest paths * BFS can be used for
unweighted graphs. Time Complexity for BFS is O(|E| + |V|)

3. A complete n-ary tree is a tree in which each node has n children or no children. Let I
be the number of internal nodes and L be the number of leaves in a complete n-ary tree.
If L = 41, and I = 10, what is the value of n?
(A) 3
(B) 4
(C) 5
(D) 6
For an n-ary tree where each node has n children or no children, following relation holds
L = (n-1)*I + 1 Where L is the number of leaf nodes and I is the number of internal nodes.
Let us find out the value of n for the given data.
L = 41 , I = 10 41 = 10*(n-1) + 1 (n-1) = 4 n = 5

4. In the following C function, let n >= m.
int gcd(n,m) { if (n%m ==0) return m; n = n%m; return gcd(m,n); } How many recursive calls
(A) (logn)?
(B) (n)
(C) (loglogn)
(D) (sqrt(n))
Above code is implementation of the Euclidean algorithm for finding Greatest Common Divisor
(GCD).
Please see http://mathworld.wolfram.com/EuclideanAlgorithm.html for time complexity.

5. What is the time complexity of the following recursive function:
int DoSomething (int n) { if (n <= 2) return 1; else return (DoSomething (floor(sqrt(n))) + n); } (A)
(n)
(B) (nlogn)
(C) (logn)
(D) (loglogn)
Recursive relation for the DoSomething() is
T(n) = T( ) + C1 if n > 2 We have ignored the floor() part as it doesnt matter here if its a floor or
ceiling.
Let n = 2^m, T(n) = T(2^m) Let T(2^m) = S(m) From the above two, T(n) = S(m) S(m) = S(m/2) + C1
/* This is simply binary search recursion*/ S(m) = O(logm) = O(loglogn) /* Since n = 2^m */ Now, let
us go back to the original recursive function T(n) T(n) = S(m) = O(LogLogn)

1. Consider the following C program segment where CellNode represents a node in a
binary tree:
struct CellNode
{
struct CellNOde *leftChild;
int element;
struct CellNode *rightChild;
};
int GetValue(struct CellNode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if ((ptr->leftChild == NULL) &&
(ptr->rightChild == NULL))
value = 1;
else
value = value + GetValue(ptr->leftChild)
+ GetValue(ptr->rightChild);
}
return(value);
}
The value returned by GetValue() when a pointer to the root of a binary tree is passed as
its argument is:
(A) the number of nodes in the tree
(B) the number of internal nodes in the tree
(C) the number of leaf nodes in the tree
(D) the height of the tree

2. Consider the process of inserting an element into a Max Heap, where the Max Heap is
represented by an array. Suppose we perform a binary search on the path from the new
leaf to the root to find the position for the newly inserted element, the number of
comparisons performed is:
(A) (logn)
(B) (LogLogn )
(C) (n)
(D) (nLogn)
The height of a Max Heap is (logn). If we perform binary search for finding the correct position then
we need to do (LogLogn) comparisons.

3. Let w be the minimum weight among all edge weights in an undirected connected
graph. Let e be a specific edge of weight w . Which of the following is FALSE?
(A) There is a minimum spanning tree containing e.
(B) If e is not in a minimum spanning tree T, then in the cycle formed by adding e to T, all edges have
the same weight.
(C) Every minimum spanning tree has an edge of weight w .
(D) e is present in every minimum spanning tree.
(A), (B) and (C) are correct.
(D) is incorrect as there may be many edges of wight w in the graph and e may not be picked up in
some of the minimum spanning trees.

4. An array of n numbers is given, where n is an even number. The maximum as well as
the minimum of these n numbers needs to be determined. Which of the following is
TRUE about the number of comparisons needed?
(A) At least 2n c comparisons, for some constant c, are needed.
(B) At most 1.5n 2 comparisons are needed.
(C) At least nLog2n comparisons are needed.
(D) None of the above.

5. Consider the following C code segment:
int IsPrime(n)
{
int i,n;
for(i=2;i<=sqrt(n);i++)
if(n%i == 0)
{printf(Not Prime\n); return 0;}
return 1;
}
Let T(n) denotes the number of times the for loop is executed by the program on input
n. Which of the following is TRUE?
(A) T(n) = O(sqrt(n)) and T(n) = (sqrt(n))
(B) T(n) = O(sqrt(n)) and T(n) = (1)
(C) T(n) = O(n) and T(n) = (sqrt(n))
(D) None of the above
Big O notation describes the upper bound and Big Omega notation describes the lower bound for an
algorithm.
The for loop in the question is run maximum sqrt(n) times and minimum 1 time. Therefore, T(n) =
O(sqrt(n)) and T(n) = (1)

1. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
a) n/2
b) (n-1)/3
c) (n-1)/2
d) (2n+1)/3
Let L be the number of leaf nodes and I be the number of internal nodes, then following relation holds
for above given tree
L = (3-1)I + 1 = 2I + 1
Total number of nodes(n) is sum of leaf nodes and internal nodes
n = L + I
After solving above two, we get L = (2n+1)/3

2. The running time of the following algorithm
Procedure A(n)
If n <= 2 return(1) else return A( );
is best described by
a) O(n)
b) O(log n)
c) O(1og log n)
d) O(1)

3. A weight-balanced tree is a binary tree in which for each node. The number of nodes
in the left sub tree is at least half and at most twice the number of nodes in the right sub
tree. The maximum possible height (number of nodes on the path from the root to the
farthest leaf) of such a tree on n nodes is best described by which of the following?
a)
b)
c)
d)
Let the maximum possible height of a tree with n nodes is represented by H(n).
The maximum possible value of H(n) can be approximately written using following recursion
H(n) = H(2n/3) + 1
The solution of above recurrence is . We can simply get it by drawing a recursion tree.

4. Consider the following algorithm for searching for a given number x in an unsorted
array A[1..n] having n distinct values:
1) Choose an i uniformly at random from 1..n;
2) If A[i] = x then Stop else Goto 1;
Assuming that x is present in A, what is the expected number of comparisons made by
the algorithm before it terminates?
a) n
b) n-l
c) 2n
d) n/2
If you remember the coin and dice questions, you can just guess the answer for the above.
Below is proof for the answer.
Let expected number of comparisons be E. Value of E is sum of following expression for all the
possible cases.
number_of_comparisons_for_a_case * probability_for_the_case
Case 1
If A[i] is found in the first attempt
number of comparisons = 1
probability of the case = 1/n
Case 2
If A[i] is found in the second attempt
number of comparisons = 2
probability of the case = (n-1)/n*1/n
Case 3
If A[i] is found in the third attempt
number of comparisons = 2
probability of the case = (n-1)/n*(n-1)/n*1/n
There are actually infinite such cases. So, we have following infinite series for E.
E = 1/n + [(n-1)/n]*[1/n]*2 + [(n-1)/n]*[(n-1)/n]*[1/n]*3 + . (1)
After multiplying equation (1) with (n-1)/n, we get
E (n-1)/n = [(n-1)/n]*[1/n] + [(n-1)/n]*[(n-1)/n]*[1/n]*2 +
[(n-1)/n]*[(n-1)/n]*[(n-1)/n]*[1/n]*3
.(2)
Subtracting (2) from (1), we get
E/n = 1/n + (n-1)/n*1/n + (n-1)/n*(n-1)/n*1/n +
The expression on right side is a GP with infinite elements. Let us apply the sum formula (a/(1-r))
E/n = [1/n]/[1-(n-1)/n] = 1
E = n

1. We have a binary heap on n elements and wish to insert n more elements (not
necessarily one after another) into this heap. The total time required for this is
(A) (logn)
(B) (n)
(C) (nlogn)
(D) (n^2)
The worst case time complexity for insertion in a binary heap is O(Logn) (Refer Wiki). So inserting n
elements in a heap of size n should take (nlogn) time.

2. The Breadth First Search algorithm has been implemented using the queue data
structure. One possible order of visiting the nodes of the following graph is

(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR

3. Consider the following functions:
f(n) = 2^n g(n) = n! h(n) = n^logn Which of the following statements about the
asymptotic behaviour of f(n), g(n), and h(n) is true?
(A) f(n) = O(g(n)); g(n) = O(h(n))
(B) f(n) = (g(n)); g(n) = O(h(n))
(C) g(n) = O(f(n)); h(n) = O(f(n))
(D) h(n) = O(f(n)); g(n) = (f(n))
According to order of growth: h(n) < f(n) < g(n) (g(n) is asymptotically greater than f(n) and f(n) is
asymptotically greater than h(n) )
We can easily see above order by taking logs of the given 3 functions
lognlogn < n < log(n!) (logs of the given f(n), g(n) and h(n)). Note that log(n!) = (nlogn)

4. The minimum number of comparisons required to determine if an integer appears
more than n/2 times in a sorted array of n integers is
(A) (n)
(B) (logn)
(C) (log*n)
(D) (n)

1. The most efficient algorithm for finding the number of connected components in an
undirected graph on n vertices and m edges has time complexity.
(A) (n)
(B) (m)
(C) (m + n)
(D) (mn)
Connected components can be found in O(m + n) using Tarjans algorithm. Once we have connected
components, we can count them.

2. Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot
element which splits the list into two sub-lists each of which contains at least one-fifth
of the elements. Let T(n) be the number of comparisons required to sort n elements.
Then
(A) T(n) <= 2T(n/5) + n
(B) T(n) <= T(n/5) + T(4n/5) + n
(C) T(n) <= 2T(4n/5) + n
(D) T(n) <= 2T(n/2) + n
For the case where n/5 elements are in one subset, T(n/5) comparisons are needed for the first subset
with n/5 elements, T(4n/5) is for the rest 4n/5 elements, and n is for finding the pivot.
If there are more than n/5 elements in one set then other set will have less than 4n/5 elements and
time complexity will be less than T(n/5) + T(4n/5) + n because recursion tree will be more balanced.

3 Dijkstras single source shortest path algorithm when run from vertex a in the below
graph, computes the correct shortest path distance to

(A) only vertex a
(B) only vertices a, e, f, g, h
(C) only vertices a, b, c, d
(D) all the vertices
Dijkstras single source shortest path is not guaranteed to work for graphs with negative weight edges,
but it works for the given graph.
Let us see
Let us run the 1st pass
b 1
b is minimum, so shortest distance to b is 1.
After 1st pass, distances are
c 3, e -2.
e is minimum, so shortest distance to e is -2
After 2nd pass, distances are
c 3, f 0.
f is minimum, so shortest distance to f is 0
After 3rd pass, distances are
c 3, g 3.
Both are same, let us take g. so shortest distance to g is 3.
After 4th pass, distances are
c 3, h 5
c is minimum, so shortest distance to c is 3
After 5th pass, distances are
h -2
h is minimum, so shortest distance to h is -2
4. The following C function takes a single-linked list of integers as a parameter and
rearranges the elements of the list. The function is called with the list containing the
integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the
function completes execution?
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}
(A) 1,2,3,4,5,6,7
(B) 2,1,4,3,6,5,7
(C) 1,3,2,5,4,7,6
(D) 2,3,4,5,6,7,1
The function rearrange() exchanges data of every node with its next node. It starts exchanging data
from the first node itself.

1. Consider a binary max-heap implemented using an array. Which one of the following
array represents a binary max-heap?

(A) 25,12,16,13,10,8,14
(B) 25,14,13,16,10,8,12
(C) 25,14,16,13,10,8,12
(D) 25,14,12,13,10,8,16
A tree is max-heap if data at every node in the tree is greater than or equal to its children s data.
In array representation of heap tree, a node at index i has its left child at index 2i + 1 and right child at
index 2i + 2.
25
/ \
/ \
14 16
/ \ / \
/ \ / \
13 10 8 12
2. What is the content of the array after two delete operations on the correct answer to
the previous question?

(A) 14,13,12,10,8
(B) 14,12,13,8,10
(C) 14,13,8,12,10
(D) 14,13,12,8,10
For Heap trees, deletion of a node includes following two operations.
1) Replace the root with last element on the last level.
2) Starting from root, heapify the complete tree from top to bottom..
Let us delete the two nodes one by one:
1) Deletion of 25:
Replace 25 with 12
12
/ \
/ \
14 16
/ \ /
/ \ /
13 10 8
Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.
16
/ \
/ \
14 12
/ \ /
/ \ /
13 10 8
2) Deletion of 16:
Replace 16 with 8
8
/ \
/ \
14 12
/ \
/ \
13 10
Heapify from root to bottom.
14
/ \
/ \
8 12
/ \
/ \
13 10
14
/ \
/ \
13 12
/ \
/ \
8 10
3. In quick sort, for sorting n elements, the (n/4)th smallest element is selected as pivot
using an O(n) time algorithm. What is the worst case time complexity of the quick sort?

(A) (n)
(B) (nLogn)
(C) (n^2)
(D) (n^2 log n)
The recursion expression becomes:
T(n) = T(n/4) + T(3n/4) + cn
After solving the above recursion, we get (nLogn).

4. What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of
a tree with a single node is 0.
(A) 2
(B) 3
(C) 4
(D) 5
AVL trees are binary trees with the following restrictions.
1) the height difference of the children is at most 1.
2) both children are AVL trees
a
/ \
/ \
b c
/ \ /
/ \ /
d e g
/
/
h
References:
http://en.wikipedia.org/wiki/AVL_tree

1. An implementation of a queue Q, using two stacks S1 and S2, is given below:

void insert(Q, x) {
push (S1, x);
}

void delete(Q){
if(stack-empty(S2)) then
if(stack-empty(S1)) then {
print(Q is empty);
return;
}
else while (!(stack-empty(S1))){
x=pop(S1);
push(S2,x);
}
x=pop(S2);
}
Let n insert and m (<=n) delete operations be performed in an arbitrary order on an
empty queue Q. Let x and y be the number of push and pop operations performed
respectively in the process. Which one of the following is true for all m and n?
(A) n+m <= x < 2n and 2m <= y <= n+m
(B) n+m <= x < 2n and 2m<= y <= 2n
(C) 2m <= x < 2n and 2m <= y <= n+m
(D) 2m <= x <2n and 2m <= y <= 2n
The order in which insert and delete operations are performed matters here.
The best case: Insert and delete operations are performed alternatively. In every delete operation, 2
pop and 1 push operations are performed. So, total m+ n push (n push for insert() and m push for
delete()) operations and 2m pop operations are performed.

The worst case: First n elements are inserted and then m elements are deleted. In first delete
operation, n + 1 pop operations and n push operation are performed. Other than first, in all delete
operations, 1 pop operation is performed. So, total m + n pop operations and 2n push operations are
performed (n push for insert() and m push for delete())
2. Consider the following graph:

Which one of the following cannot be the sequence of edges added, in that order, to a
minimum spanning tree using Kruskals algorithm?
(A) (ab),(df),(bf),(dc),(de)
(B) (ab),(df),(dc),(bf),(de)
(C) (df),(ab),(dc),(bf),(de)
(D) (df),(ab),(bf),(de),(dc)
The edge (d-e) cannot be considered before (d-c) in Kruskals minimum spanning tree
algorithmbecause Kruskals algorithm picks the edge with minimum weight from the current set of
edges at each step.
3. The median of n elements can be found in O(n)time. Which one of the following is
correct about the complexity of quick sort, in which median is selected as pivot?
(A) (n)
(B) (nlogn)
(C) (n^2)
(D) (n^3)
If median is always used as pivot, then recursion remains T(n) = 2T(n/2) + cn for all the cases where
cn is combined time for median finding and partition. So, worst case time complexity of this quick sort
becomes (nlogn). In practical implementations, however, this variant is considerably slower on
average (see http://en.wikipedia.org/wiki/Quicksort#Selection-based_pivoting)

1. Consider the polynomial p(x) = a0 + a1x + a2x^2 +a3x^3, where ai != 0, for all i. The
minimum number of multiplications needed to evaluate p on an input x is:
(A) 3
(B) 4
(C) 6
(D) 9
Multiplications can be minimized using following order for evaluation of the given expression.
p(x) = a0 + x(a1 + x(a2 + a3x))
2. To implement Dijkstras shortest path algorithm on unweighted graphs so that it runs
in linear time, the data structure to be used is:
(A) Queue
(B) Stack
(C) Heap
(D) B-Tree
The shortest path in an un-weighted graph means the smallest number of edges that must be
traversed in order to reach the destination in the graph. This is the same problem as solving the
weighted version where all the weights happen to be 1. If we use Queue (FIFO) instead of Priority
Queue (Min Heap), we get the shortest path in linear time O(|V| + |E|). Basically we do BFS traversal
of the graph to get the shortest paths.
3. A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3
children. A 3-ary heap can be represented by an array as follows: The root is stored in
the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to
a[3]. The nodes from the second level of the tree from left to right are stored from a[4]
location onward. An item x can be inserted into a 3-ary heap containing n items by
placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Which one of the following is a valid sequence of elements in an array representing 3-
ary max heap?
(A) 1, 3, 5, 6, 8, 9
(B) 9, 6, 3, 1, 8, 5
(C) 9, 3, 6, 8, 5, 1
(D) 9, 5, 6, 8, 3, 1
9
/ | \
/ | \
5 6 8
/ |
/ |
3 1
4. Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3- ary
max heap found in the above question, Which one of the following is the sequence of
items in the array representing the resultant heap?
(A) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4
(B) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
(C) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3
(D) 10, 8, 6, 9, 7, 2, 3, 4, 1, 5
After insertion of 7
9
/ | \
/ | \
7 6 8
/ | \
/ | \
3 1 5
After insertion of 2
9
/ | \
/ | \
7 6 8
/ | \ /
/ | \ /
3 1 5 2
After insertion of 10
10
/ | \
/ | \
7 9 8
/ | \ / |
/ | \ / |
3 1 5 2 6
After insertion of 4
10
/ | \
/ | \
7 9 8
/ | \ / | \
/ | \ / | \
3 1 5 2 6 4

1. Let X be a problem that belongs to the class NP. Then which one of the following is
TRUE?
(A) There is no polynomial time algorithm for X.
(B) If X can be solved deterministically in polynomial time, then P = NP.
(C) If X is NP-hard, then it is NP-complete.
(D) X may be undecidable.
(A) is incorrect because set NP includes both P(Polynomial time solvable) and NP-Complete .
(B) is incorrect because X may belong to P (same reason as (A))
(C) is correct because NP-Complete set is intersection of NP and NP-Hard sets.
(D) is incorrect because all NP problems are decidable in finite set of operations.
2. What is the number of swaps required to sort n elements using selection sort, in the
worst case?
(A) (n)
(B) (n log n)
(C) (n^2 )
(D) (n^2 log n)
Here is Selection Sort algorithm for sorting in ascending order.
1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps
above for the remainder of the list (starting at the second position and advancing each time) As we can
see from the algorithm, selection sort performs swap only after finding the appropriate position of the
current picked element. So there are O(n) swaps performed in selection sort.
Because swaps require writing to the array, selection sort is preferable if writing to memory is
significantly more expensive than reading. This is generally the case if the items are huge but the keys
are small. Another example where writing times are crucial is an array stored in EEPROM or Flash.
There is no other algorithm with less data movement.
References:
http://en.wikipedia.org/wiki/Selection_sort
3. The running time of an algorithm is represented by the following recurrence
relation:
if n <= 3 then T(n) = n else T(n) = T(n/3) + cn Which one of the following represents the
time complexity of the algorithm?
(A) (n)
(B) (n log n)
(C) (n^2)
(D) (n^2log n)
T(n) = cn + T(n/3) = cn + cn/3 + T(n/9) = cn + cn/3 + cn/9 + T(n/27) Taking the sum of infinite GP
series. The value of T(n) will be less than this sum. T(n) <= cn(1/(1-1/3)) <= 3cn/2 or we can say cn
<= T(n) <= 3cn/2 Therefore T(n) = (n) This can also be solved using Master Theorem for solving
recurrences. The given expression lies in Case 3 of the theorem.
4. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of
length 10 using open addressing with hash function h(k) = k mod 10 and linear probing.
What is the resultant hash table?

To get the idea of open addressing concept, you can go through below lines from Wikipedia
.
Open addressing, or closed hashing, is a method of collision resolution in hash tables. With this
method a hash collision is resolved by probing, or searching through alternate locations in the array
(the probe sequence) until either the target record is found, or an unused array slot is found, which
indicates that there is no such key in the table. Well known probe sequences include:
linear probing in which the interval between probes is fixedoften at 1.
quadratic probing in which the interval between probes increases linearly (hence, the indices are
double hashing in which the interval between probes is fixed for each record but is computed by
another hash function.

1. Let S be an NP-complete problem and Q and R be two other problems not known to be
in NP. Q is polynomial time reducible to S and S is polynomial-time reducible to R.
Which one of the following statements is true?
(A) R is NP-complete
(B) R is NP-hard
(C) Q is NP-complete
(D) Q is NP-hard
(A) Incorrect because R is not in NP. A NP Complete problem has to be in both NP and NP-hard.
(B) Correct because a NP Complete problem S is polynomial time educable to R.
(C) Incorrect because Q is not in NP.
(D) Incorrect because there is no NP-complete problem that is polynomial time Turing-reducible to
Q.

2) A set X can be represented by an array x[n] as follows:

Consider the following algorithm in which x,y and z are Boolean arrays of size n:

algorithm zzz(x[] , y[], z [])
{
int i;
for (i=O; i<n; ++i)
z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i])
}
The set Z computed by the algorithm is:
(A) (X Intersection Y)
(B) (X Union Y)
(C) (X-Y) Intersection (Y-X)
(D) (X-Y) Union (Y-X)
The expression x[i] ^ ~y[i]) results the only 1s in x where corresponding entry in y is 0. An array with
these set bits represents set X Y
The expression ~x[i] ^ y[i]) results the only 1s in y where corresponding entry in x is 0. An array with
these set bits represents set Y X.
The operator V results in Union of the above two sets.

3. Consider the following recurrence:

Which one of the following is true?
(A) T(n) = (loglogn)
(B) T(n) = (logn)
(C) T(n) = (sqrt(n))
(D) T(n) = (n)
Let n = 2^m
T(2^m) = T(2^(m/2)) + 1
Let T(2^m) = S(m)
S(m) = 2S(m/2) + 1
Above expression is a binary tree traversal recursion whose time complexity is (m). You can also
prove using Master theorem.
S(m) = (m)
= (logn) /* Since n = 2^m */
Now, let us go back to the original recursive function T(n)
T(n) = T(2^m) = S(m)
= (Logn)

1. The subset-sum problem is defined as follows. Given a set of n positive integers, S =
{a1 ,a2 ,a3 ,,an} and positive integer W, is there a subset of S whose elements sum to
W? A dynamic program for solving this problem uses a 2-dimensional Boolean array X,
with n rows and W+1 columns. X[i, j],1 <= i <= n, 0 <= j <= W, is TRUE if and only if
there is a subset of {a1 ,a2 ,...,ai} whose elements sum to j. Which of the following is
valid for 2 <= i <= n and ai <= j <= W?
(A) X[i, j] = X[i - 1, j] V X[i, j -ai]
(B) X[i, j] = X[i - 1, j] V X[i - 1, j - ai]
(C) X[i, j] = X[i - 1, j] V X[i, j - ai]
(D) X[i, j] = X[i - 1, j] V X[i -1, j - ai]
X[I, j] (2 <= i <= n and ai <= j <= W), is true if any of the following is true
1) Sum of weights excluding ai is equal to j, i.e., if X[i-1, j] is true.
2) Sum of weights including ai is equal to j, i.e., if X[i-1, j-ai] is true so that we get (j ai) + ai as j.
2. In question 1, which entry of the array X, if TRUE, implies that there is a subset
whose elements sum to W?
(A) X[1, W]
(B) X[n ,0]
(C) X[n, W]
(D) X[n -1, n]
If we get the entry X[n, W] as true then there is a subset of {a1, a2, .. an} that has sum as W.
Reference: http://en.wikipedia.org/wiki/Subset_sum_problem
3. Consider the following C program that attempts to locate an element x in an array Y[]
using binary search. The program is erroneous.
1. f(int Y[10], int x) {
2. int i, j, k;
3. i = 0; j = 9;
4. do {
5. k = (i + j) /2;
6. if( Y[k] < x) i = k; else j = k;
7. } while(Y[k] != x && i < j);
8. if(Y[k] == x) printf ("x is in the array ") ;
9. else printf (" x is not in the array ") ;
10. }
On which of the following contents of Y and x does the program fail?
(A) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
(B) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
(C) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
(D) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
The above program doesnt work for the cases where element to be searched is the last element of Y[]
or greater than the last element (or maximum element) in Y[]. For such cases, program goes in an
infinite loop because i is assigned value as k in all iterations, and i never becomes equal to or greater
than j. So while condition never becomes false.

4. In question 3, the correction needed in the program to make it work properly is
(A) Change line 6 to: if (Y[k] < x) i = k + 1; else j = k-1;
(B) Change line 6 to: if (Y[k] < x) i = k - 1; else j = k+1;
(C) Change line 6 to: if (Y[k] <= x) i = k; else j = k;
(D) Change line 7 to: } while ((Y[k] == x) && (i < j));
Below is the corrected function
f(int Y[10], int x) {
int i, j, k;
i = 0; j = 9;
do {
k = (i + j) /2;
if( Y[k] < x) i = k + 1; else j = k 1;
} while(Y[k] != x && i < j);
if(Y[k] == x) printf ("x is in the array ") ;
else printf (" x is not in the array ") ;
}
Reference: http://en.wikipedia.org/wiki/Binary_search_algorithm#Implementations

1) A program P reads in 500 integers in the range [0..100] exepresenting the scores of
500 students. It then prints the frequency of each score above 50. What would be the
best way for P to store the frequencies?
(a) An array of 50 numbers
(b) An array of 100 numbers
(c) An array of 500 numbers
(d) A dynamically allocated array of 550 numbers
An array of size 50 looks the best option to store number of students for each score. We need to store
frequencies of scores above 50. We can ignore scores below 50 and to index the scores above 50, we
can subtract 50 from the score value/

2) An undirected graph G has n nodes. Its adjacency matrix is given by an n n square
matrix whose (i) diagonal elements are 0s and (ii) non-diagonal elements are 1s. which
one of the following is TRUE?
(a) Graph G has no minimum spanning tree (MST)
(b) Graph G has a unique MST of cost n-1
(c) Graph G has multiple distinct MSTs, each of cost n-1
(d) Graph G has multiple spanning trees of different costs
If all non diagonal elements are 1, then every vertex is connected to every other vertex in the graph
with an edge of weight 1. Such a graph has multiple distinct MSTs with cost n-1. See the below
example.
The connected graph:

Below are three Minimum Spanning trees each of cost 2.0.
Minimum Spanning Tree 1

Minimum Spanning Tree 2

Minimum Spanning Tree 3

3) The time complexity of computing the transitive closure of a binary relation on a set
of n elements is known to be:
a) O(n)
b) O(nLogn)
c) O(n^(3/2))
d) O(n^3)
In mathematics, the transitive closure of a binary relation R on a set X is the smallest transitive
relation on X that contains R. If the original relation is transitive, the transitive closure will be that
same relation; otherwise, the transitive closure will be a different relation.
In computer science the concept of transitive closure can be thought of as constructing a data
structure that makes it possible to answer reachability questions. That is, can one get from node a to
node other node b in one or more hops? A binary relation tells you only that node a is connected to
node b, and that node b is connected to node c, etc. After the transitive closure is constructed in an
O(1) operation one may determine that node c is reachable from node a.
Warshalls algorithm can be used to construct the Transitive closure of directed graphs (). In
Warshalls original formulation of the algorithm, the graph is unweighted and represented by a
Boolean adjacency matrix. Then the addition operation is replaced by logical conjunction (AND) and
the minimum operation by logical disjunction (OR).
References:
http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
http://en.wikipedia.org/wiki/Transitive_closure
4. A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The
level-order traversal of the heap is given below:
10, 8, 5, 3, 2
Two new elements 1 and 7 are inserted in the heap in that order. The level-order
traversal of the heap after the insertion of the elements is:
(a) 10, 8, 7, 5, 3, 2, 1
(b) 10, 8, 7, 2, 3, 1, 5
(c) 10, 8, 7, 1, 2, 3, 5
(d) 10, 8, 7, 3, 2, 1, 5
Original Max-Heap is:
10
/ \
8 5
/ \
3 2
After Insertion of 1.
10
/ \
8 5
/ \ /
3 2 1
After Insertion of 7.
10
/ \
8 7
/ \ / \
3 2 1 5

1. Which one of the following is a key factor for preferring B-trees to binary search trees
for indexing database relations?
(a) Database relations have a large number of records
(b) Database relations are sorted on the primary key
(c) B-trees require less memory than binary search trees
(d) Data transfer form disks is in blocks.
A disk block contains fairly large number of keys. Unlike BST where each node contains only one key,
B-Tree is designed to contain large number of keys so that tree height is small.
2. How many distinct binary search trees can be created out of 4 distinct keys?
(a) 5
(b) 14
(c) 24
(d) 42
Here is a systematic way to enumerate these BSTs. Consider all possible binary search trees with each
element at the root. If there are n nodes, then for each choice of root node, there are n 1 non-root
nodes and these non-root nodes must be partitioned into those that are less than a chosen root and
those that are greater than the chosen root.
Lets say node i is chosen to be the root. Then there are i 1 nodes smaller than i and n i nodes
bigger than i. For each of these two sets of nodes, there is a certain number of possible subtrees.
Let t(n) be the total number of BSTs with n nodes. The total number of BSTs with i at the root is t(i
1) t(n i). The two terms are multiplied together because the arrangements in the left and right
subtrees are independent. That is, for each arrangement in the left tree and for each arrangement in
the right tree, you get one BST with i at the root.
Summing over i gives the total number of binary search trees with n nodes.

The base case is t(0) = 1 and t(1) = 1, i.e. there is one empty BST and there is one BST with one node.

3. In a complete k-ary tree, every internal node has exactly k children. The number of
leaves in such a tree with n internal nodes is:
(a) nk
(b) (n 1) k+ 1
(c) n( k 1) + 1
(d) n(k 1)
4) Suppose T(n) = 2T(n/2) + n, T(0) = T(1) = 1
Which one of the following is false.
a) T(n) = O(n^2)
b) T(n) = (nLogn)
c) T(n) = (n^2)
d) T(n) = O(nLogn)
The given recurrence relation can be solved using Master Theorem. It lies in case 2 of Master
Theorem. Or, if you remember recurrence relation of Merge Sort or best case Quick Sort, you can
guess the value of T(n).
T(n) = (nLogn)
By definition of Big O notation, we can say.
(nLogn) = O(nLogn) = O(n^2)
(nLogn) ca be equal to (n) or (nLogn), but not (n^2)

1. The following C function takes a simply-linked list as input argument. It modifies the
list by moving the last element to the front of the list and returns the modified list.
Some part of the code is left blank.

typedef struct node
{
int value;
struct node *next;
}Node;

{
Node *p, *q;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
}
Choose the correct alternative to replace the blank line.
(C) head = p; p->next = q; q->next = NULL;
When the while loop ends, q contains address of second last node and p contains address of last node.
So we need to do following things after while loop.
i) Set next of q as NULL (q->next = NULL).
Step (ii) must be performed before step (iii). If we change head first, then we lose track of head node

2. A hash table of length 10 uses open addressing with hash function h(k)=k mod 10,
and linear probing. After inserting 6 values into an empty hash table, the table is as
shown below.

Which one of the following choices gives a possible order in which the key values could
have been inserted in the table?
(A) 46, 42, 34, 52, 23, 33
(B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33
(D) 42, 46, 33, 23, 34, 52
The sequence (A) doesnt create the hash table as the element 52 appears before 23 in this sequence.
The sequence (B) doesnt create the hash table as the element 33 appears before 46 in this sequence.
The sequence (C) creates the hash table as 42, 23 and 34 appear before 52 and 33, and 46 appears
before 33.
The sequence (D) doesnt create the hash table as the element 33 appears before 23 in this sequence.
3. How many different insertion sequences of the key values using the same hash
function and linear probing will result in the hash table shown above?
(A) 10
(B) 20
(C) 30
(D) 40
In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and 33, and 46 must
appear before 33.
Total number of different sequences = 3! x 5 = 30
In the above expression, 3! is for elements 42, 23 and 34 as they can appear in any order, and 5 is for
element 46 as it can appear at 5 different places.

1 Consider a complete undirected graph with vertex set {0, 1, 2, 3, 4}. Entry Wij in the
matrix W below is the weight of the edge {i, j}.
What is the minimum possible weight of a spanning tree
T in this graph such that vertex 0 is a leaf node in the tree T?
(A) 7
(B) 8
(C) 9
(D) 10
To get the minimum spanning tree with vertex 0 as leaf, first remove 0th row and 0th column and
then get the minimum spanning tree (MST) of the remaining graph. Once we have MST of the
remaining graph, connect the MST to vertex 0 with the edge with minimum weight (we have two
options as there are two 1s in 0th row).

2. In the graph given in question 1, what is the minimum possible weight of a path P
from vertex 1 to vertex 2 in this graph such that P contains at most 3 edges?
(A) 7
(B) 8
(C) 9
(D) 10
Path: 1 -> 0 -> 4 -> 2
Weight: 1 + 4 + 3
3. The degree sequence of a simple graph is the sequence of the degrees of the nodes in
the graph in decreasing order. Which of the following sequences can not be the degree
sequence of any graph?
I. 7, 6, 5, 4, 4, 3, 2, 1
II. 6, 6, 6, 6, 3, 3, 2, 2
III. 7, 6, 6, 4, 4, 3, 2, 2
IV. 8, 7, 7, 6, 4, 2, 1, 1
(A) I and II
(B) III and IV
(C) IV only
(D) II and IV
In sequence IV, we have a vertex with degree 8 which is not possible in a simple graph (no self loops
and no multiple edges) with total vertex count as 8. Maximum possible degree in such a graph is 7.
In sequence II, four vertices are connected to 6 other vertices, but remaining 4 vertices have degrees
as 3, 3, 2 and 2 which are not possible in a simple graph (no self loops and no multiple edges).
4. Consider a B+-tree in which the maximum number of keys in a node is 5. What is the
minimum number of keys in any non-root node?
(A) 1
(B) 2
(C) 3
(D) 4
Since the maximum number of keys is 5, maximum number of children a node can have is 6.
Bydefinition of B Tree, minimum children that a node can have would be 6/2 = 3. Therefore,
minimum number of keys that a node can have becomes 2 (3-1).

1) A max-heap is a heap where the value of each parent is greater than or equal to the
values of its children. Which of the following is a max-heap?

A binary tree is max-heap if it is a complete binary tree (A complete binary tree is a binary tree in
which every level, except possibly the last, is completely filled, and all nodes are as far left as possible)
and it follows the max-heap property (value of each parent is greater than or equal to the values of its
children).
A) is not a max-heap because it is not a complete binary tree
B) is a max-heap because it is complete binary tree and follows max-heap property.
C) is not a max-heap because 8 is a chile of 5 in this tree, so violates the max-heap property.
D) is not a max-heap because 8 is a chile of 5 in this tree, so violates the max-heap property. There are
many other nodes in this tree which violate max-heap property in this tree.
2) Four matrices M1, M2, M3 and M4 of dimensions pxq, qxr, rxs and sxt respectively
can be multiplied is several ways with different number of total scalar multiplications.
For example, when multiplied as ((M1 X M2) X (M3 X M4)), the total number of
multiplications is pqr + rst + prt. When multiplied as (((M1 X M2) X M3) X M4), the
total number of scalar multiplications is pqr + prs + pst.
If p = 10, q = 100, r = 20, s = 5 and t = 80, then the number of scalar multiplications
needed is
A) 248000
B) 44000
C) 19000
D) 25000
We get minimum number of multiplications using ((M1 X (M2 X M3)) X M4).
Total number of multiplications = 100x20x5 (for M2 x M3) + 10x100x5 + 10x5x80 = 19000.

3) Which of the given options provides the increasing order of asymptotic complexity of
functions f1, f2, f3 and f4?

f1(n) = 2^n
f2(n) = n^(3/2)
f3(n) = nLogn
f4(n) = n^(Logn)
A) f3, f2, f4, f1
B) f3, f2, f1, f4
C) f2, f3, f1, f4
D) f2, f3, f4, f1

4) We are given a set of n distinct elements and an unlabeled binary tree with n nodes.
In how many ways can we populate the tree with the given set so that it becomes a
binary search tree?
A) 0
B) 1
C) n!
D) (1/(n+1)).2nCn
See this explanation from PeddaBoku.
5) An algorithm to find the length of the longest monotonically increasing sequence of
numbers in an array A[0 :n-1] is given below.
Let Li denote the length of the longest monotonically increasing sequence starting at
index i in the array

Which of the following statements is TRUE?
(A) The algorithm uses dynamic programming paradigm
(B) The algorithm has a linear complexity and uses branch and bound paradigm
(C) The algorithm has a non-linear polynomial complexity and uses branch and bound paradigm
(D) The algorithm uses divide and conquer paradigm.

1) An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,.vn. Two
nodes vi , vj are connected if and only if 0 < |i j| <= 2. Each edge (vi, vj ) is assigned a
weight i + j. A sample graph with n = 4 is shown below.

What will be the cost of the minimum spanning tree (MST) of such a graph with n
nodes?
(A) 1/12(11n^2 5n)
(B) n^2 n + 1
(C) 6n 11
(D) 2n + 1
Minimum spanning tree for 2 nodes would be
(v1) _ (v2)
Total weight 3
Minimum spanning tree for 3 nodes would be
(v1) _ (v2)
|
(v3)
Total weight= 3 + 4 = 7
Minimum spanning tree for 4 nodes would be
(v1) _ (v2) _ (v4)
|
(v3)
Total weight= 3 + 4 + 6 = 13
Minimum spanning tree for 5 nodes would be
(v1) _ (v2) _ (v4)
|
(v3)
|
(v5)
Total weight= 3 + 4 + 6 + 8 = 21
Minimum spanning tree for 6 nodes would be
(v1) _ (v2) _ (v4) _ (v6)
|
(v3)
|
(v5)
Total weight= 3 + 4 + 6 + 8 + 10 = 31
We can observe from above examples that when we add kth node, the weight of spanning tree
increases by 2k-2. Let T(n) be the weight of minimum spanning tree. T(n) can be written as
T(n) = T(n-1) + (2n-2) for n > 2
T(1) = 0, T(2) = 0 and T(2) = 3
The recurrence can be written as sum of series (2n 2) + (2n-4) + (2n-6) + (2n-8) + . 3 and solution
of this recurrence is n^2 n + 1.

2) The length of the path from v5 to v6 in the MST of previous question with n = 10 is
(A) 11
(B) 25
(C) 31
(D) 41
Any MST which has more than 5 nodes will have the same distance between v5 and v6 as the basic
structure of all MSTs (with more than 5 nodes) would be following.
(v1) _ (v2) _ (v4) _ (v6) _ . . (more even numbered nodes)
|
(v3)
|
(v5)
|
.
.
(more odd numbered nodes)
Distance between v5 and v6 = 3 + 4 + 6 + 8 + 10 = 31
3) Consider two binary operators ' ' and ' ' with the precedence of operator being
lower than that of the operator. Operator is right associative while operator is left
associative. Which one of the following represents the parse tree for expression (7 3
4 3 2)?

Let us consider the given expression ( ).
Since the precedence of is higher, the sub-expression ( ) will be evaluated first. In this
sub-expression, would be evaluated first because is right to left associative. So the expression
is evaluated as . Also, note that among the two operators, first one is
evaluated before the second one because the associativity of is left to right.

1) Let w(n) and A(n) denote respectively, the worst case and average case running time
of an algorithm executed on an input of size n. which of the following is ALWAYS
TRUE?
(A)
(B)
(C)
(D)
The worst case time complexity is always greater than or same as the average case time complexity.

2) The worst case running time to search for an element in a balanced in a binary
search tree with n2^n elements is
(A)
(B)
(C)
(D)
Time taken to search an element is where h is the height of Binary Search Tree (BST). The
growth of height of a balanced BST is logerthimic in terms of number of nodes. So the worst case time
to search an element would be which is Which
is which can be written as .

3) Assuming P != NP, which of the following is true ?
(A) NP-complete = NP
(B) NP-complete P =
(C) NP-hard = NP
(D) P = NP-complete
The answer is B (no NP-Complete problem can be solved in polynomial time). Because, if one NP-
Complete problem can be solved in polynomial time, then all NP problems can solved in polynomial
time. If that is the case, then NP and P set become same which contradicts the given condition.

4) The height of a tree is defined as the number of edges on the longest path in the tree.
The function shown in the pseudocode below is invoked as height (root) to compute the
height of a binary tree rooted at the tree pointer root.

The appropriate expression for the two boxes B1 and B2 are
(A) B1 : (1 + height(n->right)), B2 : (1 + max(h1,h2))
(B) B1 : (height(n->right)), B2 : (1 + max(h1,h2))
(C) B1 : height(n->right), B2 : max(h1,h2)
(D) B1 : (1 + height(n->right)), B2 : max(h1,h2)
The box B1 gets exected when left subtree of n is NULL and right sbtree is not NULL. In this case,
height of n will be height of right subtree plus one.
The box B2 gets executed when both left and right sbtrees of n are not NULL. In this case, height of n
will be max of heights of left and right sbtrees of n plus 1.

5) A list of n string, each of length n, is sorted into lexicographic order using the merge-
sort algorithm. The worst case running time of this computation is
(A)
(B)
(C)
(D)
The recurrence tree for merge sort will have height n. And O(n^2) work will be done at each level of
the recurrence tree (Each level involves n comparisons and a comparison takes O(n) time in worst
case). So time complexity of this Merge Sort will be .

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
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
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

1) Which of the following statements is/are TRUE for an undirected graph?
P: Number of odd degree vertices is even
Q: Sum of degrees of all vertices is even
A) P Only
B) Q Only
C) Both P and Q
D) Neither P nor Q
Q is true: Since the graph is undirected, every edge increases the sum of degrees by 2.
P is true: If we consider sum of degrees and subtract all even degrees, we get an even number (because
Q is true). So total number of odd degree vertices must be even.

2) Consider an undirected random graph of eight vertices. The probability that there is
an edge between a pair of vertices is 1/2. What is the expected number of unordered
cycles of length three?
(A) 1/8
(B) 1
(C) 7
(D) 8
A cycle of length 3 can be formed with 3 vertices. There can be total 8C3 ways to pick 3 vertices from
8. The probability that there is an edge between two vertices is 1/2. So expected number of unordered
cycles of length 3 = (8C3)*(1/2)^3 = 7

3) What is the time complexity of Bellman-Ford single-source shortest path algorithm
on a complete graph of n vertices?
(A)
(B)
(C)
(D)
Time complexity of Bellman-Ford algorithm is where V is number of vertices and E is
number edges (See this). If the graph is complete, the value of E becomes . So overall time
complexity becomes

4) Which of the following statements are TRUE?
(1) The problem of determining whether there exists a cycle in an undirected graph is in
P.
(2) The problem of determining whether there exists a cycle in an undirected graph is in
NP.
(3) If a problem A is NP-Complete, there exists a non-deterministic polynomial time
algorithm to solve A.
(A) 1,2 and 3
(B) 1 and 2 only
(C) 2 and 3 only
(D) 1 and 3 only
1 is true because cycle detection can be done in polynomial time using DFS (See this).
2 is true because P is a subset of NP.
3 is true because NP complete is also a subset of NP and NP means Non-deterministic Polynomial
time solution exists. (See this)

5) Which one of the following is the tightest upper bound that represents the time
complexity of inserting an object into a binary search tree of n nodes?
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n log n)
The worst case occurs for a skewed tree. In a skewed tree, when a new node is inserted as a child of
bottommost node, the time for insertion requires traversal of all node. For example, consider the
following tree and the case when something smaller than 70 is inserted.
100
/
90
/
80
/
70

6) Which one of the following is the tightest upper bound that represents the number of
swaps required to sort n numbers using selection sort?
(A) O(log n)
(B) O(n)
(C) O(n log n)
(D) O(n^2)
Selection sort requires only O(n) swaps. See this for details.

7) Consider the following operation along with Enqueue and Dequeue operations on
queues, where k is a global parameter
MultiDequeue(Q){
m = k
while (Q is not empty and m > 0) {
Dequeue(Q)
m = m - 1
}
}
What is the worst case time complexity of a sequence of n MultiDequeue() operations
on an initially empty queue?
(A)
(B)
(C)
(D)
Since the queue is empty initially, the condition of while loop never becomes true. So the time
complexity is

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
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)
(B)
(C)
(D)
The outer loop runs n/2 or times. The inner loop runs times (Note that j is divide
by 2 in every iteration). So the statement "k = k + n/2;" runs times. The statement
increases value of k by n/2. So the value of k becomes n/2* which is

4) The number of elements that can be sorted in time using heap sort is
(A)
(B)
(C)
(d)
Time complexity of Heap Sort is for m input elements. For m
= , the value of will be
which will
be which is

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
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

#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

Test 4
abcde
aacde

http://geeksquiz.com/

http://geeksquiz.com/data-structure/binary-search-trees/

Binary Search Trees
Question 1
What is the worst case time complexity for search, insert and delete operations in a general Binary
Search Tree?
A
O(n) for all
B
O(Logn) for all
C
O(Logn) for search and insert, and O(n) for delete
D
O(Logn) for search, and O(n) for insert and delete

Discuss it

Question 2
In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to
be deleted has both left and right child as non-empty. Which of the following is true about inorder
successor needed in delete operation?
A
Inorder Successor is always a leaf node
B
Inorder successor is always either a leaf node or a node with
empty left child
C
Inorder successor may be an ancestor of the node
D
Inorder successor is always either a leaf node or a node with
empty right child

Discuss it

Question 3
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many
ways can we populate the tree with the given set so that it becomes a binary search tree? (GATE CS
2011)
A
0
B
1
C
n!
D
(1/(n+1)).2nCn

Discuss it

Question 4
How many distinct binary search trees can be created out of 4 distinct keys?
A
4
B
14
C
24
D
42

Discuss it

Question 5
Which of the following traversal outputs the data in sorted order in a BST?
A
Preorder
B
Inorder
C
Postorder
D
Level order

Discuss it

Question 6
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary
search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order
traversal sequence of the resultant tree?
A
7 5 1 0 3 2 4 6 8 9
B
0 2 4 3 1 6 5 9 8 7
C
0 1 2 3 4 5 6 7 8 9
D
9 8 6 4 2 3 0 1 5 7

Discuss it

Question 7
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5,
15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf
node from the root)? (GATE CS 2004)
A
2
B
3
C
4
D
6

Discuss it

Question 8
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

Discuss it

Question 9
Consider the following Binary Search Tree

10
/ \
5 20
/ / \
4 15 30
/
11
If we randomly search one of the keys present in above BST, what would be the expected number of
comparisons?
A
2.75
B
2.25
C
2.57
D
3.25

Discuss it

Question 10
Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2)
Preorder 3) Postorder
A
Any one of the given three traversals is sufficient
B
Either 2 or 3 is sufficient
C
2 and 3
D
1 and 3

Discuss it

Question 11
Consider the following code snippet in C. The function print() receives root of a Binary Search Tree
(BST) and a positive integer k as arguments.
// A BST node
struct node {
int data;
struct node *left, *right;
};

int count = 0;

void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root->right, k);
count++;
if (count == k)
printf("%d ", root->data);
print(root->left, k);
}
}
What is the output of print(root, 3) where root represent root of the following BST.
15
/ \
10 20
/ \ / \
8 12 16 25
A
10
B
16
C
20
D
20 10

Discuss it

Question 12
Consider the same code as given in above question. What does the function print() do in general?
The function print() receives root of a Binary Search Tree (BST) and a positive integer k as
arguments.
// A BST node
struct node {
int data;
struct node *left, *right;
};

int count = 0;

void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root->right, k);
count++;
if (count == k)
printf("%d ", root->data);
print(root->left, k);
}
}
A
Prints the kth smallest element in BST
B
Prints the kth largest element in BST
C
Prints the leftmost node at level k from root
D
Prints the rightmost node at level k from root

Discuss it

Question 13
You are given the postorder traversal, P, of a binary search tree on the n elements 1, 2, ..., n. You
have to determine the unique binary search tree that has P as its postorder traversal. What is the time
complexity of the most efficient algorithm for doing this?
A
O(Logn)
B
O(n)
C
O(nLogn)
D
none of the above, as the tree cannot be uniquely determined.

Discuss it

Question 14
Suppose we have a balanced binary search tree T holding n numbers. We are given two numbers L
and H and wish to sum up all the numbers in T that lie between L and H. Suppose there are m such
numbers in T. If the tightest upper bound on the time to compute the sum is O(n
a
log
b
n + m
c
log
d
n),
the value of a + 10b + 100c + 1000d is ____.
A
60
B
110
C
210
D
50

Discuss it

http://geeksquiz.com/data-structure/binary-trees/

Binary Trees
Question 1
Which of the following is a true about Binary Trees
A
Every binary tree is either complete or full.
B
Every complete binary tree is also a full binary tree.
C
Every full binary tree is also a complete binary tree.
D
No binary tree is both complete and full.
E
None of the above

Discuss it

Question 2
If arity of operands is fixed, then which of the following notations can be used to parse expressions
without parentheses? a) Infix Notation (Inorder traversal of a expression tree) b) Postfix Notation
(Postorder traversal of a expression tree) c) Prefix Notation (Preorder traversal of a expression tree)
A
b and c
B
Only b
C
a, b and c
D
None of them

Discuss it

Question 3
What are the main applications of tree data structure? 1) Manipulate hierarchical data 2) Make
information easy to search (see tree traversal). 3) Manipulate sorted lists of data 4) Router algorithms
5) Form of a multi-stage decision-making, like Chess Game. 6) As a workflow for compositing digital
images for visual effects
A
1, 2, 3, 4 and 6
B
1, 2, 3, 4 and 5
C
1, 3, 4, 5 and 6
D
1, 2, 3, 4, 5 and 6

Discuss it

Question 4
Level of a node is distance from root to that node. For example, level of root is 1 and levels of left and
right children of root is 2. The maximum number of nodes on level i of a binary tree is

In the following answers, the operator '^' indicates power.
A
2^(i-1)
B
2^i
C
2^(i+1)
D
2^[(i+1)/2]

Discuss it

Question 5
In a complete k-ary tree, every internal node has exactly k children or no child. The number of leaves
in such a tree with n internal nodes is:
A
nk
B
(n 1) k+ 1
C
n( k 1) + 1
D
n(k 1)

Discuss it

Question 6
The maximum number of binary trees that can be formed with three unlabeled nodes is:
A
1
B
5
C
4
D
3

Discuss it

Question 7
In a complete k-ary tree, every internal node has exactly k children. The number of leaves in such a
tree with n internal nodes is: (GATE CS 2005)
A
nk
B
(n 1) k+ 1
C
n( k 1) + 1
D
n( k 1)

Discuss it

Question 8
The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:
A
n/2
B
(n-1)/3
C
(n-1)/2
D
(2n+1)/3

Discuss it

Question 9
A weight-balanced tree is a binary tree in which for each node. The number of nodes in the left sub
tree is at least half and at most twice the number of nodes in the right sub tree. The maximum
possible height (number of nodes on the path from the root to the farthest leaf) of such a tree on n
nodes is best described by which of the following? a) b) c) d)
A
A
B
B
C
C
D
D

Discuss it

Question 10
A complete n-ary tree is a tree in which each node has n children or no children. Let I be the number
of internal nodes and L be the number of leaves in a complete n-ary tree. If L = 41, and I = 10, what is
the value of n?
A
6
B
3
C
4
D
5

Discuss it

Question 11
The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum
number of nodes in a binary tree of height h is:
A
2^h -1
B
2^(h-1) 1
C
2^(h+1) -1
D
2*(h+1)

Discuss it

Question 12
A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. the
root is stored at X[1]. For a node stored at X[i], the left child, if any, is stored in X[2i] and the right
child, if any, in X[2i+1]. To be able to store any binary tree on n vertices the minimum size of X should
be. (GATE CS 2006)
A
log2n
B
n
C
2n + 1
D
2^n 1

Discuss it

Question 13
Postorder traversal of a given binary search tree, T produces the following sequence of keys 10, 9,
23, 22, 27, 25, 15, 50, 95, 60, 40, 29 Which 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, 95
B
9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
C
29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
D
95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29

Discuss it

Question 14
Consider the following nested representation of binary trees: (X Y Z) indicates Y and Z are the left and
right sub stress, respectively, of node X. Note that Y and Z may be NULL, or further nested. Which of
the following represents a valid binary tree?
A
(1 2 (4 5 6 7))
B
(1 (2 3 4) 5 6) 7)
C
(1 (2 3 4)(5 6 7))
D
(1 (2 3 NULL) (4 5))

Discuss it

Question 15
Consider a node X in a Binary Tree. Given that X has two children, let Y be Inorder successor of X.
Which of the following is true about Y?
A
Y has no right child
B
Y has no left child
C
Y has both children
D
None of the above

Discuss it

Question 16
In a binary tree with n nodes, every node has an odd number of descendants. Every node is
considered to be its own descendant. What is the number of nodes in the tree that have exactly one
child?
A
0
B
1
C
(n-1)/2
D
n-1

Discuss it

Question 17
The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum
number of nodes in a binary tree of height h is:
A
2
h
1
B
2
h1
-1
C
2
h+1
-1
D
2
h+1

Discuss it

http://geeksquiz.com/data-structure/balanced-binary-search-trees/

Balanced Binary Search Trees
Question 1
The worst case running time to search for an element in a balanced in a binary search tree with n2^n
elements is
(A)
(B)
(C)
(D)
A
A
B
B
C
C
D
D

Discuss it

Question 2
What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a
single node is 0.
A
2
B
3
C
4
D
5

Discuss it

Question 3
What is the worst case possible height of AVL tree?
A
2Logn
Assume base of log is 2
B
1.44log n
Assume base of log is 2
C
Depends upon implementation
D
Theta(n)

Discuss it

Question 4
Which of the following is AVL Tree?
A
100
/ \
50 200
/ \
10 300

B
100
/ \
50 200
/ / \
10 150 300
/
5

C
100
/ \
50 200
/ \ / \
10 60 150 300
/ \ \
5 180 400
A
Only A
B
A and C
C
A, B and C
D
Only B

Discuss it

Question 5
Consider the following AVL tree.
60
/ \
20 100
/ \
80 120
Which of the following is updated AVL tree after insertion of 70
A
70
/ \
60 100
/ / \
20 80 120

B
100
/ \
60 120
/ \ /
20 70 80

C
80
/ \
60 100
/ \ \
20 70 120

D
80
/ \
60 100
/ / \
20 70 120
A
A
B
B
C
C
D
D

Discuss it

Question 6
Which of the following is a self-adjusting or self-balancing Binary Search Tree
A
Splay Tree
B
AVL Tree
C
Red Black Tree
D
All of the above

Discuss it

Question 7
Consider the following left-rotate and right-rotate functions commonly used in self-adjusting BSTs
T1, T2 and T3 are subtrees of the tree rooted with y (on left side)
or x (on right side)
y x
/ \ Right Rotation / \
x T3 - - - > T1 y
/ \ < - - - - - - - / \
T1 T2 Left Rotation T2 T3
Which of the following is tightest upper bound for left-rotate and right-rotate operations.
A
O(1)
B
O(Logn)
C
O(LogLogn)
D
O(n)

Discuss it

Question 8
Which of the following is true
A
The AVL trees are more balanced compared to Red Black
Trees, but they may cause more rotations during insertion and
deletion.
B
Heights of AVL and Red-Black trees are generally same, but
AVL Trees may cause more rotations during insertion and
deletion.
C
Red Black trees are more balanced compared to AVL Trees,
but may cause more rotations during insertion and deletion.
D
Heights of AVL and Red-Black trees are generally same, but
Red Black rees may cause more rotations during insertion and
deletion.

Discuss it

Question 9
Which of the following is true about Red Black Trees?
A
The path from the root to the furthest leaf is no more than
twice as long as the path from the root to the nearest leaf
B
At least one children of every black node is red
C
Root may be red
D
A leaf node may be red

Discuss it

Question 10
Which of the following is true about AVL and Red Black Trees?
A
In AVL tree insert() operation, we first traverse from root to
newly inserted node and then from newly inserted node to root.
While in Red Black tree insert(), we only traverse once from
root to newly inserted node.
B
In both AVL and Red Black insert operations, we traverse only
once from root to newly inserted node,
C
In both AVL and Red Black insert operations, we traverse
twiceL first traverse root to newly inserted node and then from
newly inserted node to root.
D
None of the above

Discuss it

Question 11
What is the worst case possible height of Red-Black tree? Assume base of Log as 2 in all options
A
2Log(n+1)
B
1.44 Logn
C
4Logn
D
None of the above

Discuss it

Question 12
Is the following statement valid? A Red-Black Tree which is also a perfect Binary Tree can have all
black nodes
A
Yes
B
No

Discuss it

Question 13
Which of the following operations are used by Red-Black trees to
maintain balance during insertion/deletion?

a) Recoloring of nodes
b) Rotation (Left and Right)
A
Only a
B
Only b
C
Both a and b
D
Neither a nor b

Discuss it

http://geeksquiz.com/data-structure/graph/

Graph
Question 1
representation of a graph?
A
In adjacency list representation, space is saved for sparse
graphs.
B
DFS and BSF can be done in O(V + E) time for adjacency list
representation. These operations take O(V^2) time in
adjacency list representation. Here is V and E are number of
vertices and edges respectively.
C
D
All of the above

Discuss it

Question 2
The degree sequence of a simple graph is the sequence of the degrees of the nodes in the graph in
decreasing order. Which of the following sequences can not be the degree sequence of any graph? I.
7, 6, 5, 4, 4, 3, 2, 1 II. 6, 6, 6, 6, 3, 3, 2, 2 III. 7, 6, 6, 4, 4, 3, 2, 2 IV. 8, 7, 7, 6, 4, 2, 1, 1
A
I and II
B
III and IV
C
IV only
D
II and IV

Discuss it

Question 3
The time complexity of computing the transitive closure of a binary relation on a set of n elements is
known to be:
A
O(n)
B
O(nLogn)
C
O(n ^ (3/2))
D
O(n^3)

Discuss it

Question 4
The most efficient algorithm for finding the number of connected components in an undirected graph
on n vertices and m edges has time complexity. (A) (n) (B) (m) (C) (m + n) (D) (mn)
A
A
B
B
C
C
D
D

Discuss it

Question 5
Consider an undirected unweighted graph G. Let a breadth-first traversal of G be done starting from a
node r. Let d(r, u) and d(r, v) be the lengths of the shortest paths from r to u and v respectively, in G. lf
u is visited before v during the breadth-first traversal, which of the following statements is correct?
(GATE CS 2001)
A
d(r, u) < d (r, v)
B
d(r, u) > d(r, v)
C
d(r, u) <= d (r, v)
D
None of the above

Discuss it

Question 6
How many undirected graphs (not necessarily connected) can be constructed out of a given set V= {V
1, V 2,V n} of n vertices ?
A
n(n-l)/2
B
2^n
C
n!
D
2^(n(n-1)/2)

Discuss it

Question 7
Which of the following statements is/are TRUE for an undirected graph? P: Number of odd degree
vertices is even Q: Sum of degrees of all vertices is even
A
P Only
B
Q Only
C
Both P and Q
D
Neither P nor Q

Discuss it

Question 8
Consider an undirected random graph of eight vertices. The probability that there is an edge between
a pair of vertices is 1/2. What is the expected number of unordered cycles of length three?
A
1/8
B
1
C
7
D
8

Discuss it

Question 9
Given an undirected graph G with V vertices and E edges, the sum of the degrees of all vertices is
A
E
B
2E
C
V
D
2V

Discuss it

http://geeksquiz.com/data-structure/tree-traversals/

Tree Traversals
Question 1
Following function is supposed to calculate the maximum depth or height of a Binary tree -- the
number of nodes along the longest path from the root node down to the farthest leaf node.
int maxDepth(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);

/* use the larger one */
if (lDepth > rDepth)
return X;
else return Y;
}
}
What should be the values of X and Y so that the function works correctly?
A
X = lDepth, Y = rDepth
B
X = lDepth + 1, Y = rDepth + 1
C
X = lDepth - 1, Y = rDepth -1
D
None of the above

Discuss it

Question 2
What is common in three different types of traversals (Inorder, Preorder and Postorder)?
A
Root is visited before right subtree
B
Left subtree is always visited before right subtree
C
Root is visited after left subtree
D
All of the above
E
None of the above

Discuss it

Question 3
The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The
postorder traversal of the binary tree is:
A
d e b f g c a
B
e d b g f c a
C
e d b f g c a
D
d e f g b c a

Discuss it

Question 4
What does the following function do for a given binary tree?
int fun(struct node *root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
return 1 + fun(root->left) + fun(root->right);
}
A
Counts leaf nodes
B
Counts internal nodes
C
Returns height where height is defined as number of edges on
the path from root to deepest node
D
Return diameter where diameter is number of edges on the
longest path between any two nodes.

Discuss it

Question 5
Which of the following pairs of traversals is not sufficient to build a binary tree from the given
traversals?
A
Preorder and Inorder
B
Preorder and Postorder
C
Inorder and Postorder
D
None of the Above

Discuss it

Question 6
Consider two binary operators ' ' and ' ' with the precedence of operator being lower than that of
the operator. Operator is right associative while operator is left associative. Which one of the
following represents the parse tree for expression (7 3 4 3 2)? (GATE CS 2011)

A
A
B
B
C
C
D
D

Discuss it

Question 7
Which traversal of tree resembles the breadth first search of the graph?
A
Preorder
B
Inorder
C
Postorder
D
Level order

Discuss it

Question 8
Which of the following tree traversal uses a queue data structure?
A
Preorder
B
Inorder
C
Postorder
D
Level order

Discuss it

Question 9
Which of the following cannot generate the full binary tree?
A
Inorder and Preorder
B
Inorder and Postorder
C
Preorder and Postorder
D
None of the above

Discuss it

Question 10
Consider the following C program segment
struct CellNode
{
struct CelINode *leftchild;
int element;
struct CelINode *rightChild;
}

int Dosomething(struct CelINode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if (ptr->leftChild != NULL)
value = 1 + DoSomething(ptr->leftChild);
if (ptr->rightChild != NULL)
value = max(value, 1 + DoSomething(ptr-
>rightChild));
}
return (value);
}
The value returned by the function DoSomething when a pointer to the root of a non-empty tree is
passed as argument is (GATE CS 2004)
A
The number of leaf nodes in the tree
B
The number of nodes in the tree
C
The number of internal nodes in the tree
D
The height of the tree

Discuss it

Question 11
Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and
preorder traversal. Respectively, of a complete binary tree. Which of the following is always true?
(GATE CS 2000)
A
LASTIN = LASTPOST
B
LASTIN = LASTPRE
C
LASTPRE = LASTPOST
D
None of the above

Discuss it

Question 12
The array representation of a complete binary tree contains the data in sorted order. Which traversal
of the tree will produce the data in sorted form?
A
Preorder
B
Inorder
C
Postorder
D
Level order

Discuss it

Question 13
Consider the following rooted tree with the vertex P labeled as root
The order in which the nodes are visited during in-
order traversal is
A
SQPTRWUV
B
SQPTURWV
C
SQPTWUVR
D
SQPTRUWV

Discuss it

Question 14
Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to
the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of
the tree is of type treeNode.
typedef struct treeNode* treeptr;
struct treeNode
{
treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
int value=0;
if (tree != NULL)
{
if (tree->leftMostChild == NULL)
value = 1;
else
value = DoSomething(tree->leftMostChild);
value = value + DoSomething(tree->rightSibling);
}
return(value);
}
When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned
by the function corresponds to the
A
number of internal nodes in the tree.
B
height of the tree.
C
number of nodes without a right sibling in the tree.
D
number of leaf nodes in the tree.

Discuss it

http://geeksquiz.com/algorithms/graph-shortest-paths/

Graph Shortest Paths
Question 1
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.

Graph
A
SDT
B
SBDT
C
SACDT
D
SACET

Discuss it

Question 2
To implement Dijkstras shortest path algorithm on unweighted graphs so that it runs in linear time,
the data structure to be used is:
A
Queue
B
Stack
C
Heap
D
B-Tree

Discuss it

Question 3
Dijkstras single source shortest path algorithm when run from vertex a in the below graph, computes
the correct shortest path distance to

A
only vertex a
B
only vertices a, e, f, g, h
C
only vertices a, b, c, d
D
all the vertices

Discuss it

Question 4
In an unweighted, undirected connected graph, the shortest path from a node S to every other node is
computed most efficiently, in terms of time complexity by
A
Dijkstras algorithm starting from S.
B
Warshalls algorithm
C
Performing a DFS starting from S.
D
Performing a BFS starting from S.

Discuss it

Question 5
Suppose we run Dijkstras single source shortest-path algorithm on the following edge weighted
directed graph with vertex P as the source. In what order do the nodes get included into the set of
vertices for which the shortest path distances are finalized? (GATE CS 2004)

A
P, Q, R, S, T, U
B
P, Q, R, U, S, T
C
P, Q, R, U, T, S
D
P, Q, T, R, U, S

Discuss it

Question 6
Suppose we run Dijkstras single source shortest-path algorithm on the following edge weighted
directed graph with vertex P as the source. In what order do the nodes get included into the set of
vertices for which the shortest path distances are finalized? (GATE CS 2004)

A
P, Q, R, S, T, U
B
P, Q, R, U, S, T
C
P, Q, R, U, T, S
D
P, Q, T, R, U, S

Discuss it

Question 7
What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete
graph of n vertices? (A) (B) (C) (D)
A
A
B
B
C
C
D
D

Discuss it

Question 8
In a weighted graph, assume that the shortest path from a source 's' to a destination 't' is correctly
calculated using a shortest path algorithm. Is the following statement true? If we increase weight of
every edge by 1, the shortest path always remains same.
A
Yes
B
No

Discuss it

Question 9
Following statement is true or false?
If we make following changes to Dijkstra, then it can be used to
find
the longest simple path, assume that the graph is acyclic.

1) Initialize all distances as minus infinite instead of plus
infinite.

2) Modify the relax condition in Dijkstra's algorithm to update
distance
of an adjacent v of the currently considered vertex u only
if "dist[u]+graph[u][v] > dist[v]". In shortest path algo,
the sign is opposite.
A
True
B
False

Discuss it

Question 10
Which of the following algorithm can be used to efficiently calculate single source shortest paths in a
Directed Acyclic Graph?
A
Dijkstra
B
Bellman-Ford
C
Topological Sort
D
Strongly Connected Component

Discuss it

Question 11
Given a directed graph where weight of every edge is same, we can efficiently find shortest path from
a given source to destination using?
A
B
Dijkstra's Shortest Path Algorithm
C
Neither Breadth First Traversal nor Dijkstra's algorithm can be
used
D
Depth First Search

Discuss it

Question 12
Is the following statement valid about shortest paths? Given a graph, suppose we have calculated
shortest path from a source to all other vertices. If we modify the graph such that weights of all edges
is becomes double of the original weight, then the shortest path remains same only the total weight of
path changes.
A
True
B
False

Discuss it

Question 13
Match the following

Group A Group B
a) Dijkstra's single shortest path algo p) Dynamic
Programming
b) Bellmen Ford's single shortest path algo q)
Backtracking
c) Floyd Warshell's all pair shortest path algo. r) Greedy
Algorithm
A
a-r, b-q, c-p
B
a-p, b-p, c-p
C
a-r, b-p, c-p
D
a-p, b-r, c-q

Discuss it

Question 14
Is the following statement valid?.

Given a weighted graph where weights of all edges are unique (no two edge have same weights),
there is always a unique shortest path from a source to destination in such a graph.
A
True
B
False

Discuss it

Question 15
Is the following statement valid?.

Given a graph where all edges have positive weights, the shortest paths produced by Dijsktra and
Bellman Ford algorithm may be different but path weight would always be same.
A
True
B
False

Discuss it

Question 16
Which of the following statement(s)is / are correct regarding Bellman-Ford shortest path algorithm?
P: Always finds a negative weighted cycle, if one exist s.
Q: Finds whether any negative weighted cycle is reachable
from the source.
A
P Only
B
Q Only
C
Both P and Q
D
Neither P nor Q

Discuss it

http://geeksquiz.com/algorithms/graph-minimum-spanning-tree/

Graph Minimum Spanning Tree
Question 1
An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,.vn. Two nodes vi , vj are
connected if and only if 0 < |i j| <= 2. Each edge (vi, vj ) is assigned a weight i + j. A sample graph
with n = 4 is shown below. What will be the cost of the minimum spanning tree (MST) of such a graph
with n nodes? (GATE CS 2011)
A
1/12(11n^2 5n)
B
n^2 n + 1
C
6n 11
D
2n + 1

Discuss it

Question 2
The length of the path from v5 to v6 in the MST of previous question with n = 10 is
A
11
B
25
C
31
D
41

Discuss it

Question 3
Consider a complete undirected graph with vertex set {0, 1, 2, 3, 4}. Entry Wij in the matrix W below is
the weight of the edge {i, j}. What is the minimum possible weight of a spanning tree T in this graph
such that vertex 0 is a leaf node in the tree T? (GATE CS 2010)
A
7
B
8
C
9
D
10

Discuss it

Question 4
In the graph given in above question question, what is the minimum possible weight of a path P from
vertex 1 to vertex 2 in this graph such that P contains at most 3 edges?
A
7
B
8
C
9
D
10

Discuss it

Question 5
An undirected graph G has n nodes. Its adjacency matrix is given by an n n square matrix whose (i)
diagonal elements are 0s and (ii) non-diagonal elements are 1s. which one of the following is TRUE?
A
Graph G has no minimum spanning tree (MST)
B
Graph G has a unique MST of cost n-1
C
Graph G has multiple distinct MSTs, each of cost n-1
D
Graph G has multiple spanning trees of different costs

Discuss it

Question 6
Consider the following
graph:
Which one of the following cannot be the sequence of edges added, in that order, to a minimum
spanning tree using Kruskals algorithm?
A
(ab),(df),(bf),(dc),(de)
B
(ab),(df),(dc),(bf),(de)
C
(df),(ab),(dc),(bf),(de)
D
(df),(ab),(bf),(de),(dc)

Discuss it

Question 7
Let G be an undirected connected graph with distinct edge weight. Let emax be the edge with
maximum weight and emin the edge with minimum weight. Which of the following statements is false?
(GATE CS 2000)
A
Every minimum spanning tree of G must contain emin
B
If emax is in a minimum spanning tree, then its removal must
disconnect G
C
No minimum spanning tree contains emax
D
G has a unique minimum spanning tree

Discuss it

Question 8
Consider a weighted complete graph G on the vertex set {v1,v2 ,v} such that the weight of the edge
(v,,v) is 2|i-j|. The weight of a minimum spanning tree of G is: (GATE CS 2006)
A
n 1
B
2n 2
C
nC2
D
2

Discuss it

Question 9
Let G be a weighted graph with edge weights greater than one and G'be the graph constructed by
squaring the weights of edges in G. Let T and T' be the minimum spanning trees of G and G',
respectively, with total weights t and t'. Which of the following statements is TRUE?
A
T' = T with total weight t' = t
2

B
T' = T with total weight t' < t
2

C
T' != T but total weight t' = t
2

D
None of the above

Discuss it

Question 10
Consider the following graph: Which one of the
following is NOT the sequence of edges added to the minimum spanning tree using Kruskal's
algorithm?
A
(b,e)(e,f)(a,c)(b,c)(f,g)(c,d)
B
(b,e)(e,f)(a,c)(f,g)(b,c)(c,d)
C
(b,e)(a,c)(e,f)(b,c)(f,g)(c,d)
D
(b,e)(e,f)(b,c)(a,c)(f,g)(c,d)

Discuss it

Question 11
The number of distinct minimum spanning trees for the weighted graph below is
____
A
4
B
5
C
6
D
7

Discuss it

http://geeksquiz.com/algorithms/graph-traversals/

Graph Traversals
Question 1
Which of the following algorithms can be used to most efficiently determine the presence of a cycle in
a given graph ?
A
Depth First Search
B
C
Prim's Minimum Spanning Tree Algorithm
D
Kruskal' Minimum Spanning Tree Algorithm

Discuss it

Question 2
Traversal of a graph is different from tree because
A
There can be a loop in graph so we must maintain a visited flag
for every vertex
B
DFS of a graph uses stack, but inorrder traversal of a tree is
recursive
C
BFS of a graph uses queue, but a time efficient BFS of a tree is
recursive.
D
All of the above

Discuss it

Question 3
What are the appropriate data structures for following algorithms?
2) Depth First Search
3) Prim's Minimum Spanning Tree
4) Kruskal' Minimum Spanning Tree
A
1) Stack
2) Queue
3) Priority Queue
4) Union Find
B
1) Queue
2) Stack
3) Priority Queue
4) Union Find
C
1) Stack
2) Queue
3) Union Find
4) Priority Queue
D
1) Priority Queue
2) Queue
3) Stack
4) Union Find

Discuss it

Question 4
The Breadth First Search algorithm has been implemented using the queue data structure. One
possible order of visiting the nodes of the following graph is

A
MNOPQR
B
NQMPOR
C
QMNPRO
D
QMNPOR

Discuss it

Question 5
Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-
first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting
u in the traversal. Which of the following statements is always true? (GATE CS 2000)
A
{u,v} must be an edge in G, and u is a descendant of v in T
B
{u,v} must be an edge in G, and v is a descendant of u in T
C
If {u,v} is not an edge in G then u is a leaf in T
D
If {u,v} is not an edge in G then u and v must have the same
parent in T

Discuss it

Question 6
Consider the following graph
Among the following sequences
I) a b e g h f
II) a b f e h g
III) a b f h g e
IV) a f g h b e Which are depth first traversals of the above graph? (GATE CS 2003)
A
I, II and IV only
B
I and IV only
C
II, III and IV only
D
I, III and IV only

Discuss it

Question 7
Make is a utility that automatically builds executable programs and libraries from source code by
reading files called makefiles which specify how to derive the target program. Which of the following
standard graph algorithms is used by Make.
A
Strongly Connected Components
B
Topological Sorting
C
D
Dijkstra's Shortest Path

Discuss it

Question 8
Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can be used to find
if there is path from s to t?
A
Only BFS
B
Only DFS
C
Both BFS and DFS
D
Neither BFS nor DFS

Discuss it

Question 9
Which of the following condition is sufficient to detect cycle in a directed graph?
A
There is an edge from currently being visited node to an
B
There is an edge from currently being visited node to an
ancestor of currently visited node in DFS forest.
C
Every node is seen twice in DFS.
D
None of the bove

Discuss it

Question 10
Is following statement true/false If a DFS of a directed graph contains a back edge, any other DFS of
the same graph will also contain at least one back edge.
Source:http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-s2009-sol.pdf
A
True
B
False

Discuss it

Question 11
Is following statement true/false? A DFS of a directed graph always produces the same number of
tree edges, i.e., independent of the order in which vertices are considered for DFS.
(Sourcehttp://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2008-sol.pdf)
A
True
B
False

Discuss it

Question 12
If the DFS finishing time f[u] > f[v] for two vertices u and v in a directed graph G, and u and v are in
the same DFS tree in the DFS forest, then u is an ancestor of v in the depth first tree.
(Source http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2007-sol.pdf)
A
True
B
False

Discuss it

Question 13
Consider the DAG with Consider V = {1, 2, 3, 4, 5, 6}, shown below. Which of the following is NOT a
topological ordering?
A
1 2 3 4 5 6
B
1 3 2 4 5 6
C
1 3 2 4 6 5
D
3 2 4 1 6 5

Discuss it

Question 14
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time
on Depth First Search of G? Assume that the graph is represented using adjacency matrix.
A
O(n)
B
O(m+n)
C
O(n
2
)
D
O(mn)

Discuss it

Question 15
Consider the tree arcs of a BFS traversal from a source node W in an unweighted, connected,
undirected graph. The tree T formed by the tree arcs is a data structure for computing.
A
the shortest path between every pair of vertices.
B
the shortest path from W to every vertex in the graph.
C
the shortest paths from W to only those nodes that are leaves
of T.
D
the longest path in the graph

Discuss it

Question 16
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume
that a recursive call to visit a vertex is made only after first checking that the vertex has not been
visited earlier. Then the maximum possible recursion depth (including the initial call) is _________.

A
17
B
18
C
19
D
20

Discuss it

Question 17
Let T be a depth first search tree in an undirected graph G. Vertices u and n are leaves of this tree T.
The degrees of both u and n in G are at least 2. which one of the following statements is true?
A
There must exist a vertex w adjacent to both u and n in G
B
There must exist a vertex w whose removal disconnects u and
n in G
C
There must exist a cycle in G containing u and n
D
There must exist a cycle in G containing u and all its
neighbours in G.

Discuss it

http://mcqquestion.blogspot.in/2012/08/data-structures-and-algorithm-analysis.html

1. If all c(i, j )s and r(i, j)s are calculated, then OBST algorithm in worst case
takes one of the following time.
(a) O(n log n)
(b) O(n
3
)
(c) O(n
2
)
(d) O(log n)
(e) O(n
4
).
2. The following is a weighted binary tree, then what is the weighted array for
the TVS problem?

(a) [9, 2, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 4]
(b) [9, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 4, 6]
(c) [9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 7, 4]
(d) [9, 2, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 6, 4]
(e) [9, 2, 0, 0, 0, 7, 0, 0, 0, 0, 6, 4, 0, 0].
3. What are the entries of the array TREE[ ] for the above weighted binary tree
for the TVS problem.
(a) [1, 2, 3, 4, 5, 6, 0, 0]
(b) [1, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6]
(c) [1, 2, 3, 0, 0, 0, 0, 4, 5, 6]
(d) [1, 2, 3, 0, 0, 0, 0, 0, 4, 5, 6]
(e) [1, 2, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 6].
4. For a 15-puzzle problem let the initial arrangement be the following one, then
answer the questions 4 7 with the following arrangement.
10 13 15 7
9 1 4 14
12 8 6
11 2 5 3

What is the value of x used to find the reachability of the solution?
(a) 1
(b) 0
(c) 8
(d) 10
(e) 13.
5. The values for the position(i), position(j) where i = 14 and j =11, are
(a) 2, 8
(b) 8, 2
(c) 8, 13
(d) 2, 13
(e) 13, 8.
6. The values for less(i), less(j) where i =5, j =7 are
(a) 0, 6
(b) 6, 0
(c) 2, 4
(d) 2, 6
(e) 1, 6.
7. What is the value to find the reachability of the problem with which you can
say the solution is reachable or not?
(a) 71
(b) 72
(c) 73
(d) 69
(e) 68.
8. The upper bound on the time complexity of the nondeterministic sorting
algorithm is
(a) O(n)
(b) O(n log n)
(c) O(1)
(d) O( log n)
(e) O(n
2
).
9. The worst case time complexity of the nondeterministic dynamic knapsack
algorithm is
(a) O(n log n)
(b) O( log n)
(c) O(n
2
)
(d) O(n)
(e) O(1).
10. For the LCBB solution of knapsack problem with the data (p1p4) =
(10,10,12,18) and (w1w4) = (2, 4, 6, 9) and m = 18, then the values of u(1)
and (1) respectively are
(a) -38, -44
(b) -44, -38
(c) -44, -32
(d) -32, -44
(e) -32, -38.

Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set 11 Set 12 Set 13 Set
14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set 21 Set 22 Set 23 Set 24 Set 25 Set
26
Set 27 Set 28 Set 29 Set 30

Reason: As we have to calculate c(i,j), w(i,j) and r(i,j).
Reason: If we create a full binary tree then the non existing nodes will have the
corresponding edge values to zero.
Reason: If we create a full binary tree then the non existing nodes will have the
corresponding node values to zero.
Reason: x takes the value 0 if it is there in shaded position which starts from second
square and goes across every alternative square in the initial arrangement,
otherwise it takes one. In this question it is there in shaded square.
Reason: position( x ) = the square number with in which value x is there.
Reason: less( x ) is the number of all js, such that j < x and position( j ) >
position(x).
Reason: The value to define reachable is sum of less(i) + x, where i takes the values
from 1 to 15, and x is value defined in question(4).
Reason: In the algorithm there is only one for loop which runs from 1 to n.
Reason: In the algorithm there is only one for loop which runs from 1 to n.
Reason: u(1) is the negation of sum of all profits so long as the weights as whole can be
taken [ -(10+10+12)]. (1) is the value of u(1) and the negation of profit of the
fraction of the next element taken to fill the knapsack [ u(1) + -( 6/9*18) ].

DATA STRUCTURE AND ALGORITHM ANALYSIS SET 2
Data Structure and Algorithm Analysis
Questions 11 To 20

11. If for the above problem, the tree is created following the FIFOBB, then the
values for (5) and u(5) respectively are
(a) -40, -40 (b) -40, -38 (c) -40, -32 (d) -22, -
32 (e) -32, -22.
12. There is a chain of 20 stores; each of the store having 5 departments. Then,
which of the following is a correct representation of array size?
(a) 5 * 20 (b) 6 * 21 (c) 20 * 5 (d) 21 *
6 (e) 19 * 4.
13. From the following, select the valid data structure which reflects hierarchical
relationship between the elements.
(a) Graph (b) Queue (c) Linked list (d)
Stack (e) Tree.
14. Which of the following matrix does have high proportions of zero entries as
the elements?
(a) Inverse Matrix (b) Sparse Matrix (c)
Determinant Matrix
(d) Square Matrix (e) Transpose Matrix.
15. Let there is a queue whose initial values for Front and Rear are 0 and 1
respectively. Later queue found to be having the elements 3, 2, 1, 5. If a new
element is inserted in the queue, then which of the following statements is
correct?
(a) Front = 0 and Rear = 5
(b) Front = 0 and Rear = 4
(c) Front = 1 and Rear = 4
(d) Front = 1 and Rear = 5
(e) Insertion is not possible as there is no space in the queue.
16. One from the following is also known as Pre-Order traversal. What that one
is?
(a) LNR (b) LRN (c) RLN (d)
RNL (e) NLR.
17. Which of the following data structure is the best example for the multitasking
systems?
(a) Tree (b) Queue (c) Stack (d) Linked
List (e) Graph.
18. The initial configuration of the queue is having the elements x, y, z, a forming
a queue. To get the new configuration a, z, y, x one needs a minimum of
(a) 3 additions and 4 deletion (b) 3 deletions and 4 additions
(c) 3 deletions and 2 additions (d) 3 deletions and 3 additions
(e) 4 deletions and 4 additions.
19. To form the new configuration in the above problem, what best data structure
is seems to be used?
(a) Tree (b) Queue (c) Linked List (d) Circular
Queue (e) Stack.
20. Recursive algorithms are based on
(a) Divide and conquer approach (b) Top-down approach
(c) Bottom-up approach (d) Hierarchical approach

(e) Heuristic approach.

Reason: Node 5 is a node generated if first element is taken and the second object is rejected. And just
calculate as question(10).
Reason: Its array [20][5] and hence 20 * 5.
Reason: Tree, according to the organization of tree a node may have a child and a parent
also.
Reason: As sparse matrix is a matrix where the number of zeros are much than that of non zero
elements.
Reason: As a new element is inserted at a position pointed by Rear, And Rear is incremented after
insertion.
Reason: As in Pre-Order the traverse is as Node( root node), then Left node and Right node.
Reason: Because a queue is used to hold the jobs which are waiting for their turn to work with the
common processor.
Reason: Because, first we have to delete x, y, z and then insert z, y, x. And hence three deletions
Reason: As in the above discussion x is deleted first but inserted at last. Hence there should be a
stack to hold the deleted entries from above queue.
Reason: As in case of recursions the ARIs are created, and the recursive function is called by other
function first, then it calls itself util a condition is met.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

DATA STRUCTURE AND ALGORITHM ANALYSIS SET 3
Data Structure and Algorithm Analysis
Questions 21 To 30

21. What do you call the selected keys in the quick sort method?
(a) Outer key (b) Inner Key (c) Partition key
(d) Pivot key (e) Recombine key.
22. Primary clustering occurs in
(a) Linear probing (b) Quadratic probing
(c) Mid-square method (d) Order probing (e)
Chaining.
23. How many nodes do a full binary tree with N leaves contain?
(a) 2N nodes (b) N nodes (c) 2N-1 nodes
(d) N-1 nodes (e) 2(N-1) nodes.
24. How many nodes does a complete binary tree of level 5 have?
(a) 16 (b) 15 (c) 32 (d) 31 (e) 64.
25. How do you determine the cost of a spanning tree?
(a) By the sum of the costs of the edges of the tree
(b) By the sum of the costs of the edges and vertices of the tree
(c) By the sum of the costs of the vertices of the tree
(d) By the sum of the costs of the edges of the graph
(e) By the sum of the costs of the edges and vertices of the graph.
26. What would be the depth of tree whose level is 9?
(a) 10 (b) 8 (c) 9 (d) 11 (e) 7.
27. A node of a directed graph G having no out-degree and a positive in-degree
is called
(a) Source node (b) Sink node (c) Sibling node
(d) Null node (e) In-node.
28. The time complexity of the normal quick sort, randomized quick sort
algorithms in the worst case is
(a) O(n
2
), O(n log n) (b) O(n
2
), O(n
2
)
(c) O(n log n), O(n
2
) (d) O(n log n), O(n log n)
(e) O(n log n), O(n
2
log n).
29. Let there be an array of length N, and the selection sort algorithm is used to
sort it, how many times a swap function is called to complete the execution?
(a) N log N times (b) log N times (c) N
2
times
(d) N-1 times (e) N times.
30. The Sorting method which is used for external sort is
(a) Bubble sort (b) Quick sort (c) Merge sort
(d) Radix sort (e) Selection sort.

Reason: As in quick sort the division is made at an element which is pivotal element.
Reason: As there is no chance for the other probing techniques to have primary clustering.
Reason: According to full binary tree, if there are 2n-1 nodes, then there would be n leaves
exactly.
Reason: This is 2
5
1. (2
level
1)
Reason: As the other options are wrong, and the cost of a spanning tree is the cost of edges of the tree
only.
Reason: As both the depth and level are same.
Reason: Because the source vertex is a vertex which contains only leaving edges, but no any incoming
edge.
Reason: Because, there is no difference between normal quick sort and randomized quick sort
algorithms for their efficiency, except the picking of a pivotal element.
Reason: Because, every time we swap the i
th
smallest element with the i
th
position. And we do this for
i=1 to n-1 times.
Reason: As Merge sort is the only one from the given sorting techniques which is used for the external
sorting.
DATA STRUCTURE AND ALGORITHM ANALYSIS SET 4
Data Structure and Algorithm Analysis
Questions 31 To 40

31. In analysis of algorithm, approximate relationship between the size of the job
and the amount of work required to do is expressed by using _________
(a) Central tendency
(b) Differential equation
(c) Order of execution (d) Order of magnitude
(e) Order of Storage.
32. Pick the correct prefix form to the given infix expression
{a*[b/(c-d)*f]/g}/[e+h]

(a) //*a/b*-cdfg+ch (b) abcd-f*/g/*eh+/
(c) //*a*/b-cdfg+eh (d) //*ab*/-cdfg+eh
(e) -//*a*/bcdfg+eh.
33. For recursive MinMax algorithm, the average, worst cases are __________
(a) (b)
(c) (d)
(e)

34. P, Q and R are pointer variables. The statements below are intended to swap
the contents of the nodes pointed to by P and Q. rewrite it so that it will work
as intended.
P = Q; R = Q; Q = R;
(a) R=Q; P=R; Q=R; (b) R=P; P=P; Q=Q;
(c) P=P; P=Q; R=Q; (d) R=P; P=Q; Q=R;
(e) P=R; R=Q; Q=R;
35. For defining the best time complexity, let f (n) = log n and g (n) = n,
_________
(a) f (n) e(g(n)), but g(n) e (f(n)) (b) f (n) e(g(n)), but g(n) e
(f(n))
(c) f (n) e(g(n)), and g(n) e (f(n)) (d) f (n) e(g(n)), and g(n) e
(f(n))
(e) Cant be defined for best case.
36. If f, r are the front, rear pointers respectively in a circular queue then the
condition (( f r + size) % size == 1) && ( (f != 0) || (r != -1) ) represents
(a) Queue has only one element (b) Two
Elements
(c) More than 2 elements but not full (d) Impossible
values
(e) Queue is full.
37. For the following two statements, choose the correct answer.
I. n
2
eO(n
3
).
II. n = ((n+1)!).

(a) (I) and (II) are TRUE
(b) (I), (II) are FALSE
(c) (I) is FALSE, but (II) is TRUE
(d) (I) is TRUE and (II) is FALSE
(e) (I) is TRUE but (II) cant be defined.
__________ node.
(a) 2
nd
(b) 4
th
(c) 5
th
(d) 3
rd

(e) first.
39. For analyzing an algorithm, which is better computing time?
(a) O (100 Log N) (b) O (N)
(c) O (2
N
) (d) O (N log N)
(e) O (N
2
).
40. To implement the Round Robin algorithm, which of the following data structure
is used?
(a) Stack (b) Linear Queue

(c) Circular Queue (d) Priority Queue
(e) Double Stack.

Reason : The order of the magnitude is the approximate relationship between the size
of the job and the amount of work required to do. All the other options are not
Reason : According to the priority of the operators ( as first inner round brackets, then the inner square
brackets and hence so on.
Reason : According to the time analysis of the algorithm
Reason : According to the pointer manipulations of the variables
Reason : According to the limit rule for the omega notation ( Using the duality rule )
Reason : This is the one of the conditions to define the circular queue is full. All other are invalid
choices.
Reason : According to the limit rule for the big Oh notation and the theta notation
Reason : As the are only three nodes, and the list is pointed by head.
Reason : As this is a very small value when compared with all the other values.
Reason : According to the concepts of OS at process scheduling, circular queue is the best suitable one
when compared with all the remaining one.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

DATA STRUCTURE AND ALGORITHM ANALYSIS SET 5
Data Structure and Algorithm Analysis
Questions 41 To 50

41. Let f, t: NR > 0, and t (n) eO (f (n)) iff t(n) c.f (n) where c is positive real
constant and
n n
o
, then n
o
is ___________
(a) Upper bound (b) Lower bound
(c) Duality value (d) Threshold value (e)
Maximum value.
42. In a circular queue, we can disambiguate empty from full queue by
(a) Using a gap in the array
(b) Incrementing queue positions by 2 instead of 1
(c) Keeping a count of the number of elements
(d) (a) and (c) above
(e) (a), (b), and (c) above.
43. In a graph, a vertex with degree one is known as ___________
(a) Pendant vertex (b) Leaf
(c) Root (d) End vertex
(e) Internal.
44. Consider the usual algorithm for determining whether a sequence of
parentheses is balanced. What is the maximum number of parentheses that
will appear on the stack AT ANY ONE TIME when the algorithm
analyzes: (()(())(()))
(a) 1 (b) 2 (c) 3 (d) 4 (e)
5 or more.
45. If a graph G = (N, E), where E= {|}, then the corresponding adjacency matrix
is _____
(a) Unit Matrix
(b) Identity Matrix
(c) Having Diagonal elements Zero
(d) Matrix with all 1s
(e) Zero Matrix.
46. Suppose we have an array implementation of the stack class, with ten items in
the stack stored at data[0] through data[9]. The SIZE is 42. Where does the
push method place the new entry in the array?
(a) data[0] (b) data[1] (c) data[9] (d) data[10]
(e) data[11].
(a) Scans each incident node along with its children.
(b) Scans all incident edges before moving to other node.
(c) Is same as backtracking
(d) Scans all the nodes in random order.
(e) Scans all the nodes in pre-order manner.
48. Provided the space is available, then to insert an element in the queue, we
can use for the following structure
struct queue
{
int Q[20];
int f, r;
}Q;

(a) ++Q.Q[Q.r] = x; (b) Q.Q[++Q.r] =
x; (c) Q.Q[Q.r]++ = x;
(d) Syntax error (e) Q.Q[Q.r++] = x;
49. Which method of traversal does not use stack to hold nodes that are waiting to
be processed?
(a) Dept First (b) D-search (c) Breadth first
(d) Back-tracking (e) Bounding.
50. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then
removed one at a time, in what order will they be removed?
(a) ABCD (b) ABDC (c) DCAB (d) DCBA
(e) ACDB.

Reason : According to the mathematical definition of the big Oh notation, this is the Threshold value
defines the upper limit on the size of the instance.
Reason : The option B is not the suitable one and not correct also, while the other two(a, c ) can be
used

Reason : According to the definition of the graph, and the degrees of the vertex.
Reason : Use stack and increment counter by one if opening bracket is encountered, and decrement it by
one if a closing bracket is encountered.
Reason : As E = empty set, means there is no any edge between any two set of vertices. And hence it is
zero matrix.
Reason : As it is Stack always the items are pushed at the top of the stack if there is any space. Also
data[10] is the next space as data[0] to data[9] are already filled.
Reason : As BFS always make use of the Queue.
Reason : Because first we have to increment the rear pointer and at that place we can insert an element.
Reason : As D search, DFS uses stack, so BFS only uses queue and not the stack.
Reason : As Queue follows FIFO structure.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>
DATA STRUCTURE AND ALGORITHM ANALYSIS SET 6
Data Structure and Algorithm Analysis
Questions 51 To 60

51. The Knapsack problem where the objective function is to minimize the profit is
______
(a) Greedy (b) Dynamic 0 / 1 (c) Back tracking
(d) Branch & Bound 0/1 (e) NP Knapsack.
52. I have implemented the queue with a linked list, keeping track of a front node
and a rear node with two reference variables. Which of these reference
variables will change during an insertion into a NONEMPTY queue?
(a) Neither changes (b) Only front changes (c) Only rear changes
(d) Both change (e) (a) or (d) above.
53. For the given 5 jobs with penalties 6,3,4,8,5 respectively, the static state space
tree is generated, then if the first job is selected but not the second job for the
first two jobs consideration, then the rank of such situational node is ______
(a) 6 (b) 3 (c) 0 (d) 9 (e) 20.
54. Which of the following statements is/are TRUE?
i) ADTs define how data is stored and manipulated.
ii) Every linked list has to have at least one external pointer.
iii) Recursive solutions may be easier to understand.

(a) (i),(ii) and (iii) above (b) Only (i) above
(c) Only (ii) above (d) Only (iii)above
(e) Only (ii) and (iii) above.
55. Choose the correct answer for the following statements:
I. The theory of NPcompleteness provides a method of obtaining a
polynomial time for NPalgorithms.
II. All NP-complete problem are NP-Hard.

(a) I is FALSE and II is TRUE
(b) I is TRUE and II is FALSE
(c) Both are TRUE
(d) Both are FALSE
(e) II is TRUE, I cant be determined.
56. Having the address of the node to be deleted in doubly linked list, the node
can be deleted

(a) Without traversing the list
(b) Only after traversing the list from the head
(c) Only after traversing the list from the tail
(d) (b) or (c) above
(e) Cant be deleted.
57. The Hamiltonian cycles problem uses the following line of code to generate a
next vertex, provided x[ ] is a global array and k
th
vertex is under
consideration:
(a) x[k] (x[k] + 1) mod n (b) x[k] (x[k]) mod (n)
(c) x[k] (x[k] + 1) mod (n+1) (d) x[k] x[k+1] mod n
(e) x[k] x[k+1] mod (n + 1)
58. Suppose cursor refers to a node in a linked list, what statement changes
cursor so that it refers to the next node?
(a) cursor++;
(e) (a) and (c) above.

(Note:
struct cursor {
int data;
};
59. The graph coloring algorithms time can be bounded by _________
(a) O(mn
m
) (b) O(n
m
) (c) O(n
m
. 2
n
) (d) O(m
n
.2
n
) (e) O(nm
n
).
60. For 0/1 KNAPSACK problem, the algorithm takes ________ amount of time
for memory table, and ______time to determine the optimal load, for N objects
and W as the capacity of KNAPSACK.
(a) O(N+W), O(NW) (b) u(NW), O(N+W)
(c) O(N), O(NW) (d) O(NW), O(N) (e) O(N+W),
O(N+W).

Reason : As there for the generation of the state space tree we use minimization Of the profit.
Reason : As the queue is non empty and there is rear pointer pointing, this only must be
incremented.
Reason : As we have considered first two jobs and have not considered the second job so we have to
pay the penalty of second job.
Reason : As the other two options are not true and correct.
Reason : Because the theory of NP does not provides a way to obtain a polynomial time for NP
algorithms. Hence the I statement is wrong while the II statement is correct.
Reason : That is the advantage of doubly linked list. No need to traverse if the address of the desired
node is provided.
Reason : As to find the next node to be traversed this is the formula.
Reason : All the other options are invalid operations.
Reason : As all the n nodes are required to be colored, and there are m
n
ways of coloring a specific node.
Reason : As the memory table ( 2 D matrix ) contains N rows and W columns, hence it is NW, and for
the composition of the optimal load it is up to N+W time it takes.

61. List out the areas in which data structures are applied extensively.
(a) Compiler Design
(b) Operating System
(c) Database Management System
(d) Both (a) and (b) above
(e) (a), (b) and (c) above.

62. What is the major data structure used in the Network data model?
(a) Stack
(b) Array
(c) Tree
(d) Graph
(e) Queue.

63. The data structure used in the Hierarchical data model is
(a) Array
(b) Tree
(c) Graph
(d) Stack
(e) Queue.

64. Minimum number of queue(s) needed to implement the priority queue is(are)
(a) Two
(b) One
(c) Three
(d) Four
(e) Depends upon the application.

65. What data structure is used to perform recursion?
(a) Queue
(c) Stack
(e) Circular Queue.

66. For the expression ((A + B) * C (D E) ^ (F + G)), the equivalent Postfix
notation is
(a) AB + C * DE - - ^ FG +
(b) AB + CDE - - * FG + ^
(c) AB + C * DE - FG + - ^
(d) AB + C * DE - - FG + ^
(e) AB + C - DE - * FG + ^.

67. The Prefix notation for the above infix expression is
(a) - * +ABC - DE ^ + FG
(b) ^ - * +ABC - DE + FG
(c) ^ - +AB * C - DE + FG
(d) ^ - + * ABC - DE + FG
(e) - ^ * + ABC - DE + FG.

68. Sorting is not possible by using which of the following methods?
(a) Insertion
(b) Selection
(c) Deletion
(d) Exchange
(e) Partitioning.

69. The number of null branches for a binary tree with 20 nodes is
(a) 21
(b) 20
(c) 22
(d) 19
(e) 18.

70. What is the type of the algorithm used in solving the 8 Queens problem?
(a) Greedy
(b) Dynamic
(c) Branch and Bound
(d) Divide and Conquer
(e) Backtracking.

Reason : As the data structure can be used in all the application areas right from the
compiler design to operating systems and even in database management
system design.
Reason : As the graph is the suitable data structure to represent the connectivity
between the nodes as edges.
Reason : As tree data structure is used to represent the Hierarchical relationship
between the nodes as parent and children.
Reason : Two : One queue is used for actual storing of data and another for storing
priorities.
Reason : Because of its LIFO (Last In First Out) property it remembers its caller so
knows whom to return when the function has to return. Recursion makes use
of system stack for storing the return addresses of the function calls.
Reason : As per the precedence and associativity of the operators, brackets are
evaluated first and then multiplication operators followed by addition,
subtraction operators.
Reason : As per the precedence and associativity of the operators, brackets are
evaluated first and then multiplication operators followed by addition,
subtraction operators.
Reason : Using insertion we can perform insertion sort, using selection we can
perform selection sort, using exchange we can perform the bubble sort (and
other similar sorting methods) and using the portioning we can perform
merge sort(and other similar sorting methods) But no sorting method can be
done just using deletion.
Reason : A binary tree with n nodes has exactly n+1 null nodes.
Reason : As the other types of algorithms are not suitable to find the solution for the
given problem.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

1 comment :
1.
KalyanDecember 14, 2013 at 3:24 AM

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

Data Structure and Algorithm Analysis
Questions 91 To 100
DATA STRUCTURE AND ALGORITHM ANALYSIS SET 8
Data Structure and Algorithm Analysis
Questions 71 To 80

71. The Following is a binary tree. Answer questions (11 13) considering the
below given tree.

The In-order traversal of the above tree is
(a) H D E B F I J G C A
(b) D H B E A F C I G J
(c) A B D H E C F G I J
(d) H D B E A F C I G J
(e) D H B E A I G J F C.

72. The Pre-order traversal for the tree in question -11 is
(a) A B D H E C F G J I
(b) A B D E H C F G I J
(c) D H B E A F C I G J
(d) A B D H E C F G I J
(e) H D E B F I J G C A.

73. The Post-order traversal for the tree in question -11 is
(a) H D E B I J G F C A
(b) D H B E A F C I G J
(c) H D E B F I J G C A
(d) H D E B F I J G A C
(e) A B D H E C F G I J.

74. There are four trees named A, B, C and D having 8, 15, 13, 14 nodes in them
respectively. Which of them could have formed a full binary tree?
(a) A
(b) B
(c) C
(d) D
(e) No any tree can be a full binary tree.

75. In the given binary tree if the nodes are stored in an array, then where the
node 4 can be stored?

(a) At location 6
(b) At location 5
(c) At location 4
(d) At location 3
(e) Cant be stored at any location.

76. Of the following tree structure, which is efficient, considering space and time
complexities?
(a) Incomplete Binary Tree
(b) Complete Binary Tree
(c) Full Binary Tree
(d) Binary Search Tree
(e) AVL Tree.

77. Consider the following two statements and choose the correct option:
I. According to Access strategies Linked List is a linear one.
II. According to Storage Linked List is a Non-linear one.
(a) (I) is true but (II) is false
(b) (I) is false but (II) is true
(c) Both (I) and (II) are true
(d) Both (I) and (II) are false
(e) Cant be determined.

78. Which of the following are differences between structures and arrays?
(a) Structures and arrays use different operators to access their elements
(b) Structures are not limited to containing variables of a single data type
(c) Structures can contain elements that are pointers but arrays not
(d) Structures are passed by value while arrays are passed by reference
(e) Structures and arrays contains the different elements.

79. Which of the following pairs of statements are identical?
(a) (*ptr) element AND ptr.element
(b) *ptr.element AND ptrelement
(c) *ptrelement AND ptr.element
(d) *(ptr.element) AND ptrelement
(e) (*ptr).element AND ptrelement.

80. A "stack" is also known as what?
(a) A FIFO
(b) A Queue
(c) A LIFO
(e) A FOLI.

Reason : Because the rules for traversing the binary tree in In-order are:
i. Traverse the left sub tree in In-order
ii. Traverse the root
iii. Traverse the right sub tree in In-order
Reason : Because the rules for traversing the binary tree in Pre-order are:
i. Traverse the root
ii. Traverse the left sub tree in Pre-order
iii. Traverse the right sub tree in Pre-order
Reason : Because the rules for traversing the binary tree in Post-order are:
i. Traverse the left sub tree in Post-order
ii. Traverse the right sub tree in Post-order
iii. Traverse the root
Reason : In general there are 2
n
-1 nodes in a full binary tree.
By the method of elimination: Full binary trees contain odd number of nodes.
So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13
nodes you can form a complete binary tree but not a full binary tree. So the
Reason : At location 6
1 2 3 - - 4 - - 5
Root
1
LC1
2
RC1
3
LC2
4
RC2
5
LC3
6
RC3
7
LC4
8
RC4
9
where LCn means Left Child of node n and RCn means Right Child of
node n. Top row represents the node values and bottom row represents the
positions.
Reason : Complete Binary Tree.
By the method of elimination: Full binary tree loses its nature when
operations of insertions and deletions are done. For incomplete binary trees,
extra storage is required and overhead of NULL node checking takes place.
So complete binary tree is the better one since the property of complete
binary tree is maintained even after operations like additions and deletions
are done on it
Reason : As the linked list nodes do not have any index and hence they must be
accessed in linear order always. Also they are stored in discrete locations
and not in contiguous memory locations.
Reason : As this is the most suitable option when compared to the other options. Also
option c, e are wrong.
Reason : As both the operators [ and (*). ] are used to access the members of the
structure through pointers. All the other options have the syntax errors.
Reason : As the Stack behaves in Last In First Out manner.

Reason : Because the rules for traversing the binary tree in In-order are:
i. Traverse the left sub tree in In-order
ii. Traverse the root
iii. Traverse the right sub tree in In-order
Reason : Because the rules for traversing the binary tree in Pre-order are:
i. Traverse the root
ii. Traverse the left sub tree in Pre-order
iii. Traverse the right sub tree in Pre-order
Reason : Because the rules for traversing the binary tree in Post-order are:
i. Traverse the left sub tree in Post-order
ii. Traverse the right sub tree in Post-order
iii. Traverse the root
Reason : In general there are 2
n
-1 nodes in a full binary tree.
By the method of elimination: Full binary trees contain odd number of nodes.
So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13
nodes you can form a complete binary tree but not a full binary tree. So the
Reason : At location 6
1 2 3 - - 4 - - 5
Root
1
LC1
2
RC1
3
LC2
4
RC2
5
LC3
6
RC3
7
LC4
8
RC4
9
where LCn means Left Child of node n and RCn means Right Child of
node n. Top row represents the node values and bottom row represents the
positions.
Reason : Complete Binary Tree.
By the method of elimination: Full binary tree loses its nature when
operations of insertions and deletions are done. For incomplete binary trees,
extra storage is required and overhead of NULL node checking takes place.
So complete binary tree is the better one since the property of complete
binary tree is maintained even after operations like additions and deletions
are done on it
Reason : As the linked list nodes do not have any index and hence they must be
accessed in linear order always. Also they are stored in discrete locations
and not in contiguous memory locations.
Reason : As this is the most suitable option when compared to the other options. Also
option c, e are wrong.
Reason : As both the operators [ and (*). ] are used to access the members of the
structure through pointers. All the other options have the syntax errors.
Reason : As the Stack behaves in Last In First Out manner.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>
DATA STRUCTURE AND ALGORITHM ANALYSIS SET 9
Data Structure and Algorithm Analysis
Questions 81 To 90

81. A "queue" is also known as what?
(a) A FIFO
(b) A stack
(c) A LIFO
(e) A FOFI.

82. A "linked list" always contains elements that can be described as
(a) Redundant
(b) Recursive
(c) Sentinel
(d) Bidirectional
(e) Self-referential.

83. In tree construction, which is the suitable and efficient data structure?
(a) Array
(c) Stack
(d) Queue
(e) Graph.

84. The following are the statements regarding the NP problems. Chose
the right option from the following options:
I. All NP-complete problems are not NP-hard.
II. Some NP-hard problems are not known to be NP-complete.
(a) Both (I) and (II) are true
(b) Both (I) and (II) are false
(c) Only (I) is true
(d) Only (II) is true
(e) (I) is true but (II) is not always true.

85. Let G be a graph with n nodes and let m be the chromatic number of the
graph. Then the time taken by the backtracking algorithm to color it is
(a) O(nm)
(b) O(n+m)
(c) O(mn
m
)
(d) O(n
m
)
(e) O(nm
n
).

86. The time complexity of the shortest path algorithm can be bounded by
(a) O(n
2
)
(b) O(n
4
)
(c) O(n
3
)
(d) O(n)
(e) O(n log n ).

87. Read the following statements carefully and pick the correct option:
I. The worst time complexity of the Floyds algorithm is O(n
3
).
II. The worst time complexity of the Warshalls algorithm is O(n
3
).
(a) (I) is false but (II) is true
(b) (I) is true but (II) is false
(c) Both (I) and (II) are true
(d) (I) is true and (II) is not true always
(e) Both (I) and (II) are false.

88. The asymptotic notation for defining the average time complexity is
(a) Equivalence
(b) Symmetric
(c) Reflexive
(d) Transitive
(e) Both (c) and (d) above.

89. The suitable data structure to represent the IDs of employees is
(a) Stack
(b) Queue
(d) Array
(e) Tree.

90. The number of nodes a null tree can have is
(a) One
(b) Two
(c) Three
(d) Zero
(e) Null tree is invalid tree.

Reason : As the Stack behaves in Last In First Out manner.
Reason : There should be a self referential structure in linked list so as to point to the
other node in the link list. The other options are wrong.
Reason : If a linked list is used then a node can hold the address of both the left child
and right child. Stack, Queue and Graph can never be used. Arrays can also
be used but careful indexing is required to be done and hence is not a valid
data structure.
Reason : According to the theory of NP-complete and NP-hard problems.
Reason : As the number of internal nodes in the state space tree are m
n
, and O(mn)
is the time spent by the NextValue algorithm to determine the children
corresponding to legal colorings. Hence the total time is bounded by O(nm
n
).
Reason : As the algorithm contains three for loops nested together running from 1 to
n and hence the total time is O(n
3
).
Reason : As the both the algorithms contains three for loops nested together running
from 1 to n and hence the total time is O(n
3
) for both the algorithms.
Reason : This is a theta notation which is Reflexive, Symmetric and Transitive and
hence it is an Equivalence relation.
Reason : As the type of all Id for all the employees is same then a simple array is
enough to store the values.
Reason : As a null tree is a valid tree with zero number of nodes.

91. For the bubble sort algorithm, what is the time complexity of the best/worst
case? (assume that the computation stops as soon as no more swaps in one
pass)
(a) best case: O(n) worst case: O(n*n)
(b) best case: O(n) worst case: O(n*log(n))
(c) best case: O(n*log(n)) worst case: O(n*log(n))
(d) best case: O(n*log(n)) worst case: O(n*n)
(e) best case: O( 1) worst case: O( n ).

92. For the quick sort algorithm, what is the time complexity of the best/worst
case?
(a) best case: O(n) worst case: O(n*n)
(b) best case: O(n) worst case: O(n*log(n))
(c) best case: O(n*log(n)) worst case: O(n*log(n))
(d) best case: O(n*log(n)) worst case: O(n*n)
(e) best case: O(n*log(n)) worst case: O(n*n*n).

93. In an arbitrary tree ( not a search tree) of order M. Its size is N, and its height
is K. The computation time needed to find a data item on T is
(a) O(K*K)
(b) O(M*M)
(c) O(N)
(d) O(K)
(e) O( 1 ).

94. When we organize our data as an ordered list, what is the time complexity of
inserting/deleting a data item to/from the list?
(a) O(length_of_list*length_of_list)
(b) O(length_of_list)
(c) O(log(length_of_list * length_of_list))
(d) O(1)
(e) O( log(length_of_list)*length_of_list ).

95. Five statements about B-trees are below. Four of them are correct. Which one
is INCORRECT?
(a) All B-trees are also search trees
(b) The word B-tree stands for balanced tree
(c) The word B-tree also stands for binary tree
(d) All leaves of a B-tree must be on the same level
(e) B-tree is not same as B
+
-tree.

96. For any B-tree of height H (H>1), after inserting a new key, is it possible for a
key, K, which was located in a leaf-node to move up to the root in this regard
which of the following is correct?
(a) Cant be defined without data
(b) Never
(c) Yes, only if H=2
(d) Yes, only when the half of the keys in the root are less than K and the
other half of the keys in the root are greater than K
(e) Yes.

97. When we say the order of a tree is M, we mean
(a) Every non-leaf node must have M subtrees
(b) Every non-leaf node must have M keys
(c) Every non-leaf node can have at most M subtrees
(d) Every non-leaf node can have at most M keys
(e) The Height of the tree is M.

98. T is a search tree of order M, its size is N, and its height is K. The computation
time needed to INSERT/DELETE a data item on T is
(a) O( 1 )
(b) O( M )
(c) O( Log K )
(d) O( N )
(e) O( K ).

99. Suppose that we have a data file containing records of famous people, and we
need to build a hash table to find a record from the person's birthday. The size
of the hash table is 4096. The following are hash functions which convert a
birthday to an integer. Which of the following function is the best?
(a) h1( day/month/year ) = day + month + year
(b) h2( day/month/year ) = day + month*31 + year
(c) h3( day/month/year ) = (day + month*31 + year*365) mod 4096
(d) h4( day/month/year ) = (day + month*31 + year*365) mod 4093
(e) h5(day/month/year ) = (day + month*31 + year*365).

100. What is collision resolution with open addressing?
(a) When collision happens, we create a new memory location outside of
the existing table, and use a chain to link to the new memory location
(b) When collision happens, we enlarge the hash table
(c) When collision happens, we look for an unoccupied memory location in
the existing table
(d) We use an extra table to collect all collided data
(e) We use indexed hash table to resolve collision.

Reason: Best Case is the one in which it is already sorted, and hence it takes O(n) time, otherwise it takes O(n
2
)
time.
Reason: The worst case here is, if the list is already sorted in descending order and we want to sort it in
ascending order. In this case it takes O(n
2
) time. Otherwise it takes O( n * log(n) ) time.
Reason: As the size is N, we have to traverse almost entire tree to look for a node ( because it is not a binary
search tree). And hence it takes O(N) time.
Reason: Because we have to search for deletion/insertion both in the ordered list. And the algorithm takes O(n) in
this case[ best option in the given options].
Reason: As B-tree is not a binary tree, but a balanced m-way tree
Reason: Yes, this is the best option given because, if the insertion in a node causes the node to contain the keys
equal to order of the tree, then the middle key of the node will be passed to one level up (it can be a root
also ). A common mistake is to tick D. But, the condition stated in D is too strong. We were only asking
whether a key can move up to the root, and we were not asking whether a key can become a new root!
Reason: Here in the answer can is most important. That is every non-leaf node can have at most M subtrees
Reason: In this case it is a search tree ( BST ). And hence the search time is proportional to the height of the
tree. This answer assumes that M < K If M >> K, you could answer B However, M is a fixed number, but
K grows when the size of data grows. Therefore, e is a better answer.
Reason: With the answer a some of the entries in the hash table, say 3000-4000, are never used. A similar
problem also applies to b, e. In the answer c, 4096 is a bad choice. It should a prime number.
Reason: The meaning of the open addressing it self is to look and probe for unoccupied and open location to
place the incoming key.

DATA STRUCTURES AND ALGORITHM ANALYSIS SET
11
Data Structures and Algorithm Analysis
Questions 101 To 110

101. What of the following cases is a so-called "collision"?
(a) A hash function produces the same address for two different keys:
h( key1 ) = h( key2 ) where key1 =\= key2
(b) Two different hash functions produce the same address for a given key:
h1( key ) = h2( key )
(c) Two different hash functions produce the same address for two different
keys:
h1( key1 ) = h2( key2 ) where key1 =\= key2
(d) A hash function produces the same address for two different keys with
different lengths: h( key1 ) = h( key2 ) where length(key1) =\= length
(key2)
(e) A hash function produces the same address for two keys of the same
length:
h( key1 ) = h( key2 ) where length(key1) = length(key2).

102. Which of the following statements is INCORRECT when the
differences/similarities between binary search trees and heaps?
(a) Both binary search trees and heaps are binary trees
(b) Both binary search trees and heaps require ascending order between
sibling nodes
(c) With heaps the smallest value is stored at the root, while with binary
trees the smallest value is on the leftmost leaf
(d) In heaps no gaps are allowed apart from the right side of the leaves'
level, but in binary search trees gaps are allowed
(e) The order of root, left, right nodes are different for both the trees.

103. Find the odd one out from the following categories of algorithms.
(a) TVSP
(b) OBST
(c) N-Queens
(d) 15-Puzzle
(e) Bin-Packing.

104. For the ease of searching in a linked list, the linked list be created in
(a) Descending order
(b) Ascending order
(c) Without underflow condition
(d) Any order
(e) With underflow condition.

105. The time complexity of binary search in best, worst cases for an array of size
N is
(a) N, N
2

(b) 1, Log N
(c) Log N, N
2

(d) 1, N log N
(e) N, N.

106. Folding is a method of generating
(a) A hash function
(b) Index function for a triangular matrix
(d) Linear probing
(e) Chaining.

107. Which of following algorithm scans the list by swapping the entries whenever
pair of adjacent keys are out of desired order?
(a) Insertion sort
(b) Quick sort
(c) Shell sort
(d) Bubble sort

108. What is the linked list that contains a variable whose value is the address of
the location of the other node in the list?
(a) Integer
(b) Float
(c) Char
(d) Void
(e) Pointer.

109. The mathematical definition for Omega can be defined as, provided
f,g:NR
+
and c is a positive constant and n > n
0
,
(a) f(n) c. g(n) n
(b) f(n) = c. g(n) n
(c) f(n) c + g(n) n
(d) f(n) = c + g(n) n
(e) f(n) = g(n) n.

110. The u notation is
(a) Symmetric
(b) Reflexive
(c) Transitive
(d) Both (b) and (c) above
(e) (a), (b) and (c) above.

Reason: Collision happens if the hash function produces the same address for two different keys in the prime
area.
Reason: As this statement is totally wrong as Binary search trees require ascending order between siblings node,
but heaps don't.
Reason: This one belongs to NP-Class category.
Reason: What ever the order it may be, always we have to search for a node in the entire linked list, which is
always organized in discrete locations. Hence no any particular order is needed.
Reason: Best case is; if the element to search is there at mid of he list. Worst case is; if the element we are
searching is either not there in the list or at the beginning of the list or end of the list.
Reason: Folding is one of the methods in hashing functions. It can be shift folding or rounded folding.
Reason: Bubble sort only is the algorithm from the given options which compares the adjacent keys.
Reason: A self referential pointer variable is used to hold the address of the next node in the list.
Reason: According to the mathematical definition of the Asymptotic notations.
Reason: Because u notation is equivalence relationship in nature.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Se
DATA STRUCTURES AND ALGORITHM ANALYSIS SET
12
Data Structures and Algorithm Analysis
Questions 111 To 120

111. Which of the following belongs to the algorithm paradigm?
(a) Minimum & Maximum problem
(b) Knapsack problem
(c) Selection problem
(d) Merge sort
(e) Quick sort.

112. If f,

t: N R
+
, then t (n) e (f (n)), iff f(n) e O (t (n)) is known as
(a) Limit rule
(b) Rule of inference
(c) Duality rule
(d) Rule of consequences
(e) Rule of symmetricity.

113. How many children do an external node of a binary tree of order N contains?
(a) N exactly
(b) N-1 exactly
(c) One exactly
(d) 0 exactly
(e) N/2 exactly.

114. To calculate c(i, j )s, w( i, j)s and r(i, j)s; Howmuch time does the OBST
algorithm in worst case takes?
(a) O(log n)
(b) O (n
4
)
(c) O (n
3
)
(d) O (n log n)
(e) O (n
2
).

115. Which of the following in breadth first search uses data structures as an
auxiliary structure to hold nodes for future processing?
(a) Queue
(c) Graph
(d) B-Tree
(e) Stack.

116. The number of loop(s) of a node in a simple graph of N nodes is
(a) One
(b) N
(c) Zero
(d) Exactly two
(e) N-1.

117. The time taken by NP-class sorting algorithm is
(a) O(1)
(b) O(log n)
(c) O(n
2
)
(d) O(n log n)
(e) O(n).

118. Pick the correct one from the following by reading carefully the below
statements
I. Floyds algorithm takes n
3
among of time.
II. Warshalls algorithm takes n
3
among of time.
(a) I is true but II is false
(b) I is false but II is true
(c) Both are false
(d) Both are true
(e) I is true and II takes n4 amount of time.

119.
What defines the average case for the functions f, g : N > R
>0
for all n c N,
lt
n >
f(n)/g(n) = ?
(a) f(n) c O( g(n)), and f(n) c u ( g(n))
(b) f(n) c O( g(n)), but f(n) e u ( g(n))
(c) f(n) e O( g(n)), but f(n) c u ( g(n))
(d) f(n) c O( g(n)), but g(n) e u ( f(n))
(e) f(n) c O( g(n)), but f(n) e u ( g(n)).

120. For the functions f, g : N > R
>0
for all n c N, f(n) = n and g(n) = n log n then
(a) f(n) e O( g(n)), but g(n) c O( f(n))
(b) f(n) c O( g(n)), but g(n) e O( f(n))
(c) g(n) c O( f(n)), but f(n) e O ( g(n))
(d) f(n) c O( g(n)), and g(n) c O( f(n))
(e) f(n) c O( g(n)), but f(n) e O( g(n)).

Reason: Remaining all belongs to divide and conquer paradigm.
Reason: According to the asymptotic notation rules. And this rule is used to apply the limit rule for the omega
notated values.
Reason: Because, the leaf nodes cant have the children.
Reason: Because, we have to calculate all c(i, j )s, w( i, j)s and r(i, j)s for the tree of n identifiers.
Reason: As in BFS, all the adjacent nodes are traversed first, before traversing the descendent nodes. And
hence a queue is used.
Reason: Because, the simple graph should not contain any loop in the entire graph.
Reason: Here only one loop from one to n is there. And hence it runs for n times only.
Reason: As there are three nested loops running from 1 to n in both of the algorithms. And hence it is O(n
3
)
Reason: According to the limit rules for big Oh notation.
Reason: According to the limit rules for big Oh notation.

121. Consider that a heap is implemented using a partially filled array called data,
and the array contains n elements (n > 0), where is the entry with the greatest
value?

(a) data[0]
(b) data[n-1]
(c) data[n]
(d) data[2*n + 1]
(e) data[2*n + 2].

122. Which of the following are used to represent the sparse matrices?

(a) Singly Linked List where each node contains non- zero elements of the matrix

(b) Circular Linked List for each row, column with each node corresponding to
non-zero element
(d) Cant be represented using Linked List, but with arrays only
(e) Can be represented only by queues.

123. Which of the following has a desired key is searched, starting itself from hash

(b) Random probing
(c) Reverse probing
(d) Chaining
(e) Linear probing.

124. Consider A is an array of order m*n stored in a Row Major order. If the
address of the first element in the array is K which is there at A[0, 0], then What is

(a) K + (i-1) * m + j 1
(b) K + (i-1) * n + j 1
(c) K + i * m + j
(d) K + (j-1) * m + j 1
(e) K + j * n + i.

125. Which of the following is used in conversion of an infix expression to a
postfix expression?

(a) Circular list
(b) Array
(c) Stack
(d) Queue
(e) Dequeue.

126. What is the output of the following function when it is called?
void disp( node *first )
if( first->next )
{
{
printf("%4d", first->data);
disp( first->next );
}
}

(a) Display the data of next node always in the list
(b) Display the data of all nodes of the list
(c) Does not execute
(d) Display the data of the first node only
(e) Display the data of the last node only.

127. With the wrap around implementation of a queue, which of the following
code should be used to work out the next location of deletion?

(a) front++
(b) front--
(c) front = (front % max_queue_size) + 1
(d) front = (front ++) % max_queue_size
(e) front = 0.

128. Consider the order of a tree is represented by M. Then, which of the following
is true?

(a) Every non-leaf node must have M subtrees
(b) Every non-leaf node must have M keys
(c) Every non-leaf node can have atmost M subtrees
(d) Every non-leaf node can have atmost M keys
(e) The Height of the tree is M.

129. What are the time complexities of binary search for an array of size N in best
and worst cases respectively?

(a) N, N2
(b) 1, Log N
(c) Log N, N2
(d) 1, N Log N
(e) N, N.

130. What is the algorithm, which scans the list by swapping the entries whenever
pair of adjacent keys is out of desired order?

(a) Insertion sort
(b) Quick sort
(c) Shell sort
(d) Bubble sort

Reason: if a heap is implemented using a partially filled array called data, and the array contains n
elements (n > 0), then the entry with the greatest value is data[0] .
Reason: To represent the sparse matrices, we have Singly Linked List where each node contains non-
zero elements of the matrix
Reason: Linear Probing has a desired key is searched, starting itself from hash address, sequentially in
a table.
Reason: If A is an array of order m*n stored in a Row Major order. If the address of the first element in
the array is K which is there at A[0, 0], then the address of A[i, j] is K + (i-1) * n + j 1
Reason: Stack is used in conversion of an infix expression to a postfix expression.
Reason: if the following function is called.
void disp( node *first )
if( first->next )
{
{
printf("%4d", first->data);
disp( first->next );
}
} then it Display the data of all nodes of the list.
Reason: With the ``wrap around'' implementation of a queue, front = (front ++) %
max_queue_size should be used to work out the next location of deletion.
Reason: if the order of a tree is represented by M. Then , Every non-leaf node can have at most M
subtrees is true.
Reason: 1, Log N are the time complexities of binary search for an array of size N in best and worst
cases respectively
Reason: Bubble sort is the algorithm, which scans the list by swapping the entries whenever pair of
adjacent keys is out of desired order.

DATA STRUCTURES AND ALGORITHM ANALYSIS SET
14
Data Structures and Algorithm Analysis
Questions 131 To 140

131. The in-order traversal of some binary tree produced the sequence HFIEJGZ, and the post-order
traversal of the same tree produced the sequence HIFJZGE. What will be the total number of
nodes in the left sub tree of the given tree?
(a) 1
(b) 2
(c) 3
(d) 4
(e) 5.

132. Which of the following shows the difference between a queue and a stack?
(a) Queues require linked lists, but stacks do not
(b) Stacks require linked lists, but queues do not
(c) Queue is used for complex programs and stack for simple programs
(d) Stacks use two ends of the structure, queues use only one
(e) Queues use two ends of the structure; stacks use only one.

133. Which of the following is generated by Folding method?
(a) A hash function
(b) Index function for a triangular matrix
(d) Linear probing
(e) Chaining.

134. From the following choose the one which belongs to the algorithm paradigm other than to which
others from the following belongs to.
(a) Minimum & Maximum problem
(b) Knapsack problem
(c) Selection problem
(d) Merge sort
(e) Quick sort.

135. Which of the following data structures is used by breadth first search as an auxiliary structure to
hold nodes for future processing?
(a) Queue
(c) Graph
(d) B-Tree
(e) Stack.

136. Pick the correct statement(s) from the following set of statements.
I. In the Kruskals algorithm, for the construction of minimal spanning tree for a graph, the selected
edges always form a forest.
II. In Prims algorithm, for the construction of minimal spanning tree for a graph, the selected edges
always form an orchard.
III. DFS, BFS algorithms always make use of a queue, and stack respectively.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (III) above
(e) Both (II) and (III) above.

137. Which of the following methods of collision processing has some of their addresses may remain
unchecked?
(c) Linear collision processing
(d) Random collision processing
(e) Clustering collision processing.

138. Consider a tree t has two subtrees t
1
and t
2
. Identify the tree where the subtree t
1
has minimum
height and the subtree t
2
has maximum height.
(a) Fibonacci
(b) B
+

(c) Sparse
(d) Complete binary
(e) B.

139. Which of the following is used to select the first entry which has the free block equal to or more
than required one in memory allocation?
(a) Best fit method
(b) Average fit method
(c) Worst fit method
(d) Big fit method
(e) First fit method.

140. What is the total number of nodes at every level (L) of a complete binary tree?
(a) 2L
(b) 2L1
(c) 2
L1

(d) 2
L
1
(e) 2
L
.

Reason: The in-order traversal of some binary tree produced the sequence HFIEJGZ, and the post-
order traversal of the same tree produced the sequence HIFJZGE. Then the total number of
nodes in the left sub tree of the given tree is 3.
Reason: Queues use two ends of the structure; stacks use only one show the difference between them.
Reason: a hashing function is generated by Folding method.
Reason: Knapsack problem is the one which belongs to the algorithm paradigm other than to which
others from the following belongs to
Reason: Queue is the data structure used by breadth first search as an auxiliary structure to hold nodes
for future processing.
Reason: In the Kruskals algorithm, for the construction of minimal spanning tree for a graph, the
selected edges always form a forest.
unchecked.
Reason: Consider a treet has two sub trees t
1
and t
2
. Fibonacci is the tree where the sub tree t
1
has
minimum height and the sub tree t
2
has maximum height.
Reason: First fit method is used to select the first entry which has the free block equal to or more than
required one in memory allocation.
Reason: The total number of nodes at every level ( L ) of a complete binary tree is 2
L
.
141. Which of the following statements is true related to a B-tree?
(a) All entries of a node are greater than or equal to the entries in the node's children
(b) All leaves are at the different depths
(c) All the leaf nodes appear at same level
(d) All non-leaf nodes have the exact same number of children
(e) All nodes contain the exact same number of entries.

142. A simple graph has no loops. Which of the following forms another property of a simple graph?
(a) Contain un-weighted edges
(b) Undirected
(c) Have at least one vertex
(d) Have no multiple edges
(e) Must be directed.

143. Which of the following information is not saved in the activation record, when ever a function call
is executed?
(b) Current depth of recursion
(c) Formal parameters
(d) Location where the function should return when done
(e) Local variables.

144. Use the following code and the linked list pictured below to answer the questions 24 - 26.
First

Then, First nextnextdata is
(a) Null
(b) Syntax Error
(c) 30
(d) 20
(e) 45.

145. Pick the right option from the following:
I. Firstnext == Temp1.
II. Temp1nextdata == 60.
III. Temp2next next == NULL.
(a) Only (I) is TRUE
(b) Only (II) is TRUE
(c) Both (I) and (II) are TRUE
(d) Both (I) and (III) are TRUE
(e) Both (II) and (III) are TRUE.

146. Decide whether the syntax of each of the following statements is valid or invalid.
I. Firstnext = Temp1next;
II. First next = *( Temp2next);
III. * First = Temp2;
(a) Only (I) is valid
(b) Only (II) is valid
(c) Only (III) is valid
(d) Both (I) and (III) are valid
(e) Both (II) and (III) are valid.

147. Which of the following is not possible as a balance factor of any node of an AVL tree?
I. -2.
II. 1.
III. 0.
IV. 2.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (IV) above
(e) Both (III) and (IV) above.

148. Which of the following statement(s) is/are TRUE in view of a multi-way search tree? If a node has
I. 4 sub trees it contains 3 keys.
II. 5 keys, it has 7 sub trees.
III. 6 sub trees, it contains 6 keys.
IV. 10 keys if it contains 11 children.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (IV) above
(e) Both (II) and (III) above.

149. Identify the name of the sorting in which time is not proportional to n
2
.
(a) Selection sort
(b) Bubble sort
(c) Qucik sort
(d) Merge sort
(e) Insertion sort.

150. How many fields are required by a node to represent the polynomials in computer memory using
(a) Two fields
(b) Four fields
(c) More than three fields
(d) One is enough
(e) Three fields.

Reason: All the leaf nodes appear at same level is true related to a B-tree.
Reason: A simple graph has no loops. Other property is to have no multiple edges.
Reason: Current depth of recursion is not saved in the activation record, when ever a function call is
executed.
Reason: the following code and the linked list pictured

Then, First nextnextdata = 45

Reason: Firstnext == Temp1 and Temp2next next == NULL are true.
Reason: The syntax of Firstnext = Temp1next is valid.
Reason: -2 and 2 is not possible as a balance factor of any node of an AVL tree.
Reason: If a node has 4 sub trees it contains 3 keys and 10 keys if it contains 11 children. is TRUE in
view of a multi-way search tree.
Reason: Merge Sort is a sort in which time is not proportional to n
2.

Reason: Three fields are required by a not to represent the polynomials in computer memory using

161. The optimal solution to a problem is a combination of optimal solutions to its sub-
problems. This is known as
(a) Principle of Duality
(b) Principle of Equality
(c) Principle of Feasibility
(d) Principle of Optimality
(e) Principle of Dynamicity.

162. Which of the following versions of merge sort algorithm does uses space efficiently?
(a) Contiguous version
(b) Array version
(d) Structure version
(e) Heap version.

163. Identify the correct problem for multistage graph from the list given below.
(a) Resource allocation problem
(b) Traveling salesperson problem
(c) Producer consumer problem
(d) Barbers problem
(e) Dining philosopher problem.

164. How many edges are there in a Hamiltonian cycle if the edge cost is c and the cost of
cycle is cn
(a) c
(b) 2n
(c) cn
(d) 2c
(e) n.

165. A problem L is NP-complete iff L is NP-hard and
(a) L NP
(b) L NP
(c) L NP
(d) L = NP
(e) L NP.

166. What would be the cost value for any answering node of a sub tree with root r using
branch-bound algorithm?
(a) Maximum
(b) Minimum
(c) Optimal
(d) Average
(e) Negative.

167. Name the process which executes a correct program on data sets measuring the time
and space of a program, in order to compute the results
(a) Partitioning
(b) Hashing
(c) Validating
(d) Profiling
(e) Verifying.

168. What would be the hash address for the following function by using the folding method
H
k
= H(4326)?
(a) 69
(b) 32
(c) 46
(d) 105
(e) 42.

169. Name the node which has been generated but none of its children nodes have been
generated in state space tree of backtracking method.
(b) Live node
(c) E-Node
(d) State Node
(e) Bounded Node.

170. How many nodes are there in a full state space tree with n = 6?
(a) 65
(b) 64
(c) 63
(d) 32
(e) 31.

Reason: According to the principle of optimality, the optimal solution to a problem is a combination of optimal
solutions to its sub-problems which are optimum.
Reason: Because, in linked version auxiliary memory is not required, as needed in array version.
Reason: Because, the resource allocation problem also same as allocating the right resource to appropriate
person. The remaining options are not suitable.
Reason: Because. Simple in Hamiltonian graph there should be exactly n edges to connect all the nodes exactly
once to form a Hamiltonian cycle.
Reason: According to the definitions of NP-class, NP-complete and NP-hard class problems.
Reason: Because the objective in branch-bound algorithms is to minimize the objective function.
Reason: Because, in order to compute the results the profiling process executes the correct program on data sets
measuring the time and space of a program. The remaining options are invalid.
Reason: Because, according to folding method this is 43+26 which is 69.
Reason: Because in state space tree organization live node is a node which has been generated but none of its
children nodes have been generated, but are being generated.
Reason: As this is 2
n
-1 ( i.e. 2
6
-1 = 63 ).

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

171. Consider the following Traveling salesperson problem instance matrix. What would be
the value of g(1, {2, 3})?
8 5
1 7
2 3

(a) 2
(b) 12
(c) 14
(d) 17
(e) 9.

172. The running time complexity of graph coloring problem is
(a) O(n
m
)
(b) O(m
n
)
(c) O(mn
m
)
(d) O(nm
n
)
(e) O(m
n
n
m
).

173. What is the time taken by the binary search algorithm to search a key k in a sorted
array of n elements?
(a) O(log
2
n)
(b) O(n)
(c) (n log
2
n)
(d) O(n
2
)
(e) O(1).

174. Name the function which is used to kill live nodes without generating all their children in
state space tree using backtracking method
(a) Solution function
(b) Bounding function
(c) Optimal function
(d) State Space function
(e) Randomized function.

175. Which of the following search method will always find a goal node nearest to the root of
the tree?
(a) Depth first search
(b) Binary search
(d) Heuristic search
(e) Randomized search.

176. What do we call the selected keys in quick sort?
(a) Recombine key
(b) Inner key
(c) Outer key
(d) Pivot key
(e) Median key.

177. This sort finds location pos of smallest elements in a(i), . , a(n) and then interchange
a(pos) with a(i) for i = 1, ., n 1.
(a) Selection sort
(b) Quick sort
(c) Heap sort
(d) Bubble sort
(e) Insertion sort.

178. Adjacency matrix of a digraph is
(a) Identity matrix
(b) Symmetric matrix
(c) Asymmetric matrix
(d) Sparse matrix
(e) Zero matrix.

179. We can efficiently reverse a string using
(a) Linear queue
(b) Stack
(c) Circular queue

180. The technique of linear probing for collision resolution can lead to
(a) Underflow
(c) Efficient storage utilization
(d) Overflow
(e) Clustering.

Reason : This is g( 1, {2,3}) = min( C
12
+ g( 2, {3}), C
13
+ g( 3, {2})
= min( 8+9, 5+4 ) = 9.
Reason: According to the complexity analysis of the algorithm.
Reason: According to the mathematical analysis binary search takes ( log
2
n ) time.
Reason: Because, in state space tree generation of backtracking approach a bounding function is used to kill live
nodes without generating all their children, if that node is not promising the solution.
Reason: Because in this method nodes are generated level by level and hence the goal node is found nearest to
root node.
Reason: Because, in quick sort division is made along the pivotal element.
Reason: As this process is the key process for the selection sort algorithm.
Reason: Because, in a diagraph for directed edge <a, b>; b is adjacent to a, but reverse is not
true. Hence the adjacency matrix is asymmetric matrix.
Reason: This is nothing but pushing the characters of the string in a stack and then popping the stack to get the
reversed order of the string.
Reason: Because, if we follow the linear probing method to resolve the collision then it may lead to the clustering.
181. For the smart bubble sort algorithm, what is the time complexity of the best
and worst case?
(a) best case: O(n) worst case: O(n
2
)
(b) best case: O(n) worst case: O(n*log n)
(c) best case: O(n*log n) worst case: O(n*log n)
(d) best case: O(n*log n) worst case: O(n
2
)
(e) best case: O(1 ) worst case: O(n).

182. There are two sorted lists L1 and L2. Their lengths are n1 and n2 respectively.
You are asked to design an algorithm to generate a sorted
list L3 from L1 and L2. That is, for example,
if L1=[1,5,30] andL2=[3,6,12,24,43] then L3=[1,3,5,6,12,24,30,43]. Which of the
following methods is the most efficient?
(a) Taking elements from L1 one by one, and inserting them into L2such that
the order of L2 remains unchanged
(b) Appending two lists (i.e. join one list's beginning to the other one's end),
then applying quick sort to the appended list.
(c) Using a merge method that compares two elements at the head of both
lists. The smaller one is then taken out and inserted at the end
of L3 while the larger one is kept its location unchanged. Repeat this until
either L1 or L2 is empty.
(d) Append two lists and apply Shell sort.
(e) Append two lists and apply Selection sort.

183. What is the time complexity of the algorithm a in question 2?
(a) O(log(n1)*n2)
(b) O(n1*log(n2))
(c) O(n1+n2)
(d) O(max(n1,n2))
(e) O(n1*n2).

184. What is the best choice for the time complexity of the algorithm C in question
2?
(a) O(n1*n2)
(b) O(n1*log(n2))
(c) O(n1+n2)
(d) O(max(n1,n2))
(e) O(log(n1)*n2).

185. What is the Postfix form of the following in fix expression?
(2+3)*(4+(5*6)/(7*8/9*1))
(a) 23+456*78*9/1*/+*
(b) 23+456*78*91/*/+*
(c) 23+456*78*91*//+*
(d) 23+456*78*91*//*+
(e) 23+456*+78*91*//*.

186. The direct or random access of element is not possible in
(a) Array
(b) String
(d) Both (a) and (b) above
(e) Both (b) and (c) above.

187. If for a given Queue initially f=0 and r=-1, then f = r refers to
(a) Queue is empty
(b) Queue is full
(c) Queue has two elements
(d) Exactly one element is there
(e) Not possible at all.

188. Any string of bits of length n represents a unique non-negative integer
between
(a) 0 and 2
n-1
-1
(b) 0 and 2
n
1
(c) 2
n-1

(d) 0 and 2
n

(e) 0 and 2
n-1
.

189. Pick the statement(s) which is(are) not true
(a) ADT is useful tool for specifying the logical properties of data type
(b) While defining the ADT as a mathematical concept, the space efficiency
isnt a major concern
(c) Every ADT can be implemented using any programming language
(d) ADT helps us to hide the organization of the data
(e) A & B.

190. Using arrays the most efficient implementation of the QUEUE is as
(a) Linear queue
(b) Circular queue
(d) Cant be implemented
(e) Priority queue.

Reason : The smart bubble sort assumes that the computation stops as soon as
no more swaps in pass one.
Reason : As the time complexity of merge sort is small when compared with all the
remaining algorithms. Also the lists L1 and L2 already sorted.
Reason : Each ordered insertion costs O(n2). There is a total of n1 insertions.
Reason : If the size of each list is n1 and n2, then in merge sort the final list
contains n1+n2 elements.
Reason : According to precedence of operators.
Reason : Because, in linked list the elements are always not stored at contiguous
memory locations, but are stored at discrete locations. And hence random
access is not possible.
Reason : If front pointer f and rear pointer r points to same location then there is
only one element in the queue.
Reason : As all the bits are used to hold the data bits, and there is no sign
bit in the array.

Reason : Though ADT is independent of any programming language, but still
every ADT cant be implemented using any programming language.
Reason : Because in Circular queue the memory can be used efficiently.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 S
191. One has implemented the queue with a circular array, keeping track offirst, last,
and count (the number of items in the array). Suppose firstis zero, and last is
SIZE-1. What can you tell me about count?
(a) count must be zero
(b) count must be SIZE
(c) count could be zero or SIZE, but no other values could occur
(d) count must be SIZE+1
(e) count must be SIZE-2.

192. When we say the order of a tree is M, we mean
(a) every non-leaf node must have M subtrees
(b) every non-leaf node must have M keys
(c) every non-leaf node can have at most M keys
(d) every non-leaf node can have at most M subtrees
(e) the Height of the tree is M.

193. Find the odd one out from the following categories of algorithms
(a) Bin-packing
(b) OBST
(c) N-Queens
(d) 15-Puzzle
(e) TVSP.

194. This algorithm scans the list by swapping the entries whenever pair of
adjacent keys are out of desired order.
(a) Insertion sort.
(b) Bubble sort.
(c) Shell sort.
(d) Quick sort.

195. The mathematical definition for Omega can be defined as, provided f,g:NR+
and c is a positive constant and n > n0,
(a)
f(n) = g(n) n
(b)
f(n) = c. g(n) n
(c)
f(n) c + g(n) n
(d)
f(n) = c + g(n) n
(e)
f(n) c. g(n) n.

196. Let f, t: N R+, then t (n) e (f (n)), iff f(n) e O (t (n)) is known as what?
(a) Limit rule
(b) Rule of inference
(c) Duality rule
(d) Rule of consequences
(e) Rule of symmetricity.

197. The u notation is
(a) Symmetric
(b) Reflexive
(c) Transitive
(d) B & C only
(e) A, B, & C.

198. How many children do an external node of a binary tree of order N contains.
(a) N exactly
(b) N-1 exactly
(c) One exactly
(d) 0 exactly
(e) N/2 exactly.

199. From the following chose the one which belongs to the algorithm paradigm
other than to which others from the following belongs to.
(a) Minimum & Maximum problem.
(b) Knapsack problem.
(c) Selection problem.
(d) Merge sort.
(e) Quick sort.

200. To calculate c(i, j )s, w( i, j)s and r(i, j)s; the OBST algorithm in worst case
takes the following time.
(a) O(log n)
(b) O (n
4
)
(c) O (n
3
)
(d) O (n log n)
(e) O (n
2
).

Reason : This is the case where either the list is full or the list the made
empty after it was full.
Reason : Here in the answer can is most important. That is every non-leaf node
can have at most M subtrees.
Reason : This one belongs to NP-Class category.
Reason : Bubble sort only is the algorithm from the given options which
Reason : According to the mathematical definition of the Asymptotic
notations.
Reason : According to the asymptotic notation rules. And this rule is used to apply
the limit rule for the omega notated values
Reason : Because u notation is equivalence relationship in nature.
Reason : Because, the leaf nodes cant have the children.
Reason : Remaining all belongs to divide and conquer paradigm.
Reason : Because, we have to calculate all c(i, j )s, w( i, j)s and r(i, j)s for
the tree of n identifiers
<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

201. The time taken by NP-class sorting algorithm is
(a) O(1)
(b) O(log n)
(c) O(n
2
)
(d) O(n log n)
(e) O(n).

202. For the functions f, g : N > R
>0
for all n c N, f(n) = n and g(n) = n log n then
(a) f(n) e O( g(n)), but g(n) c O( f(n))
(b) f(n) c O( g(n)), but g(n) e O( f(n))
(c) g(n) c O( f(n)), but f(n) e O ( g(n))
(d) f(n) c O( g(n)), and g(n) c O( f(n))
(e) f(n) c O( g(n)), but f(n) e O( g(n)).

203. What is the type of the algorithm used in solving the 8 Queens problem?
(a) Greedy
(b) Dynamic
(c) Branch and Bound
(d) Divide and Conquer
(e) Backtracking.

204. Let G be a graph with n nodes and let m be the chromatic number of the
graph. Then the time taken by the backtracking algorithm to color it is
(a) O(nm
n
)
(b) O(n+m)
(c) O(mn
m
)
(d) O(n
m
)
(e) O(nm).

205. What is the major data structure used in the Network data model?
(a) Stack
(b) Array
(c) Graph
(d) Tree
(e) Queue.

206. Minimum number of queue(s) needed to implement the priority queue is(are)
(a) Four
(b) Three
(c) Two
(d) One
(e) Depends upon the application.

207. Sorting is not possible by using which of the following methods?
(a) Insertion
(b) Selection
(c) Exchange
(d) Deletion
(e) Partitioning.

208. The number of null branches for a binary tree with 20 nodes is
(a) 21
(b) 20
(c) 22
(d) 19
(e) 18.

209. The time complexity of binary search in best, worst cases for the array of size
N is
(a) N, N
2

(b) N, N
(c) 1, logN
(d) 1, NlogN
(e) logN, N
2
.

210. A desired key in a table is searched, starting itself from hash address,
sequentially for the following.
(b) Linear probing
(c) Random probing
(d) Chaining
(e) Reverse probing.

Reason : Here only one loop from one to n is there. And hence it runs for
n times only
Reason : According to the limit rules for big Oh notation
Reason : As the other types of algorithms are not suitable to find the
solution for the given problem.
Reason : As the number of internal nodes in the state space tree are m
n
, and
O(mn) is the time spent by the NextValue algorithm to determine the children
corresponding to legal colorings. Hence the total time is bounded by O(nm
n
).
Reason : As the graph is the suitable data structure to represent the connectivity
between the nodes as edges.
Reason : Two : One queue is used for actual storing of data and another
for storing priorities.
Reason : There is no sorting algorithm available where we delete an element.
Using insertion we can perform insertion sort, using selection we can perform
selection sort, using exchange we can perform the bubble sort (and other
similar sorting methods) and using the portioning we can perform merge
sort(and other similar sorting methods) But no sorting method can be done just
using deletion.
Reason : For n nodes there are always n+1 null branches in a binary
tree.
Reason : In best case if the required element is at the middle then it takes O( 1 )
time other wise it takes O( log n ) time in worst case.
Reason : According to opening addressing techniques, the linear probing starts
probing from the hash address and it continues sequentially in a linear manner.

211.
Find the correct sequence for the Big Oh values for all n > 2
(a) O(1) < O( n) < O( log n) < O(n log n) < O(n2) < O(n3) < O(2n)
(b) O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n) < O(n3)
(c) O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n)
(d) O(1) < O(n) < O(log n) < O(n log n) < O(n2) < O(2n) < O(n3)
(e) O(1) < O(log n) < O(n) < O(n2) < O(n log n) < O(n3) < O(2n).

212.
The u notation is
I. Reflexive.
II. Symmetric.
III. Transitive.
(a) Only (I) above
(b) Only (III) above
(c) Both (I) and (II) above
(d) Both (II) and (III) above
(e) All (I), (II) and (III) above.

213.
Data structure
(a) May be helpful to develop efficient algorithms in different phases
of data processing
(b) Need not give relationship between data items
(c) Is programming language dependent
(d) Is procedural language dependent
(e) Need to describe the relationship among data items.

214.
Modular programming uses
(a) Only top-down method
(b) Only bottom-up method
(c) Both (a) and (b) above
(d) Procedural approach
(e) Object oriented approach.

215. In Knapsack problem, the best strategy to get the optimal solution,
where P
i
, W
i
is the Profit, Weight associated with each of the
X
i
th
object respectively is to
(a) Arrange the values P
i
/W
i
in ascending order
(b) Arrange the values P
i
/X
i
in ascending order
(c) Arrange the values P
i
/W
i
in descending order
(d) Arrange the values P
i
/X
i
in descending order
(e) Arrange the values Wi/ Xi in decreasing order.

216.
In the following weighted graph, the length of the minimum spanning tree is
(a) 39
(b) 41
(c) 43
(d) 52
(e) depends on the starting vertex.

217.
The time complexities of Max Min recursive and non-recursive algorithms are
(a) 1, 2
(b) , 2
(c) 2, 1
(d) ,
(e) 3n 2, 3n 1.

218.
Greedy job scheduling with deadlines algorithms complexity is defined as
(a) O(N)
(b) ( n log n)
(c) O (n
2
log n)
(d) O ( n log n)
(e) (N
2
).

219.
The divide and conquer merge sort algorithms time complexity can be defined as
(a) u (long n)
(b) u (n)
(c) (n log n)
(d) u (n log n)
(e) u (n
2
log n).

220.
In analysis of algorithm, approximate relationship between the size of the job and
the amount of work required to do it is expressed by using
(a) Order of magnitude or Big - O
(b) Central tendency
(c) Differential equation
(d) Polynomial equation
(e) Space complexity.

Reason : O(1) < O(log n) < O(n) < O(n log n) < O(n
2
) < O(n
3
) < O(2
n
)
Reason : (I), (II), & (III).
Reason : may be helpful to develop efficient algorithms in different phases
of data processing
Reason : both a & b
Reason : Arrange the values P
i
/X
i
in descending order
Reason : depends on the starting vertex
Reason : 1, 2
Reason : O(N)
Reason : u (n log n)
Reason : order of magnitude or Big - O
221.
In linked list, a node contains atleast
(a) Node address field, data field
(b) Node number, data field
(c) Next address field, information field
(d) Node number, information field
(e) Node address field, information field.

222.
Which of the following is the correct big-O expression for 1 + 2 + 3 + ... + n?
(a) O(log n)
(b) O(n)
(c) O(n log n)
(d) O(n)
(e) O(2
n
).

223.
A B-tree of order n is also called
(a) (nn) 1 tree
(b) n(n2) tree
(c) (n1)n tree
(d) n(n1) tree
(e) n1 tree.

224. The minimum number of keys contained in each non root node of a B-
tree of order 11 are
(a) 4
(b) 5
(c) 3
(d) 2
(e) 6.

225. Row-major order in two dimensional array refers to
(a) All elements of a row are stored in memory in sequence followed
by next row in sequence and so on
(b) All elements in the sorted order according to their column
sequence
(c) All elements of a column in sequence and so on
(d) All elements in the sorted order according to their row sequence
(e) All elements of a row are stored in memory in sequence followed
by next column in sequence and so on.

226.
Null pointer is used to define
II. Empty pointer field of a structure.
III. The linked list is empty.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (II) above
(e) All (I), (II) and (III) above.

227.
Direct or random access of element is not possible in
(b) Array
(c) String
(d) Queue

228. Get a node, store new element and insert the new node at the top
refers to insert operation in non empty
(a) Stack
(b) Queue
(c) Array
(e) Tree.

229. A technique which collects all deleted spaces onto free storage list is
called
(a) Static memory allocation
(b) Garbage collection
(c) Calloc
(d) Dynamic memory allocation
(e) Malloc.

230.
datatype *s;
datatype *p;
datatype *i;
p = malloc(100); t=p; s=p+ (100/size of (datatype))-sizeof (datatype);
Func(datatype j )
{ if (p > s) {printf (overflow/n); return:}
*p= j; p++;}
is referring to
(a) Push operation of stack
(b) Pop operation of stack
(c) Push operation of queue
(d) Pop operation of queue
(e) Push and pop of the stack.

Reason : next address field, information field.
Reason : O(n)
Reason : (n-1)-n tree
Reason : 5
Reason : all elements of a row are stored in memory in sequence followed by next
row in sequence and so on.
Reason : (I), (II), (III) are correct
Reason : string
Reason : stack
Reason : Garbage collection
Reason : push operation of stack
231. malloc() returns pointer to
(a) Integer
(b) Structure
(c) Character
(d) Boolean
(e) Float.

232. If a binary tree is threaded for an in order traversal order, a NULL right
(a) Successor
(b) Predecessor
(c) Root
(d) Postdecessor
(e) None of these.

233.
If graph G has no edges then corresponding adjacency matrix is
(a) Unit matrix
(b) Zero matrix
(c) Matrix with all 1s
(d) Diagonal matrix
(e) Upper diagonal matrix.

234.
Worst case efficiency of binary search is
(a) log
2
n + 1
(b) n
(c) N
2

(d) 2
n

(e) log n.

235.
The property that is not expected from good hashing technique should
(a) Produce keys uniformly distributed over the range
(b) Easy to program
(c) Produce no collisions
(d) Produces collisions
(e) All above properties are expected.

236.
Graph structure is available in
(a) C
(b) C++
(c) Pascal
(d) Java
(e) Data structures.

237.
The element at the root of heap is
(a) Largest
(b) Smallest
(c) Depending on type of heap it may be smallest or largest
(d) Fixed
(e) Unlimited.

238.
Worst case efficiency of which search is O(n)?
(a) Sequential search
(b) Binary search
(c) Indexed search
(d) Hashing
(e) Depth first search.

239.
(a) Scans all incident edges before moving to other vertex
(b) Scans adjacent unvisited vertex as soon as possible
(c) Is same as backtracking
(d) Computes a path between two vertices of graph or equivalently
reporting that no such path exists
(e) Scans for cycle in graph, that no such cycle exists.

240.
Which of the following searching methods requires that all keys must reside in
internal memory?
(a) Binary search
(b) Sequential search
(c) Hashing
(d) Depth first search

Reason : character
Reason : successor
Reason : zero matrix
Reason : log2 n+1
Reason : produce no collisions.
Reason : data structures
Reason : depending on type of heap it may be smallest or largest
Reason : Sequential search
Reason : scans all incident edges before moving to other vertex
Reason : Binary search

241. Standard queue operations are
(a) empty(), fill(), place(), remove() (b) enque(), dequeue(), isempty(), isfull()
(c) init(), delete(), add() (d) isempty(), isfull(), fill(), remove()
(e) isempty(), isfull(), init(), delete(), add().
242. Assume that variable A is a sorted array of integers of length L. Consider the following code
segment:
int k=1, flag=0;
for ( ; k<L; k++ )
{
if (A[k-l] == A[k] ) flag = 1;
}

Which of the following best describes when variable flag is 1 after this code segment executes?
(a) Always
(b) Never
(c) If and only if array A really is sorted
(d) If and only if array A contains adjacent duplicate values
(e) If and only if all values in array A are the same.
243. Consider using binary search to look for a given value in an array of integers. Which of the
following must be true in order for the search to work?
I. The values in the array are stored in sorted order.
II. The array does not contain any duplicate values.
Ill. The array does not contain any negative values.
(a) Only (I) above (b) Only (II) above (c) Both (I) and (II) above
(d) Both (I) and (III) above (e) Both (II) and (III) above.
244. A program is required to store and process information about the solar system. Which types of
data structure would be most appropriate for storing the following respectively : (i) A list of the
names of the planets, (ii) full details of a planet including distance from sun, diameter,
atmospheric composition, etc, (iii) a predetermined list of scientific observations made of the
planet.
(b) (i) array (ii) structure (iii) linked list
(c) (i) linked list (ii) array (iii) array
(e) (i) array (ii) structure (iii) array.
245. If data is a circular array of CAPACITY elements, and R is an rear index into that array, what is
the formula for the index after R?
(a) (R + 1) % CAPACITY (b) R % (1 + CAPACITY)
(c) (R % 1) + CAPACITY (d) R + (1 % CAPACITY) (e) R = R + 1.
246. In the linked list implementation of the queue, where does the insert method place the new
(b) At the tail
(c) After all other entries that are greater than the new entry
(d) After all other entries that are smaller than the new entry
(e) This operation is not possible.
247. One difference between a queue and a stack is:
(a) Queues require linked lists, but stacks do not
(b) Stacks require linked lists, but queues do not
(c) Queue is used for complex programs and stack is used for simple programs
(d) Stacks use two ends of the structure, queues use only one
(e) Queues use two ends of the structure; stacks use only one.
248. Suppose cursor refers to a node in a linked list. What boolean expression will be true when
cursor refers to the tail node of the list?
(a) cursor.next == null (b) cursor == null (c) cursor.data
== null
(d) cursor.data == 0 (e) cursor.next == cursor.
249. Five statements about lists and stacks are below. Four of them are correct. Which one
isincorrect?
(a) Lists can be implemented by using arrays or linked lists
(b) Lists are the dynamic data structures
(c) A stack is a special kind of list in which all insertions and deletions take place at one end
(d) A list is a sequence of one or more data items
(e) Stacks are easier to implement than lists.
250. For questions 10-11, refer to the following:
Queue q;
int i=1;
const int value = 10;
for ( ; i < value; i++) insert(i, q);
printf(%d, frontofqueue(q) ) ;
delete(q);
delete(q);
printf(%d, frontofqueue(q) ) ;
while (! isempty(q))
delete(q);
What is the output for the above code?
(a) 1, 3 (b) 1, 4 (c) No output (d) 9, 6 (e) 9, 7.

Reason: As fill( ), place( ), add( ),empty( )are not the standard operations on queue, while enque ( ),
dequeue( ), isempty( ), isfull( ) are the standard one.
Reason: Because this is an array, and k is the index. So k, k-1 are the adjacent index values.
Reason: Because the precondition for binary search is, the array must be there in sorted order, should
not contain duplicate values and may have positive or negative values.
Reason: To keep track of the planets a fixed size array is enough as the number of plants is finite value.
In addition, to keep track of complex information as a single logical unit structure is the best
suitable data structure. For predetermined list array is suitable.
Reason: As the circular queue is given, if the empty places are there at any place and rear pointer R is
a rear index, then this is the best ever formula to increment R. All other are not valid or
appropriate one.
Reason: Because, in queue always the new entries are inserted at the end of queue, no matter the
queue is implemented as array or linked list.
Reason: As all the other options are meaning less.
Reason: As the next field of the node is holding NULL address, means it is the last node .
Reason: As in lists only one data item of some type can only be stored. More data items cant be stored
in a list.
Reason: As using insert we are inserting the elements in queue are 1, 2, 3, 4, 5, 6, 7, 8, 9. Then using
printf statement we are displaying the first element, then we are deleting the front element
twice (i.e. 1, 2) then again we are displaying front element.

251. At the end of this code segment the queue
(a) Contains numbers 1 to 8 (b) Contain numbers 1 to 10 but we can't see them
(c) Is empty (d) Contains numbers 3 to 10
(e) Contains only 1.
252. What is the correct post fix form of the given infix expression a+b/(c+d)%e*(g-h/(i+j)*k)?
(a) abcd+/e%ghij+/k**-+ (b) abcd+e/%ghij+/k**-+
(c) ab+cd/e%ghij+/k**- (d) ab+cd+/e%ghij+/k*-*
(e) abcd+/e%ghij+/k*-*+.
253. With the wrap around implementation of a queue, which of the following code should be used
to work out the next location of deletion?
(a) front++
(b) front--
(c) front = (front % max_queue_size) + 1
(d) front = (front ++) % max_queue_size
(e) front = 0.
254. Which boolean expression indicates whether the number in two nodes (p and q) are the
same? Assume that neither p nor q is null.
(a) p == q (b) p.data == q.data (c) p.link == q.link
255. Which of the following statements is not true?
(a) An array is a data structure containing a number of items of the same type
(b) An array is normally used to store two or more items of data
(c) The lower bound of an array can be declared as any unsigned value
(d) The elements of an array can be initialized when the array is declared
(e) The number of elements in an array must be declared at compile time.
256. Consider searching for a given value in a large, sorted array. Under which of the following
conditions is sequential search slower than binary search?
(a) Always
(b) Never
(c) When the value being searched for is the first element in the array
(d) When the value being searched for is the second element in the array
(e) When the value being searched for is the last element in the array.
257. The following data structure is used to store the values of the extreme ends of the freely
hanging simple pendulum for every oscillation.
(a) Linked List (b) Priority Queue (c) Deque
(d) Double Stack (e) Array.
258. The linked list which can be processed in either of the direction is
(c) Stack implemented as linked list (d) Queue implemented as linked list
259. What does a run-time analysis usually count?
(a) The number of arithmetic and other operations required for the program to run
(b) The number of megabytes required for the program to run
(c) The number of seconds required for the program to run
(d) The number of seconds plus the number of megabytes
(e) The number of seconds times the number of megabytes.
260. Which of these is the correct big-O expression for 1+2+3+...+n?
(a) O(log n) (b) O(n) (c) O(n log n) (d) O(n) (e) O(1).

Reason: As using while loop we have removed all the elements. Queue becomes empty.
Reason: Once we convert it all the other options are wrong except E
Reason: As in circular queue all the other options are not correct to identify the location pointed by front
pointer where the recent deletion is made.
Reason: As we are comparing the data of both of the nodes, we have to check for the data, a member
of nodes p, q. Also we have to use relational operator( ==).
Reason: As there is no any syntax to declare the lower bound of the array except the size of the array
which can be declared.
Reason: As in all the cases the time taken by sequential( linear) search is O(n) which is greater than the
time taken by binary search which is O(log n).
Reason: As the other data structures are not suitable to store the elements from both of the ends
simultaneously.
Reason: As the Doubly linked list contains two pointers to keep track the address of both the previous
and next nodes.
Reason: All the other options are meaning less. Because for run-time analysis, the number of
operations and memory is not considered, but the unit time(i.e., seconds)
Reason: Because this the summation of the first n numbers, that is n(n+1)/2 => ( n
2
+n)/2 => then by
ignoring the lower order terms and the constants this becomes n
2
.

261. Which of the following formulas in Omega notation best represent the expression n+35n+6?
(a) (n) (b) (n) (c) (n) (d) (35) (e) (6).
262. What term is used to describe an O(n) algorithm?
(a) Constant (b) Non Polynomial Deterministic
(c) Logarithmic (d) Quadratic (e) Linear.
263. Express the formula (n - 2)*(n - 4) using notation:
(a) (n
2
) (b) (8) (c) (log n) (d) (n) (e) (1).
264. Read the following statements carefully and pick the right most option.
I. A linear algorithm to solve a problem must perform faster than a quadratic algorithm to solve
the same problem.
II. An algorithm with worst case time behavior of 3n takes at least 30 operations for every input
of size n=10.
(a) Both (I) and (II) are TRUE
(b) Both (I) and (II) are FALSE
(c) Only (I) is true but (II) depends upon the instance size.
(d) (I) is TRUE but (II) is FALSE
(e) (I) is FALSE and (II) is TRUE.
265. Which of the following are essential statement types for describing algorithms?
(a) Sequence (b) Selection (c) Repetition
(d) All the above (e) A and B only.
266. When we say an algorithm has a time complexity of O (n), what does it mean?
(a) The algorithm has n nested loops
(b) The computation time taken by the algorithm is proportional to n
(c) The algorithm is n times slower than a standard algorithm
(d) There are n number of statements in the algorithm
(e) The computation time taken by the algorithm is less than n seconds.
267. Can we read a data item at any location of a list within a constant time (i.e. O(1))?
(a) Yes
(b) Yes, only if the list is implemented by pointers (i.e. linked-list)
(c) Yes, only if the list is implemented by an array
(d) No, we need O(n) computation steps no matter what kind of implementation is used
(e) No, as constant time is not possible for algorithms.
268. Sequential search has a time complexity of O(n), and binary search has a time complexity
ofO(log(n)). What difference will it make when the size n is 1000?
(a) You would not notice much difference because computers run very fast anyway
(b) As n is 1000, binary search is twice as fast as sequential search
(c) As n is 1000, binary search is 10 times as fast as sequential search
(d) As n is 1000, binary search is 10 times as slow as sequential search
(e) As n is 1000, binary search is 100 times as fast as sequential search.
I. The notation is Anti Symmetric.
II. The big Oh notation is Semi Equivalence.
(a) (I) is FALSE but (II) is TRUE (b) Both (I), (II) are TRUE
(c) (I) is TRUE but (II) is FALSE (d) Both (I), (II) are FALSE
(e) (II) is TRUE and (I) cannot be defined.
270. Find the odd one out.
(a) Bin-Packing Problem (b) TVSP Problem (c) Knap Sack
Problem
(d) OBST Problem (e) Sum of Sub Sets Problem.

Reason: As the given expression is a quadratic equation of degree 2, then by ignoring the constants
and lower order terms, the highest term is n
2
, and hence it is (n
2
).
Reason: Because it is a single term of degree one. And all the remaining options are wrong.
Reason: Again this is a polynomial of degree 2 i.e. n
2
6n + 8. And hence n
2
according to above reason
of Question (21).
Reason: As the first statement is true according to the definition of linear algorithms, the second is false
as 30 operations are not needed.
Reason: Because all are essential for the algorithm to state the solution of a given problem.
Reason: As all the other options are meaning less and are wrong.
Reason: For an array implementation, we can find the given location straightway with the index value
and hence it takes constant time. But for a linked list, we have to follow the pointers one by
one. The cost of this is proportional to the size of the list.
Reason: log(1024) = 10, 1000/log(1000) is roughly equal to 1000/10 = 100
Reason: According to the definitions of Omega, Big Oh notations.
Reason: As Bin-Packing problem belongs to NP ( Non Deterministic Polynomial) hard problems and the
remaining all are Deterministic polynomial time algorithms.
Data Structure and Algorithm Analysis
Questions 271 To 280

271. The suitable data structure to represent the data of rainfall of week days in ten cities of three
states is __________
(a) Stack (b) Queue (c) Array
(d) Multi way tree (e) Connected graph.
272. The data structure which allows the insertion at both the ends, but allows the deletion at only
one end is ________
(a) Output restricted Deque
(b) Input restricted Deque
(c) Circular queue
(d) Linear queue
(e) Priority queue.
273. Searching the linked list requires linked list be created in _________
(a) Ascending order (b) Descending order
(c) With underflow condition (d) Any order (e) Without underflow condition.
274. The time complexity of binary search in best, worst cases for an array of size N is
(a) N, N
2
(b) N, N (c) Log N, N
2
(d) 1, N log N (e) 1, log N.
275. How many minimum number of spanning trees, one can have from a given connected graph
with N nodes is having different weights for the edges.
(a) N-1 (b) One (c) 1/(N+1) 2N
CN
(d) 2N
CN
(e) N.
276. How many children do an external node of a binary tree of order N contains.
(a) N at least (b) 2 exactly (c) More than two (d) 0 (e) N at most.
277. In-order traversal of binary search tree implies visiting the nodes in __________
(a) Post-order
(b) The order of increasing magnitude of their key
(c) Pre-order
(d) The order of decreasing magnitude of their key N
(e) Arbitrary order.
278. For a binary tree the In-order traversal was found to be as HIGCEFD. Once the post-order
traversal was conducted the output is IHGFEDC. What is the per-order for the given tree?
(a) CGDHEIF (b) GHICDEF (c) CGHIDEF
(d) CDEFGHI (e) Data insufficient and hence cant be answered.
279. Breadth first search uses __________ as an auxiliary structure to hold nodes for future
processing.
(a) Stack (b) Linked list (c) Graph (d) B-Tree (e) Queue.
280. Folding is a method of generating ________
(a) A hash function
(b) Index function for a triangular matrix
(d) Linear probing
(e) Chaining.

Reason: As the array can keep the data for multiple dimension values which is not possible by using
any other data structure given in the question.
Reason: Because in Deque insertion and deletion can take place at any end, but in the question it is
given as deletion is allowed only at one end. Hence it is Output restricted Deque
Reason: Because the reference of the next node is stored in the node itself with which one can go to
next node, and hence a particular order is not required.
Reason: Because if the list is either small or the element is present at the mid position for the first
call to the function or for the first iteration then it takes one unit of time. In worst case it
takes log n time,
Reason: Because one can have at most one tree which is minimum, also the weights are different.
Reason: Because external node is a leaf node, that cant have the children
Reason: Because if we traverse a BST in in-order, then it is nothing but traversing in the order of
increasing magnitude of their key.
Reason: Draw the tree according to the asked in-order and post-order and traverse the tree in pre-
order.
Reason: As this requires the traversing of nodes on the same level before traversing the nodes at
next level of the tree, it requires queue.
Reason: Because folding is one of the method of hashing function to locate the key.
Data Structure and Algorithm Analysis
Questions 271 To 280

271. The suitable data structure to represent the data of rainfall of week days in ten cities of three
states is __________
(a) Stack (b) Queue (c) Array
(d) Multi way tree (e) Connected graph.
272. The data structure which allows the insertion at both the ends, but allows the deletion at only
one end is ________
(a) Output restricted Deque
(b) Input restricted Deque
(c) Circular queue
(d) Linear queue
(e) Priority queue.
273. Searching the linked list requires linked list be created in _________
(a) Ascending order (b) Descending order
(c) With underflow condition (d) Any order (e) Without underflow condition.
274. The time complexity of binary search in best, worst cases for an array of size N is
(a) N, N
2
(b) N, N (c) Log N, N
2
(d) 1, N log N (e) 1, log N.
275. How many minimum number of spanning trees, one can have from a given connected graph
with N nodes is having different weights for the edges.
(a) N-1 (b) One (c) 1/(N+1) 2N
CN
(d) 2N
CN
(e) N.
276. How many children do an external node of a binary tree of order N contains.
(a) N at least (b) 2 exactly (c) More than two (d) 0 (e) N at most.
277. In-order traversal of binary search tree implies visiting the nodes in __________
(a) Post-order
(b) The order of increasing magnitude of their key
(c) Pre-order
(d) The order of decreasing magnitude of their key N
(e) Arbitrary order.
278. For a binary tree the In-order traversal was found to be as HIGCEFD. Once the post-order
traversal was conducted the output is IHGFEDC. What is the per-order for the given tree?
(a) CGDHEIF (b) GHICDEF (c) CGHIDEF
(d) CDEFGHI (e) Data insufficient and hence cant be answered.
279. Breadth first search uses __________ as an auxiliary structure to hold nodes for future
processing.
(a) Stack (b) Linked list (c) Graph (d) B-Tree (e) Queue.
280. Folding is a method of generating ________
(a) A hash function
(b) Index function for a triangular matrix
(d) Linear probing
(e) Chaining.

Reason: As the array can keep the data for multiple dimension values which is not possible by using
any other data structure given in the question.
Reason: Because in Deque insertion and deletion can take place at any end, but in the question it is
given as deletion is allowed only at one end. Hence it is Output restricted Deque
Reason: Because the reference of the next node is stored in the node itself with which one can go to
next node, and hence a particular order is not required.
Reason: Because if the list is either small or the element is present at the mid position for the first
call to the function or for the first iteration then it takes one unit of time. In worst case it
takes log n time,
Reason: Because one can have at most one tree which is minimum, also the weights are different.
Reason: Because external node is a leaf node, that cant have the children
Reason: Because if we traverse a BST in in-order, then it is nothing but traversing in the order of
increasing magnitude of their key.
Reason: Draw the tree according to the asked in-order and post-order and traverse the tree in pre-
order.
Reason: As this requires the traversing of nodes on the same level before traversing the nodes at
next level of the tree, it requires queue.
Reason: Because folding is one of the method of hashing function to locate the key.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set 21 Set
22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

What you think about these Questions and

281. This algorithm scans the list by swapping the entries whenever pair of adjacent keys are out of
desired order.
(a) Insertion sort (b) Quick sort (c) Shell sort (d) Bubble sort (e) Radix sort.
282. At a given node of a simple graph, the number of loops are _________
(a) More than one (b) Not more than one (c) Zero
(d) Exactly two (e) Equal to the number of nodes of the graph.
283. In the linked list implementation of the stack, where does the push member function place the
new entry on the linked list?
(b) At the tail
(c) After all other entries those are greater than the new entry
(d) After all other entries those are smaller than the new entry
(e) At null node.
284. I have implemented the queue with a circular array, keeping track of first, last, and count (the
number of items in the array). Suppose first is zero, and last is SIZE-1. What can you tell me
(a) count must be zero.
(b) count must be SIZE
(c) count must be SIZE-2
(d) count must be SIZE+1
(e) count could be zero or SIZE, but no other values could occur.
285. Convert the following postfix expression to a fully-parenthesized infix expression:
A B C - D E + * F * +
(a) (A + ((B - C) * (D + E) * F))
(b) (A + (((B - C) * (D + E)) * F))
(c) A + (((B - C) * (D + E)) * F)
(d) (A + ((B - C) * (D + E)) * F)
(e) A + ((B - C) * (D + E)) * F.
286. The linked list would be able to make use of a ------------- whose value is the address of the
location which stores the node.
(a) integer (b) float (c) char (d) void (e) pointer.
287. What kind of list is best to answer questions such as "What is the item at position n?"
(a) Lists implemented with an array
(e) Circular linked list implemented with priorities.
288. Consider the following pseudo code:
declare a stack of characters
while (there are more characters in the word to read ) {
push the character on the stack
}
while ( the stack is not empty) {
pop a character off the stack
write the character to the screen
}
What is written to the screen for the input "carpets"?
(a) serc (b) steprac (c) carpets (d) ccaarrppeettss (e) stepr.
289. What is the value of the postfix expression 6 3 2 4 + - *
(a) Something between 5 and 15
(b) Something between -5 and -15
(c) Something between 5 and -5
(d) Something between -15 and -100
(e) Something between 15 and 100.
290. Suppose we have a circular array implementation of the queue type, with ten items in the
queue stored at data[2] through data[11]. The current SIZE is 22. Where does the insert
method place the new entry in the array?
(a) data[1] (b) data[22] (c) data[12] (d) data[11] (e) data[21].

Reason: As in bubble sort always we swap the adjacent keys if they are not in desired order
Reason: Because in simple graphs the loops are not allowed.
Reason: Because in stack implemented using linked list, head always points to the top most node.
Reason: Because first = 0 and rear = size-1 means either the stack is full or it was made to empty by
after filling it to full. (Since this is a circular queue).
Reason: As the fully parenthesized expression requires its sub parts to be in the
parenthesis
Reason: Because the pointer is used to hold the address of the next node in the list.
Reason: Because index is required to locate a particular position, and in arrays this index can be
used to locate an element.
Reason: This nothing but pushing the characters on the stack and popping them from the stack and
hence it will print the input in the reverse because of LIFO manner.
Reason: Because the answer is -18.
Reason: Because still the space is there in the queue and the next available location is 12
th
indexed
one, therefore new element is inserted at that location.
291. The mathematical definition for Omega can be defined as, provided f,g:NR
+
and c is a
positive constant and n > n
0
,
(a) f(n) c. g(n) n
(b) f(n) c. g(n) n
(c) f(n) c + g(n) n
(d) f(n) c + g(n) n
(e) f(n) g(n) n.
292. The u notation is __________
I. Symmetric.
II. Reflexive.
III. Transitive.
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Both (I) and (II) above
(e) All (I), (II) and (III) above.
293. From the following pick the one which does not belongs to the same paradigm to which others
belongs to.
(a) Minimum & Maximum problem
(b) Knapsack problem
(c) Selection problem
(d) Merge sort
(e) Quick sort.
294. The OBST algorithm in worst case takes _______ time if all c(i, j )s and r(i, j)s are calculated.
(a) O(log n) (b) O(n
4
) (c) O(n
3
) (d) O(n log n) (e) O(n
2
).
I. For the Backtracking algorithms stack data structure is used.
II. For the Branch-and-bound algorithms queue data structure is used.
(a) (I) is FALSE but (II) is TRUE
(b) (I) and (II) both are FALSE
(c) (I) is TRUE but (II) is FALSE
(d) (I) and (II) both are TRUE
(e) (II) is TRUE and (I) cant be defined.
296. The weighted array used in TVS problems for the following binary tree is __________

(a) [1,2,3,0,0,4,0,5,6]
(b) [1,2,3,0,0,4,0,5,0,0,0,0,6]
(c) [1,2,3,4,5,6]
(d) [1,2,3,0,0,4,5,6]
(e) [1,3,5,2,4,6].
297. Let f,

t: N R
+
, then t (n) e (f (n)), iff f(n) e O (t (n)) is known as __________
(a) Limit rule (b) Rule of inference (c) Duality rule
(d) Rule of consequences (e) Rule of symmetricity.
298. Let f, t: NR
0
, and t (n) eO (f (n)) iff t(n) c.f (n) where c is positive real constant and n
n
o
, then n
o
is ___________
(a) Upper bound (b) Lower bound (c) Duality value
(d) Threshold value (e) Maximum value.
299. For the 15-puzzle problem if the initial arrangement is as follows, then the value of x used to
find the reachability is ________
1 3 5 7
9 11 14

2 4 8 6
10 12 15 13
(a) 0 (b) 1 (c) 8 (d) 14 (e) 13.
300. The time taken by nondeterministic sorting algorithm is ______
(a) O(1) (b) O(log n) (c) O(n
2
) (d) O(n log n) (e) O(n).

Reason: According to the definition of the Omega notation.
Reason: According to the asymptotic notational rules, u notation is equivalence one
Reason: The remaining all belongs to divide and conquer paradigm.
Reason: Because of the memory functions in OBST we calculate c(i, j )s, r(i, j)s and w(i , j)s.
Reason: According to the need of the traverse, Backtracking algorithm uses stack and Branch-and-
bound algorithms uses queue.
Reason: For any null node in the full binary tree it stores zeros.
Reason: According to the asymptotic notational rules.
Reason: According to the definition of Big Oh ( O )
Reason:
1 3 5 7
9 11 14

2 4 8 6
10 12 15 13

Accordingly the EMPTY cell is there in non shaded position and hence value of x is 0,
otherwise value of x is 1.
Reason: As there it sorts the entries in one loop. Because in nondeterministic sorting algorithms the
abstractions are used which cant be programmed.

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set 21 Set
22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set
21 Set 22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>

<< Prev Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set
11 Set 12 Set 13 Set 14 Set 15 Set 16 Set 17 Set 18 Set 19 Set 20 Set 21 Set
22 Set 23 Set 24 Set 25 Set 26 Set 27 Set 28 Set 29 Set 30 Next >>