0% found this document useful (0 votes)
14 views

Objectivequestions DS Example-1

The document discusses various data structures and algorithms. It contains questions and explanations about queues, stacks, linked lists, and time complexities of different operations on these data structures. Common operations include insertion, deletion, traversal, and more.

Uploaded by

Aniket behal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Objectivequestions DS Example-1

The document discusses various data structures and algorithms. It contains questions and explanations about queues, stacks, linked lists, and time complexities of different operations on these data structures. Common operations include insertion, deletion, traversal, and more.

Uploaded by

Aniket behal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

1) A linear list of elements in which deletion can be done from one end (front) and

insertion can take place only at the other end (rear) is known as _____________
a) Queue
b) Stack
c) Tree
d) Linked list
Answer a
2) The data structure required for Breadth First Traversal on a graph is?
a) Stack
b) Array
c) Queue
d) Tree

Answer: c

3). A queue follows __________


a) FIFO (First In First Out) principle
b) LIFO (Last In First Out) principle
c) Ordered array
d) Linear tree

Answer: a

4) Circular Queue is also known as ________

a) Ring Buffer

b) Square Buffer

c) Rectangle Buffer

d) Curve Buffer

Answer: a

Explanation: Circular Queue is also called as Ring Buffer. Circular Queue is a


linear data structure in which last position is connected back to the first position
to make a circle. It forms a ring structure.
5) If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a
time, in what order will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC

Answer: a
Explanation: Queue follows FIFO approach. i.e. First in First Out Approach. So, the
order of removal elements are ABCD.

6) A data structure in which elements can be inserted or deleted at/from both ends but
not in the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue

Answer: c
Explanation: In dequeuer, we can insert or delete elements from both the ends. In
queue, we will follow first in first out principle for insertion and deletion of elements.
Element with least priority will be deleted in a priority queue.
7) A normal queue, if implemented using an array of size MAX_SIZE, gets full when?
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front

Answer: a
Explanation: When Rear = MAX_SIZE – 1, there will be no space left for the elements to
be added in queue. Thus queue becomes full.
8) Queues serve major role in ______________
a) Simulation of recursion
b) Simulation of arbitrary linked list
c) Simulation of limited resource allocation
d) Simulation of heap sort

Answer: c
Explanation: Simulation of recursion uses stack data structure. Simulation of arbitrary
linked lists uses linked lists. Simulation of resource allocation uses queue as first
entered data needs to be given first priority during resource allocation. Simulation of
heap sort uses heap data structure.
9) Which of the following is not the type of queue?
a) Ordinary queue
b) Single ended queue
c) Circular queue
d) Priority queue
10) A linear collection of data elements where the linear node is given by means of
pointer is called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list

Answer: a
Explanation: In Linked list each node has its own data and the address of next node.
These nodes are linked by using pointers. Node list is an object that consists of a list of
all nodes in a document with in a particular selected set of nodes.
11) Consider an implementation of unsorted singly linked list. Suppose it has its
representation with a head pointer only. Given the representation, which of the
following operation can be implemented in O(1) time?

i) Insertion at the front of the linked list


ii) Insertion at the end of the linked list
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list
a)I and II
b) I and III
c) I, II and III
d) I, II and IV

Answer: b
Explanation: We know the head node in the given linked list. Insertion and deletion of
elements at the front of the linked list completes in O (1) time whereas for insertion and
deletion at the last node requires to traverse through every node in the linked list.
Suppose there are n elements in a linked list, we need to traverse through each node.
Hence time complexity becomes O(n).
12) In linked list each node contains a minimum of two fields. One field is data field to
store the data second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
Answer: c
Explanation: Each node in a linked list contains data and a pointer (reference) to the
next node. Second field contains pointer to node.

13) What would be the asymptotic time complexity to add a node at the end of singly
linked list, if the pointer is initially pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
Answer: c
Explanation: In case of a linked list having n elements, we need to travel through every
node of the list to add the element at the end of the list. Thus asymptotic time
complexity is θ(n).
14) What would be the asymptotic time complexity to insert an element at the front of
the linked list (head is known)?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)

Answer: a
Explanation: To add an element at the front of the linked list, we will create a new node
which holds the data to be added to the linked list and pointer which points to head
position in the linked list. The entire thing happens within O (1) time. Thus the
asymptotic time complexity is O (1).
15) What would be the asymptotic time complexity to find an element in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n4)

Answer: b
Explanation: If the required element is in the last position, we need to traverse the entire
linked list. This will take O (n) time to search the element.
16) What would be the asymptotic time complexity to insert an element at the second
position in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)

Answer: a
Explanation: A new node is created with the required element. The pointer of the new
node points the node to which the head node of the linked list is also pointing. The head
node pointer is changed and it points to the new node which we created earlier. The
entire process completes in O (1) time. Thus the asymptotic time complexity to insert an
element in the second position of the linked list is O (1).
17) The concatenation of two lists can be performed in O(1) time. Which of the following
variation of the linked list can be used?
a) Singly linked list
b) Doubly linked list
c) Circular doubly linked list
d) Array implementation of list
Answer: c
Explanation: We can easily concatenate two lists in O (1) time using singly or doubly
linked list, provided that we have a pointer to the last node at least one of the lists. But
in case of circular doubly linked lists, we will break the link in both the lists and hook
them together. Thus circular doubly linked list concatenates two lists in O (1) time.
18) Consider the following definition in c programming language.

struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
Which of the following c code is used to create new node?
a) ptr = (NODE*)malloc(sizeof(NODE));
b) ptr = (NODE*)malloc(NODE);
c) ptr = (NODE*)malloc(sizeof(NODE*));
d) ptr = (NODE)malloc(sizeof(NODE));

19) Which of the following is false about a doubly linked list?


a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list
Answer: d
Explanation: A doubly linked list has two pointers ‘left’ and ‘right’ which enable it to
traverse in either direction. Compared to singly liked list which has only a ‘next’ pointer,
doubly linked list requires extra space to store this extra pointer. Every insertion and
deletion requires manipulation of two pointers, hence it takes a bit longer time.
Implementing doubly linked list involves setting both left and right pointers to correct
nodes and takes more time than singly linked list.

20)What is a memory efficient double linked list?


a) Each node has only one pointer to traverse the list back and forth
b) The list has breakpoints for faster traversal
c) An auxiliary singly linked list acts as a helper list to traverse through the doubly linked
list
d) A doubly linked list that uses bitwise AND operator for storing addresses

Answer: a
Explanation: Memory efficient doubly linked list has only one pointer to traverse the list
back and forth. The implementation is based on pointer difference. It uses bitwise XOR
operator to store the front and rear pointer addresses. Instead of storing actual memory
address, every node store the XOR address of previous and next nodes.
21)How do you calculate the pointer difference in a memory efficient double linked list?
a) head or tail
b) pointer to previous node or pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node

Answer: b
Explanation: The pointer difference is calculated by taking XOR of pointer to previous
node and pointer to the next node.
22) What is the worst case time complexity of inserting a node in a doubly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
Answer: c
Explanation: In the worst case, the position to be inserted maybe at the end of the list,
hence you have to traverse through the entire list to get to the correct position, hence
O(n).

23) How do you insert a node at the beginning of the list?


a)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(node);
head.setNext(node);
size++;
}
b)

public class insertFront(int data)


{
Node node = new Node(data, head, head);
node.getNext().setPrev(node);
head.setNext(node);
size++;
}
c)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(head);
head.setNext(node);
size++;
}
d)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(node);
head.setNext(node.getNext());
size++;
}
Answer: a
Explanation: The new node’s previous pointer will point to head and next pointer will
point to the current next of head.
24) Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list
after performing the given sequence of operations?
Node temp = new Node(6,head,head.getNext());
Node temp1 = new Node(0,tail.getPrev(),tail);
head.setNext(temp);
temp.getNext().setPrev(temp);
tail.setPrev(temp1);
temp1.getPrev().setNext(temp1);
a)head-0-1-2-3-4-5-6-tail
b)head-1-2-3-4-5-6-tail
c)head-6-1-2-3-4-5-0-tail
d) head-0-1-2-3-4-5-tail

Answer: c
Explanation: The given sequence of operations performs addition of nodes at the head
and tail of the list.
25) What is the functionality of the following piece of code?

public int function()


{
Node temp = tail.getPrev();
tail.setPrev(temp.getPrev());
temp.getPrev().setNext(tail);
size--;
return temp.getItem();
}
a) Return the element at the tail of the list but do not remove it
b) Return the element at the tail of the list and remove it from the list
c) Return the last but one element from the list but do not remove it
d) Return the last but one element at the tail of the list and remove it from the list

Answer: b

Explanation: The previous and next pointers of the tail and the last but one element are
manipulated, this suggests that the last node is being removed from the list.

26) Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list
after performing the given sequence of operations?
Node temp = new Node(6,head,head.getNext());

head.setNext(temp);

temp.getNext().setPrev(temp);

Node temp1 = tail.getPrev();

tail.setPrev(temp1.getPrev());

temp1.getPrev().setNext(tail);

a) head-6-1-2-3-4-5-tail

b) head-6-1-2-3-4-tail

c) head-1-2-3-4-5-6-tail

d) head-1-2-3-4-5-tail

Answer: b
Explanation: A new node is added to the head of the list and a node is deleted from the
tail end of the list.

27) In a Doubly Linked list how many nodes have atleast 1 node before and after it?
A. N+1
B. N
C. N-2
D. N-1

Correct Option: C

28) What is Dynamic Allocation in Array?

A. Allocation that takes place at compile time

B.Allocation that take place as bipartite graph


C. memory allocation that takes place during run time rendering the resizing of an

Array

D. All of these

Correct Option: C

29) The height of a BST is given as h. Consider the height of the tree as the no. of

edges in the longest path from root to the leaf. The maximum no. of nodes possible in

the tree is?

A. 2h-1 -1

B. 2h+1 -1

C. 2h +1

D. 2h-1 +1

Correct Option: B

30. Which type of traversal of binary search tree outputs the value in sorted order?

A. Post-order

B. Pre-order

C. In-order

D. None

Correct Option: C

Inorder gives in correct order

31) the run time for traversing all the nodes of a binary search tree with n nodes and

printing them in an order is a) b) c) d)

A. O(n)

B. O(√n)
C. O(log(n))

D. O(nlg(n))

Correct Option: A

32)A binary search tree is generated by inserting in order the following integers: 50, 15,

62, 5, 20, 58, 91, 3, 8, 37, 60, 24 The number of the node in the left sub-tree and right

sub-tree of the root, respectively, is

A. (3, 8)

B. (8, 3)

C. (7, 4)

D. (4, 7)

Correct Option: C

33) Which of the following is the correct postfix format for this infix format A + B – C + D

A. -+DC+BA

B. AB+CD+-

C. +BA-+CD

D. None of these

Correct Option: B

34) Match the following –

Group A Group B

A. Stacks 1. Matrix Multiplication

B. Queue 2. Fastest Search

C. Arrays 3. FIFO
D. Trees 4. LIFO

A. A1, B3, C4, D2

B. A4, B3, C1, D2

C. A4, B3, C2, D1

D. A1, B3, C2, D4

Correct Option: B

35) Priya has a box that looks like a stack and she does the following operations on

empty box

PUSH(8)

PUSH(7)

POP

PUSH(1)

PUSH(3)

A. 31_8

B. 8_1_7

C. 8_1_

D. None of these

Correct Option: C

36) Cloe has x number of stacks her father wants her to write a program that can

perform as a queue. What will you suggest to her how many stacks must she use?

A. 1

B. 2
C. 4

D. 3

Correct Option: B

37)These operations can be performed on which type of structure? Push, Pop, Peek

A. Queue

B. Priority Queue

C.Stack

D. Both 1 and 2

Correct Option: C

Only stack has these queue has enqueue and dequeue

38)Which one of the following is an application of Queue Data Structure?

A. When a resource is shared among multiple consumers.

B. When data is transferred asynchronously (data not necessarily received at same rate

as sent) between two processes

C. Load Balancing

D. All the above

Correct Option: D

Explain :- Queue has many applications like – 1) When a resource is shared among

multiple consumers. Examples include CPU scheduling, Disk Scheduling. 2) When data

is transferred asynchronously (data not necessarily received at same rate as sent)

between two processes. Examples include IO Buffers, pipes, file IO, etc.
39)The retrieval of items in a stack is ……….. operation.

A. push

B. pop

C. retrieval

D. access

Correct Option: B

40)In a queue, the initial values of front pointer f rare pointer r should be …….. and

……….. respectively.

A. -1, -1

B. 0, -1

C. 0, 0

D. -1, 0

Correct Option: A

41)When new data are to be inserted into a data structure, but there is not available

space; this situation is usually called …….

A. Memory Leak

B. Memory Full

C. OverLeak

D. Overflow
Correct Option: D

42)What is a hash function? a)

A. A function has allocated memory to keys

B. A function that computes the location of the key in the array

C. A function that creates an array

D. None of the mentioned

Correct Option: B

In a hash table, there are fewer array positions than the keys, so the position of the key

in the array has to be computed, this is done using the hash function.

43) What is the search complexity in direct addressing?

A. O(n)

B. O(logn)

C. O(nlogn)

D. O(1)

Correct Option: D

Since every key has a unique array position, searching takes a constant time

44)What is the best that can be the techniques to avoid collision?

A. Make the hash function appear random

B. Use the chaining method


C. Use uniform hashing

D. All of the mentioned

Correct Option: B

Making the hash function random is not really a good choice, although it is considered

one of the techniques to avoid collisions along with chaining and simple uniform

hashing. Chaining is the best

45)Consider a hash function that distributes keys uniformly. The hash table size is 20.

After hashing of how many keys will the probability that any new key hashed collides

with an existing one exceed 0.5?

A. 40

B. 2

C. 5

D. 10

Correct Option: D

For each entry probability of collision is 1/20 {as possible total spaces =20, and an entry

will go into only 1 place} Say after inserting x values probability becomes ½ (1/20).x = ½

X=10

46)Which will be the best to implement the following a car comes at a petrol station and

waits. The next car to get its Gas filled should be the one which has waited the longest

time and thus is given priority?

A. Binary Trees
B. Heaps

C. m-way Trees

D. Binary Search Tree

Correct Option: B

47)For a hashing table what is the time complexity?

A. O(1)

B. O(n2)

C. O(log n)

D. O(n)

Correct Option: A

48) Every element of a data structure has an address and a key associated with it.A

search mechanism deals with two or more values assigned to the same address by

using the key. What is this search mechanism?

A. Linear Search

B. Binary search

C. Hash Coded Search

D. None of these

Correct Option: C

49)In a binary max heap containing n numbers, the smallest element can be found in

time?

A. O(n)

B. O(log n)
C. O(log log n)

D. O(1)

Correct Option: A

50)The number of edges from the root to the node is called __________ of the tree.

a) Height

b) Depth

c) Length

d) Width

Answer: b

51)The number of edges from the node to the deepest leaf is called _________ of the

tree.

a) Height

b) Depth

c) Length

d) Width

Answer: a

52)What is a full binary tree?

a) Each node has exactly zero or two children

b) Each node has exactly two children

c) All the leaves are at the same level

d) Each node has exactly one or two children

Answer: a
53)What is a complete binary tree?

a) Each node has exactly zero or two children

b) A binary tree, which is completely filled, with the possible exception of the bottom

level, which is filled from right to left

c) A binary tree, which is completely filled, with the possible exception of the bottom

level, which is filled from left to right

d) A tree In which all nodes have degree 2

Answer: c

54)What is the average case time complexity for finding the height of the binary tree?

a) h = O(loglogn)

b) h = O(nlogn)

c) h = O(n)

d) h = O(log n)

Answer: d

55)Which of the following is not an advantage of trees?

a) Hierarchical structure

b) Faster search

c) Router algorithms

d) Undo/Redo operations in a notepad

Answer:d
Explanation: Undo/Redo operations in a notepad is an application of stack.
Hierarchical structure, Faster search, Router algorithms are advantages of trees.
56)In a full binary tree if number of internal nodes is I, then number of leaves L are?
a)L=2*I
b)L=I+1
c)L=I–1
d) L = 2*I – 1

Answer: b

57)In a full binary tree if number of internal nodes is I, then number of nodes N are?
a)N= 2*I
b)N= I + 1
c)N = I – 1
d) N = 2*I + 1

Answer: d

58) In a full binary tree if there are L leaves, then total number of nodes N are?
a) N = 2*L
b) N = L + 1
c) N = L – 1
d) N = 2*L – 1

Answer: d

59)Which of the following is incorrect with respect to binary trees?


a) Let T be a binary tree. For every k ≥ 0, there are no more than 2k nodes in level k
b) Let T be a binary tree with λ levels. Then T has no more than 2 λ – 1 nodes
c) Let T be a binary tree with N nodes. Then the number of levels is at least ceil(log
(N+1))
d) Let T be a binary tree with N nodes. Then the number of levels is at least floor(log (N
+ 1))

Answer: d

60)What is the maximum number of children that a binary tree node can have?
a) 0
b) 1
c) 2
d) 3

Answer: c
61)The following given tree is an example for?

a) Binary tree
b) Binary search tree
c) Fibonacci tree
d) AVL tree

Answer: a

62) A binary tree is a rooted tree but not an ordered tree.


a) true
b) false

Answer: b

63)How many common operations are performed in a binary tree?


a) 1
b) 2
c) 3
d) 4

Answer: c

64)What is the traversal strategy used in the binary tree?


a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) Priority traversal

Answer: b

65)How many types of insertion are performed in a binary tree?


a) 1
b) 2
c) 3
d) 4
Answer: b

66) What operation does the following diagram depict?

a) inserting a leaf node


b) inserting an internal node
c) deleting a node with 0 or 1 child
d) deleting a node with 2 children

Answer: c

67)General ordered tree can be encoded into binary trees.


a) true
b) false

Answer: a

68)How many bits would a succinct binary tree occupy?


a) n+O(n)
b) 2n+O(n)
c) n/2
d) n

Answer: b

69)The average depth of a binary tree is given as?


a) O(N)
b) O(√N)
c) O(N2)
d) O(log N)

Answer: d
70)How many orders of traversal are applicable to a binary tree (In General)?
a) 1
b) 4
c) 2
d) 3

Answer: d

71)If binary trees are represented in arrays, what formula can be used to locate a left
child,if the node has an index i?
a) 2i+1
b) 2i+2
c) 2i
d) 4i

Answer: a

72) Using what formula can a parent node be located in an array?


a) (i+1)/2
b) (i-1)/2
c) i/2
d) 2i/2

Answer: b

73)Which of the following properties are obeyed by all three tree – traversals?
a) Left subtrees are visited before right subtrees
b) Right subtrees are visited before left subtrees
c) Root node is visited before left subtree
d) Root node is visited before right subtree

Answer :a
74)For the tree below, write the pre-order traversal.

a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9

Answer: a

75)For the tree below, write the post-order traversal.

a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9

Answer: c

76)Select the code snippet which performs pre-order traversal.


a)
public void preorder(Tree root)
{
System.out.println(root.data);
preorder(root.left);
preorder(root.right);
}
b)

public void preorder(Tree root)


{
preorder(root.left);
System.out.println(root.data);
preorder(root.right);
}
c)

public void preorder(Tree root)


{
System.out.println(root.data);
preorder(root.right);
preorder(root.left);
}
d)

public void preorder(Tree root)


{
preorder(root.right);
preorder(root.left);
System.out.println(root.data);
}
Answer: a
77)What is the time complexity of pre-order traversal in the iterative fashion?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)

Answer: b

78) What is the space complexity of the post-order traversal in the recursive fashion? (d
is the tree depth and n is the number of nodes)
a) O(1)
b) O(nlogd)
c) O(logd)
d) O(d)
Answer: d

79) To obtain a prefix expression, which of the tree traversals is used?


a) Level-order traversal
b) Pre-order traversal
c) Post-order traversal
d) In-order traversal

Answer: b

80)Consider the following data. The pre order traversal of a binary tree is A, B, E, C, D.
The in order traversal of the same binary tree is B, E, A, D, C. The level order sequence
for the binary tree is _________
a) A, C, D, B, E
b) A, B, C, D, E
c) A, B, C, E, D
d) D, B, E, A, C

Answer: b

81)Consider the following data and specify which one is Preorder Traversal Sequence,
Inorder and Postorder sequences.
S1: N, M, P, O, Q
S2: N, P, Q, O, M
S3: M, N, O, P, Q
a) S1 is preorder, S2 is inorder and S3 is postorder
b) S1 is inorder, S2 is preorder and S3 is postorder
c) S1 is inorder, S2 is postorder and S3 is preorder
d) S1 is postorder, S2 is inorder and S3 is preorder

Answer: c

82. What is an external sorting algorithm?


a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
Answer: a
Explanation: As the name suggests, external sorting algorithm uses external memory
like tape or disk.
83. What is an internal sorting algorithm?
a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
View Answer

Answer: b
Explanation: As the name suggests, internal sorting algorithm uses internal main
memory.

84. What is the worst case complexity of bubble sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

Answer: d
Explanation: Bubble sort works by starting from the first element and swapping the
elements if required in each iteration.

85. What is the average case complexity of bubble sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

Answer: d
Explanation: Bubble sort works by starting from the first element and swapping the
elements if required in each iteration even in the average case.

86. What is the best case efficiency of bubble sort in the improvised version?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer:-Answer: c
Explanation: Some iterations can be skipped if the list is sorted, hence efficiency
improves to O(n).
87. What is the worst case complexity of selection sort?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

Answer: d
Explanation: Selection sort creates a sub-list, LHS of the ‘min’ element is already sorted
and RHS is yet to be sorted. Starting with the first element the ‘min’ element moves
towards the final element.

88. What is the best case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

Answer: d
Explanation: The best, average and worst case complexities of selection sort is O(n 2).
(n-1) + (n-2) + (n-3) + …. + 1 = (n(n-1))/2 ~ (n2)/2.

89. How many passes does an insertion sort algorithm consist of?
a) N
b) N-1
c) N+1
d) N2

Answer: b
Explanation: An insertion algorithm consists of N-1 passes when an array of N elements
is given.

90. What is the average case running time of an insertion sort algorithm?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: d
Explanation: The average case analysis of a tight bound algorithm is mathematically
achieved to be O(N2).
91. Any algorithm that sorts by exchanging adjacent elements require O(N2) on
average.
a) True
b) False
View Answer

Answer: a
Explanation: Each swap removes only one inversion, so O(N2) swaps are required.

92. What is the running time of an insertion sort algorithm if the input is pre-sorted?
a) O(N2)
b) O(N log N)
c) O(N)
d) O(M log N)
View Answer

Answer: c
Explanation: If the input is pre-sorted, the running time is O(N), because the test in the
inner for loop always fails immediately and the algorithm will run quickly.

93. What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
View Answer

Answer: b
Explanation: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.

94. For the following question, how will the array elements look like after second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21
View Answer

Answer: d
Explanation: After swapping elements in the second pass, the array will look like, 8, 34,
64, 51, 32, 21.
95. Which of the following real time examples is based on insertion sort?
a) arranging a pack of playing cards
b) database scenarios and distributes scenarios
c) arranging books on a library shelf
d) real-time systems
View Answer

Answer: a
Explanation: Arranging a pack of cards mimics an insertion sort.

96. For the best case input, the running time of an insertion sort algorithm is?
a) Linear
b) Binary
c) Quadratic
d) Depends on the input
View Answer

Answer: a
Explanation: The best case input for an insertion sort algorithm runs in linear time and is
given by O(N).

97. Which of the following examples represent the worst case input for an insertion
sort?
a) array in sorted order
b) array sorted in reverse order
c) normal unsorted array
d) large array
Answer: b
Explanation: The worst case input for an insertion sort algorithm will be an array sorted
in reverse order and its running time is quadratic.

98. Merge sort uses which of the following technique to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
Answer: c
Explanation: Merge sort uses divide and conquer in order to sort a given array. This is
because it divides the array into two halves and applies merge sort algorithm to each
half individually after which the two sorted halves are merged together.
99. What is the average case time complexity of merge sort?
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a

100. What is the auxiliary space complexity of merge sort?


a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
View Answer

Answer: c
Explanation: An additional space of O(n) is required in order to merge two sorted arrays.

101. Merge sort can be implemented using O(1) auxiliary space.


a) true
b) false
Answer: a
Explanation: Standard merge sort requires O(n) space to merge two sorted arrays. We
can optimize this merging process so that it takes only constant space. This version is
known as in place merge sort.

102. What is the worst case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a
Explanation: The time complexity of merge sort is not affected by worst case as its
algorithm has to implement the same number of steps in any case. So its time
complexity remains to be O(n log n).

103. Which of the following method is used for sorting in merge sort?
a) merging
b) partitioning
c) selection
d) exchanging
Answer: a
Explanation: Merge sort algorithm divides the array into two halves and applies merge
sort algorithm to each half individually after which the two sorted halves are merged
together. Thus its method of sorting is called merging.

104. What will be the best case time complexity of merge sort?
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a
Explanation: The time complexity of merge sort is not affected in any case as its
algorithm has to implement the same number of steps. So its time complexity remains
to be O(n log n) even in the best case.

105. Which of the following is not in place sorting algorithm by default?


a) merge sort
b) quick sort
c) heap sort
d) insertion sort
Answer: a
Explanation: Quick sort, heap sort, and insertion sort are in-place sorting algorithms,
whereas an additional space of O(n) is required in order to merge two sorted arrays.
Even though we have a variation of merge sort (to do in-place sorting), it is not the
default option. So, among the given choices, merge sort is the most appropriate answer.

106. Quick sort follows Divide-and-Conquer strategy.


a) True
b) False
Answer: a
Explanation: In quick sort, the array is divided into sub-arrays and then it is sorted
(divide-and-conquer strategy).

107. What is the worst case time complexity of a quick sort algorithm?
a) O(N)
b) O(N log N)
c) O(N2)
d) O(log N)
Answer: c
Explanation: The worst case performance of a quick sort algorithm is mathematically
found to be O(N2).
108. What is the average running time of a quick sort algorithm?
a) O(N2)
b) O(N)
c) O(N log N)
d) O(log N)
Answer: c
Explanation: The best case and average case analysis of a quick sort algorithm are
mathematically found to be O(N log N).

109. How many sub arrays does the quick sort algorithm divide the entire array into?
a) one
b) two
c) three
d) four
Answer: b
Explanation: The entire array is divided into two partitions, 1st sub array containing
elements less than the pivot element and 2nd sub array containing elements greater
than the pivot element.

110. On which algorithm is heap sort based on?


a) Fibonacci heap
b) Binary tree
c) Priority queue
d) FIFO
Answer: c
Explanation: Heap sort is based on the algorithm of priority queue and it gives the best
sorting time.

111. In what time can a binary heap be built?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: a
Explanation: The basic strategy is to build a binary heap of N elements which takes
O(N) time.
112. Consider the following heap after buildheap phase. What will be its corresponding
array?

a) 26,53,41,97,58,59,31
b) 26,31,41,53,58,59,97
c) 26,41,53,97,31,58,59
d) 97,53,59,26,41,58,31
Answer: d
Explanation: Constructing a max heap using the elements 97,53,59,26,41,58,31 will
cause the heap to look like that.

113. In what position does the array for heap sort contains data?
a) 0
b) 1
c) -1
d) anywhere in the array
Answer: a
Explanation: The array for heap sort contains data at position 0 whereas for a binary
heap, array begins at 1. This is the reason for its complexity.

114. What is the typical running time of a heap sort algorithm?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: b
Explanation: The total running time of a heap sort algorithm is mathematically found to
be O(N log N).
115. What is the time taken to perform a delete min operation?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: c
Explanation: The time taken to perform a deletion of a minimum element is
mathematically found to be O( log N).

You might also like