DSANotes
DSANotes
DSANotes
1. Let A be an array containing integer values. The distance of A is defined as the minimum number of elements
in A that must be replaced with another integer so that the resulting array is sorted in non-decreasing order.
The distance of the array [2,5,3,1,4,2,6] is _____3______.
Given array: [2, 5, 3, 1, 4, 2, 6]
Indices: 0 1 2 3 4 5 6
Values: 2 5 3 1 4 2 6
The array with sorted indices: positions different from given array is 3
Indices: 0 1 2 3 4 5 6
Values: 2 2 3 3 4 4 6
2. What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array
of size n?
A) Θ(sqrt(n))
B) Θ(log(n))
C) Θ(n^2)
D) Θ(n)
3. Let P be an array containing n integers. Let t be the lowest upper bound on the number of comparisons of the
array elements, required to find the minimum and maximum values in an arbitrary array of n elements. Which
one of the following choices is correct?
A) t > 2n−2
B) t > 3⌈n/2⌉ and t ≤ 2n−2
C) t > n and t ≤ 3⌈n/2⌉
D) t > ⌈logn⌉ and t ≤ n
We will use the tournament approach to get the min and max elements. (Array element is analogous to Player)
Considering there are n players in the tournament, the first round will have n/2 matches. A player is considered to be the
winner if he has a higher rank than his opponent. In this stage we did 1 comparison for selecting winner or loser from 1
match, so total n/2 comparison for n/2 matches.
After the first round, we will keep all the losers of first round in one area and all the winners in the second area. Note
how each area will have n/2 players in them.
Now from the losers pool we will find the minimum rank player by doing individual comparison. Since this pool has
only n/2 players we can find the player with lowest rank by doing (n/2)–1 comparisons. This lowest rank player
corresponds to the minimum array in the element.
Similarly we will find the highest rank player from the pool of winners by doing (n/2)–1 comparisons again. The highest
rank player corresponds to the maximum element in the array.
Hence total comparisons needed to find minimum and maximum = t = (n/2) + ((n/2)+1) + ((n/2)-1) = 3n/2 -2
4. If an array A contains the items 10,4,7,23,67,12 and 5 in that order, what will be the resultant array A after
third pass of insertion sort?
A) 67,12,10,5,4,7,23
B) 4,7,10,23,67,12,5
C) 4,5,7,67,10,12,23
D) 10,7,4,67,23,12,5
Pass 1 will be applied on the first 2 elements. 10,4 ∣ ,7,23,67,12,5 Pass 1:4,10 ∣ ,7,23,67,12,5
We can move from right to left, while keeping a note of the maximum element so far (let's call it current_max).
Starting from the rightmost element, we initialize our current_max with it, since the rightmost element will always be
a leader.
Moving from right to left, if an element x is greater than our current_max, then x is also a leader. Add this element to
list of leaders (or simply print it). Set current_max to x and carry-on leftward. O(n) linear Tc.
6. A one-dimensional array A has indices 1....75. Each element is a string and takes up three memory words.
The array is stored at location 1120 decimal. The starting address of A [49] is
A. 1267
B. 1164
C. 1264
D. 1169
7. Let A(1:8,−5:5,−10:5) be a three dimensional array. How many elements are there in the array A?
A. 1200
B. 1408
C. 33
D. 1050
8. In an array of 2N elements that is both 2-ordered and 3-ordered, what is the maximum number of positions
that an element can be from its position if the array were 1-ordered?
A. 1
B. 2
C. n/2
D. 2n−1 An array A [ 1 … n] is said to be k-ordered if
A [i - k] < A [ i ] < A [i + k] for each i such that k < i < n – k. if the array were 1-ordered
then array will be 1 2 3 4 5 6 7 8
A. 3.00
B. 3.46
C. 2.81
D. 3.33
10 20 30 40 50 60 70 80 90 100 110
Now apply binary search in the given array .For convenience we are using Binary search tree.
Now binary search continue either in lower or in upper halve for searching .
For upper halve (70 80) 90 (100 110) and continued in same way.
10. The average number of key comparisons required for a successful search for sequential search on n items is
A. n/2
B. (n−1)/2
C. (n+1)/2
D. None of the above
Expected number of comparisons = 1× Probability of first element being x+2× Probability of second element being x+
…+n× Probability of last element being x
=1/n+2/n+3/n+…+n/n =(n×(n+1)/2)/n = (n+1)/2
11. A program P reads in 500 integers in the range [0,100] representing 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
our area of interest is only the 50 numbers so take An array of 50 numbers where A[0] corresponds
to 51...A[49] corresponds to 100 then after reading an input just increment the counter in correct position
12. An array A consists of n integers in locations A[0],A[1],…A[n−1]. It is required to shift the elements of the
array cyclically to the left by k places, where 1<=k<=(n−1). An incomplete algorithm for doing this in linear
time, without using another array is given bellow. Complete the algorithm by filling in the blanks.
1. min=n; i=0;
2. while(_________) {
4. while(_________) {
5. A[j]= _______;
6. j=(j+k) mod n;
7. if(j<min) then
8. min = j;
9. }
11. i=________;
12.
13. }
char A[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory
address 0, the address of A[40][50] is:
A. 4040
B. 4050
C. 5040
D. 5050
Now you want to go and meet a friend who lives on 50th building of 40th street means A[40][50]
So, firstly cross 39(0−39) streets each consist of 100 buildings: 40×100=4000
Now cross 49(0−49) buildings in order to reach your destination: 50 4000+ 50 4050 answer
A. 15i+j+84
B. 15j+i+84
C. 10i+j+89
The array indices started with 1 and it is in Row major order: Base Address + Size * [ (i-1)*n + (j-1)]
The array indices started with 0 and it is in Row major order : Base Address + Size * [ i*n + j ]
The array indices started with 1 and it is in Column major order: Base Address + Size * [ (j-1)*m + (i-1)]
The array indices started with 0 and it is in Column major order : Base Address + Size * [ j*m + i ]
15. Suppose we want to arrange the n numbers stored in any array such that all negative values occur before all
positive ones. Minimum number of exchanges required in the worst case is
A. n−1
B. n
C. n+1
D. None of the above answer is n/2 We just require n/2 swaps in the worst case.
A. 0
B. n−1
C. n^2 − 3n + 2
D. n^2 (n+1)/2
17. Suppose you are given an array A[1....n] and a procedure reverse (s,I,j) which reverses the order of elements
in s between positions i and j (both inclusive). What does the following sequence do, where 1 ⩽ k ⩽ n:
Effect of the above 3 reversals for any K is equivalent to left rotation of the array of size n by k.
Let , S[1…7]=1234567
So, n=7, k=2
18. Consider the following algorithm for searching for a given number x in an unsorted
array A[1..n] having � distinct values:
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−1
C. 2n
D. n/2
19. In a compact single dimensional array representation for lower triangular matrices (i.e all the elements above
the diagonal are zero) of size n×n, non-zero elements, (i.e elements of lower triangle) of each row are stored
one after another, starting from the first row, the index of the (i,j)th element of the lower triangular matrix in
this new representation is:
A) i+j
B) i+j−1
C) (j−1)+i(i−1)/2
D) i+j(j−1)/2
Whole trick to this question is to guess if array start with 1 or zero. As they have not mentioned you have to consider
both the cases and then go by the concept of row major.
First case:
if array start with zero then we have to cross n rows and j number of element in the last column. As each row will be
containing elements in increasing order like 1st row will contain 1 non zero element. 2 will 2 non zero. So total number
of element we have to cross can be represented as the sum of natural numbers.
so if array start with zero you will can get address with i(i+1)/2 + j
similarly if it start with 1 replace i and j with (i-1) and j-1 respectively.
21. Linked lists are not suitable data structures for which one of the following problems?
A) Insertion sort
B) Binary search Because finding mid element itself takes O(n) time.
C) Radix sort
D) Polynomial manipulation
22. For merging two sorted lists of sizes m and n into a sorted list of size m+n, we require comparisons of
A) O(m)
B) O(n)
C) O(m+n)
D) O(logm+logn)
The number of moves are however always m+n so that we can term it as Θ(m+n). But the number of comparisons varies as per
the input. In the best case, the comparisons are min(m,n) and in worst case they are m+n−1.
24. The concatenation of two lists is to be performed on O(1) time. Which of the following implementations of a
list should be used?
A) Singly linked list
B) Doubly linked list
C) Circular doubly linked list
D) Array implementation of list
25. In the worst case, the number of comparisons needed to search a single linked list of length n for a given
element is
A) logn
B) n/2
C) logn−1
D) n
Worst case is we do not find the element in list. We might end up searching entire list & comparing with each element
26. Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-case
time complexity of the best-known algorithm to delete the node x from the list ?
A) O(n)
B) O((logn)^2)
C) O(logn)
D) O(1)
28. Which of the following operations is performed more efficiently by doubly linked list than by linear linked
list?
29. The minimum number of fields with each node of doubly linked list is
A. 1
B. 2
C. 3 prev data next
D. 4
30. The following steps in a linked list result in which type of operation?
p = getnode() you are allocating a space for a new node that you want to create
info(p) = 10 you are setting the info value to 10
next (p) = list you are updating list Field
list = p
31. Consider a single linked list where F and L are pointers to the first and last elements respectively of the linked
list. The time for performing which of the given operations depends on the length of the linked list?
In this we need to get the address of the node just before the last node, because let address of node before the last node
is (1000) then we need to set this address to (1000) −>next=NULL. So calculating this address we need to linear search the
linked list and so it depends on the length of the linked list.
32. An unordered list contains n distinct elements. The number of comparisons to find an element in this list that
is neither maximum nor minimum is
A. Θ(nlogn)
B. Θ(n)
C. Θ(logn)
D. Θ(1) Because all elements are distinct, select any three numbers and output 2nd largest from them.
33. N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to
be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be
performed.
A. O((logN)^2)
B. O(N)
C. O(N^2) correct is O(NlogN)
D. Θ(N^2logn)
Delete - θ(1) time as pointer is directly given
Insert - O(N) time,we may need to insert at the end of the sorted list..
Find - θ(N) time.. list we need to search sequentially..
Decrease key - θ(N) time, pointer is directly given, delete then insert
Assuming that m and n point to valid NULL-terminated linked lists, invocation of join will
35. Consider a singly linked list of the form where F is a pointer to the first element in the linked list and L is the
pointer to the last element in the list. The time of which of the following operations depends on the length of
the list?
37. What is the worst case time complexity of inserting n elements into an empty linked list, if the linked list
needs to be maintained in sorted order?
A. Θ(n)
B. Θ(nlogn)
C. Θ(n)^2
D. Θ(1)
"Inserting n elements into an empty linked list, list needs to be maintained in sorted order".
It is not mentioned if the "n elements" are given initially itself or we must insert one element at a time.
1. For the former case, we can just sort the initial n element array - can be done in O(nlogn) and then we'll insert them one
by one to the linked list where the sorted order is always maintained. This process will take O(n) and so the entire
process can be done in Θ(nlogn) time.
2. For the latter case, where we have to do insertion to the linked list one element at a time, the only way to ensure sorted
order for the linkedlist is to follow insertion sort mechanism. This will take Θ(n^2) time in the worst case.
Since, the question is ambiguous in saying how the elements are presented, both options B and C should be given marks.
Unfortunately official key considered later case only. Option C.
38. Consider the problem of reversing a singly linked list. To take an example, given the linked list below,
Which one of the following statements is TRUE about the time complexity of algorithms that solve the above
problem in O(1) space?
A. The best algorithm for the problem takes Θ (n) time in the worst case.
B. The best algorithm for the problem takes Θ (nlogn) time in the worst case.
C. The best algorithm for the problem takes Θ (n^2) time in the worst case.
D. It is not possible to reverse a singly linked list in O(1) space.
Suppose you are given a linkedlist like this –
Which means, you just want to change pointer of node that is pointed by current.
39. Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to
the head of the list. Similarly, let DLLdel be another function that deletes a node in a doubly-linked list given
a pointer to the node and a pointer to the head of the list.
Let n denote the number of nodes in each of the linked lists. Which one of the following choices
is TRUE about the worst-case time complexity of SLLdel and DLLdel?
40. Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table
below.
⟹3+1+5∗2/7+2−4−7−6/2 ⟹(3+1)+5∗2/7+2−4−7−6/2
⟹(4+5)∗2/7+2−4−7−6/2 ⟹9∗2/(7+2)−4−7−6/2
⟹9∗2/9−4−7−6/2 ⟹9∗2/9−4−(7−6)/2
⟹9∗2/9−(4−1)/2 ⟹9∗2/(9−3)/2
⟹9∗2/6/2 ⟹(9∗2)/6/2
Queue
42. A stack is implemented with an array of ′A[0...N−1]′ and a variable ‘pos’. The push and pop operations are
defined by the following code.
1. push (x)
2. A[pos] <- x
4. end push
5. pop()
7. return A[pos]
8. end pop
Which of the following will initialize an empty stack with capacity N for the above implementation
A. pos←−1
B. pos←0
C. pos←1
D. pos←N−1
Stack is growing downwards, and on pushing an item, the top [pos] is decrementing by one, meaning the on the topmost position
[pos] will be 0, or (N - 1 - (N - 1)], so the initial value of [pos] will be N – 1
A. (A−B)∗C+(D∗E)−(F+G)
B. (((A+B)∗C)−((D−E)∗(F+G)))
C. (A+B-C)∗(D−E)∗(F+G)
D. (A+B)∗C−(D∗E)−(F+G)
(a+(b-c))*((d-e)/(f+g-h))
A. *+a-bc/-de-+fgh
B. *+a-bc-/de-+fgh
C. *+a-bc/-ed-+fgh
D. *+ab-c/-de-+fgh
E. (a+(b−c))∗((d−e)/(f+g−ℎ)) (a+(b−c))∗((−de)/((+fg)−ℎ)) (a+(−bc))∗((−de)/(−+fgℎ))
(+a−bc)∗(/−de−+fgℎ) ∗+a−bc/−de−+fgℎ
45. The best data structure to check whether an arithmetic expression has balanced parenthesis is a:
A. Queue
B. Stack Balanced Parenthesis is one of the application of Stack.
C. Tree
D. List
46. If the sequence of operations - push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop are
performed on a stack, the sequence of popped out values
A. 2,2,1,1,2
B. 2,2,1,2,2
C. 2,1,2,2,1
D. 2,1,2,2,2 After pushing some element in stack when we do pop , we get element in LIFO order
A. 284
B. 213
C. 142 (10 + 5) * (60 / 6) - 8 = 15 * 10 - 8 = 142
D. 71
48. The five items: A, B, C, D, and E are pushed in a stack, one after other starting from A. The stack is popped four items
and each element is inserted in a queue. The two elements are deleted from the queue and pushed back on the stack.
Now one item is popped from the stack. The popped item is
A. A
B. B
C. C
D. D
Initially A, B, C, D, and E are pushed in a stack, one after other starting from A.
Four element is inserted in a queue. The two elements are deleted from the queue
Two elements are pushed back on the stack.Now one item is popped from the stack. The popped item is D.
49. Stack A has the entries a, b, c (with a on top). Stack B is empty. An entry popped out of stack A can be printed
immediately or pushed to stack B. An entry popped out of the stack B can be only be printed. In this
arrangement, which of the following permutations of a, b, c are not possible?
A. bac
B. bca
C. cab
D. abc
If a stack S contains the integers 2,−3,2,−1,2 in order from bottom to top, what is f(S)?
A. 6
B. 4
C. 3
D. 2
51. 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 (top1<top2) 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
B. top1+top2=MAXSIZE
C. (top1=MAXSIZE/2) or (top2=MAXSIZE)
D. top1=top2−1
Since the stacks are growing from opposite ends, initially top1=1 and top2=MAXSIZE. Now, to make the space usage
most efficient we should allow one stack to use the maximum possible space as long as other stack doesn't need it. So,
either of the stack can grow as long as there is space on the array and hence the condition must be top1=top2−1.
53. Which of the following is essential for converting an infix expression to the postfix form efficiently?
A. An operator stack
B. An operand stack
C. An operand stack and an operator stack
D. A parse tree
When we evaluate a given postfix expression, then we only push the operands into the stack but not any operator. So, the stack used
in postfix evaluation called operand stack.
When we convert an infix notation into postfix notation, then we only push the operators into the stack but not any operand. So,
stack used in the conversion from infix to postfix is called operator stack.
54. Which of the following permutations can be obtained in the output (in the same order) using a stack assuming
that the input is the sequence 1, 2, 3, 4, 5 in that order?
A. 3, 4, 5, 1, 2
B. 3, 4, 5, 2, 1
C. 1, 5, 2, 3, 4
D. 5, 4, 3, 1, 2 1 is the last element of the stack which was popped out in output so check
in given options which option have last pop as 1
55. Which of the following data structure is useful in traversing a given graph by breadth first search?
A. Stack
B. Queue Breadth-first search uses a Queue data structure.
C. List
D. None of the above
56. The queue data structure is to be realized by using stack. The number of stacks needed would be
A. It cannot be implemented
B. 2 stacks A queue can be implemented using two stacks.
C. 4 stacks
D. 1 stack
B. Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for
almost all the basic LIST operations.
58. A circular queue has been implemented using a singly linked list where each node consists of a value and a
single pointer pointing to the next node. We maintain exactly two external pointers FRONT and REAR
pointing to the front node and the rear node of the queue, respectively. Which of the following statements
is/are CORRECT for such a circular queue, so that insertion and deletion operations can be performed
in O(1) time?
I. Next pointer of front node points to the rear node.
II. Next pointer of rear node points to the front node.
A. (I) only.
B. (II) only.
C. Both (I) and (II).
D. Neither (I) nor (II).
59. A priority queue Q is used to implement a stack that stores characters. PUSH (C) is implemented as
INSERT (Q,C,K) where K is an appropriate integer key chosen by the implementation. POP is implemented
as DELETEMIN(Q). For a sequence of operations, the keys chosen are in
A. non-increasing order
B. non-decreasing order
Implementing stack using priority queue require first element inserted in stack will be deleted at last, and to
implement it using deletemin() operation of queue will require first element inserted in queue must have highest
priority.
60. Consider a standard Circular Queue implementation (which has the same condition for Queue Full and Queue
Empty) whose size is 11 and the elements of the queue are q[0],q[1],…q[10].
The front and rear pointers are initialized to point at q[2]. In which position will the ninth element be added?
A. q[0]
B. q[1]
C. q[9]
D. q[10]
For enqueue, we increment the REAR pointer and then insert an element
For dequeue, we increment the FRONT and then remove the element at that position.
For empty check, when FRONT == REAR, we declare queue as empty
61. A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed
efficiently. Which one of the following statements is CORRECT (n refers to the number of items in the
queue) ?
Which one of the following is the time complexity of the most time-efficient implementation of 'enqueue' and '
dequeue, respectively, for this data structure?
A. Θ(1),Θ(1)
B. Θ(1),Θ(n)
C. Θ(n),Θ(1)
D. Θ(n),Θ(n)
Time Complexity O(1) Because only pointer manipulation is involved which takes constant time in insertion
Time Complexity = Time for Traversing list, free the last node and keep track of Tail pointer O(n)
63. Consider a complete binary tree with 7 nodes. Let A denote the set of first 3 elements obtained by performing
Breadth-First Search (BFS) starting from the root. Let B denote the set of first 3 elements obtained by
performing Depth-First Search (DFS) starting from the root.
The value of ∣A-B∣ is __1_____ A-B= the remaining node from the set of level2 nodes.
64. The postorder traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1. The inorder traversal of the same tree
is 8,6,9,4,7,2,5,1,3. The height of a tree is the length of the longest path from the root to any leaf. The height
of the binary tree above is ___4__
A. logn nodes
B. n+1 nodes
C. 2n nodes
D. 2n+1 nodes
67. Consider a binary tree T that has 200 leaf nodes. Then the number of nodes in T that have exactly two children
are ______. The general formula for a Binary tree having two children with n leaves is=> n-1.
Here n = 200 So answer is 199.
1. A binary tree T has 20 leaves. The number of nodes in T having two children is ___19___. (n-1)
2. How many different trees are there with four nodes A, B, C and D?
A) 30
B) 60
C) 90
D) 120 formula F(n) = (n+1)!
No of spanning tree of complete graph with n nodes=No of unrooted labeled tree with n nodes=cayley formula=n^n-2
The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum number of nodes in a
binary tree of height 5 are
A. 63 and 6, respectively
B. 64 and 5, respectively
C. 32 and 6, respectively
D. 31 and 5, respectively Option A is correct because height 5 means level 6
so maximum node 2level - 1 63for minimum, at each level only single node so total 6
Consider a rooted n node binary tree represented using pointers. The best upper bound on the time required to determine
he number of subtrees having exactly 4 nodes is O(nalogba). Then the value of a+10b is ______1____ We need to
raverse all nodes at least once, and we need only one traversal. O(n); a=1, b=0
Which of the following number of nodes can form a full binary tree?
A) 8
B) 15 1+2+4+8
C) 14
D) 13
The in-order traversal of a tree resulted in FBGADCE. Then the pre-order traversal of that tree would result in
A. FGBDECA
B. ABFGCDE
C. BFGCDEA
D. AFGBDEC
n 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 C. 1
B. (n−1)/2 D. n−1
0 because every node has an odd number of descendants so least odd number 1 and every node is considered to be its own
descendant so all nodes have even number of descendants (0,2,4,6...) so every node has either 0 children or 2 children...
A. n nodes
B. log2n nodes
C. 2n−1 A Full binary tree with n leaves contains 2−1 nodes.
D. 2n nodes
76. A complete binary tree with the property that the value at each node is at least as large as the values at its
children is known as
Heap or (max heap) which is a complete binary tree with every node has value more than or equal to its children
77. The maximum number of binary trees that can be formed with three unlabelled nodes is:
A. 1
B. 5
C. 4
D. 3
No. of unlabelled binary trees for n nodes (2n)! / ( (n+1)! *(n!)) here n=3
78. 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 ℎ is:
A. 2ℎ−1
B. 2ℎ−1−1
D. 2ℎ+1
79. In a binary tree, the number of internal nodes of degree 1 is 5, and the number of internal nodes of
degree 2 is 10. The number of leaf nodes in the binary tree is
A. 10
B. 11
C. 12
D. 15
We are given no. of 1 degree node =5, no. of 2 degree nodes =10.
Total no. of edges =1∗5+2∗10=25 (In tree degree is for outgoing edges only, and hence each degree corresponds
to an edge)
So, total no. of nodes =25+1=26 (No. of nodes in a tree is 1 more than no. of edges).
A. Overlaying is used to run a program, which is longer than the address space of the computer.
B. Optimal binary search tree construction can be performed efficiently by using dynamic
programming.
C. Depth first search cannot be used to find connected components of a graph.
D. Given the prefix and postfix walls over a binary tree, the binary tree can be uniquely constructed
81. A binary tree T has n leaf nodes. The number of nodes of degree 2 in T is
A. Log2n
B. n−1
C. n
D. 2n
For the tree, the degree of a node is defined as the number of sub-trees of the node or no of children of a node.
82. Which of the following sequences denotes the post order traversal sequence of the below tree?
A. fegcdba
B. gcbdafe
C. gcdbfea Left → Right → Root.
D. fedgcba
84. 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?
A. LASTIN = LASTPOST
B. LASTIN = LASTPRE
C. LASTPRE = LASTPOST
D. None of the above
Therefore, only Option D matches (as Question ask, which always TRUE)
85. Level order traversal of a rooted tree can be done by starting from the root and performing
A. preorder traversal
B. in-order traversal
C. depth first search
D. breadth first search Level order traversal of a tree is breadth first traversal for the tree.
86. Suppose a binary search tree with 1000 distinct elements is also a complete binary tree. The tree is stored using
the array representation of binary heap trees. Assuming that the array indices start with 0, the 3rd largest element of
the tree is stored at index _____509_________
Log21000 +1 9 +1 10 levels 210 – 2 1022 level is not fully completed as 0-999 is made
29= 512 range is 511 to 999 on 10th level level 9 ends on 510 3rd largest node is 510-1 509
87. A binary search tree 𝑇 contains 𝑛 distinct elements. What is the time complexity of picking an element in 𝑇 that
is smaller than the maximum element in 𝑇?
A. Θ(𝑛log𝑛)
B. Θ(𝑛)
C. Θ(log𝑛)
D. Θ(1)
we only need to compare any two elements We don’t need to find the “Second Largest Number”. We need ANY number which is not
the largest. So, take ANY two elements & the smaller in them is your answer.
88. What is the in-order successor of 15 in the given binary search tree?
A. 18
B. 6
C. 17
D. 20
89. The preorder traversal of a binary search tree is 15,10,12,11,20,18,16,19. Which one of the following is the
postorder traversal of the tree?
A. 10,11,12,15,16,18,19,20
B. 11,12,10,16,19,18,20,15 PreOrder data left right
C. 20,19,18,16,15,12,11,10 InOrder left data right
D. 19,16,18,20,11,12,10,15 PostOrder left right data
In Pre-order, the first node is ROOT. So root is 15. In Post-order, the last node is ROOT. So in the Post-order
sequence, 15 must be at last. In Pre-order, the second node is the left child of ROOT, if it is less than the root.
90. Let 𝑇 be a binary search tree with 15 nodes. The minimum and maximum possible heights of 𝑇 are:
A. 4 and 15 respectively.
B. 3 and 14 respectively. Minimum log2(n+1) - 1 Maximum n-1
C. 4 and 14 respectively.
D. 3 and 15 respectively.
91. While inserting the elements 71,65,84,69,67,83 in an empty binary search tree (BST) in the sequence shown,
the element in the lowest level is
A. 65
B. 67
C. 69
D. 83
92. What are the worst-case complexities of insertion and deletion of a key in a binary search tree?
93. Which of the following is/are correct in order traversal sequence(s) of binary search tree(s)?
I. 3,5,7,8,15,19,25
II. 5,8,9,12,10,15,25
III. 2,7,10,8,14,16,20
IV. 4,6,7,9,18,20,25 In order traversal of key are always in ascending order.
A. I and IV only
B. II and III only
C. II and IV only
D. II only
94. Consider the following binary search tree T given below: Which node contains the fourth smallest element in T?
A. Q
B. V
C. W In order traversal of key are always in ascending order. UQXWPVZY
D. X
1. Best 𝐶𝑎𝑠𝑒=𝑂(1) , When it is smallest/greatest element and BST contains only all greater/smaller element than inserting
element respectively.
2. Avg 𝐶𝑎𝑠𝑒=𝑂(log𝑛) , When it belongs between some elements .
3. Worst 𝐶𝑎𝑠𝑒=𝑂(𝑛) , When it is smallest/greatest element and BST contains only all smaller/greater element than inserting
element respectively.
95. How many distinct binary search trees can be created out of 4 distinct keys?
A. 5
B. 14
C. 24
D. 35
96. 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)?
A. 2
B. 3
C. 4
D. 6
A. The cost of searching an AVL tree is Θ(log𝑛) but that of a binary search tree is 𝑂(𝑛)
B. The cost of searching an AVL tree is Θ(log𝑛) but that of a complete binary tree is Θ(𝑛log𝑛)
C. The cost of searching a binary search tree is 𝑂(log𝑛) but that of an AVL tree is Θ(𝑛)
D. The cost of searching an AVL tree is Θ(𝑛log𝑛) but that of a binary search tree is O(n)
AVL tree is a balanced search tree that has time complexity of searching Θ(log𝑛), but in binary search tree, we can have a completely
left/right skewed tree, in which search is 𝑂(𝑛).
98. The numbers 1,2,.…𝑛 are inserted in a binary search tree in some order. In the resulting tree, the right subtree of
the root contains 𝑝 nodes. The first number to be inserted in the tree must be
A. 𝑝
B. 𝑝+1
C. 𝑛−𝑝 from 1,...n elements p elements are on the right. so root or first inserted will be at n-p
D. 𝑛−𝑝+1
99. Let 𝑇(𝑛) be the number of different binary search trees on 𝑛 distinct elements.
A. 𝑛−𝑘+1
B. 𝑛−𝑘
C. 𝑛−𝑘−1
D. 𝑛−𝑘−2 left subtree + root + right subtree = total node (k-1) + 1 + x = n x = n – k
100. A binary search tree contains the value 1,2,3,4,5,6,7,8. The tree is traversed in pre-order and the values are
printed out. Which of the following sequences is a valid output?
A. 53124786
B. 53126487
C. 53241678
D. 53124768
101. 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 nodes in the left subtree and right subtree of the root respectively is
A. (4,7)
B. (7,4)
C. (8,3)
D. (3,8)
Root will be 50. now insert one by one, greater to 50 in the right sub tree, lesser in left sub tree.
Or you can simply count the number looking at the i/p. less than 50 are 7. more than 50 are 4.
A. Ceil (log2(𝑛+1))
B. 1.44 log2𝑛 ======== Max height
C. Floor (log2(𝑛+1)) Min height
D. 1.64 log2𝑛
103. What is the worst case time complexity of inserting 𝑛2 elements into an AVL-tree with 𝑛 elements initially?
A. Θ(𝑛4)
B. Θ(𝑛2)
C. Θ(𝑛2log2𝑛)
D. Θ(𝑛3)
A. Linear list
B. Search tree O(logn)
C. Hash table
D. Self organization list
105. The number of rotations required to insert a sequence of elements 9,6,5,8,7,10 into an empty 𝐴𝑉𝐿 tree is?
A. 0
B. 1
C. 2
D. 3
106. The worst case running time to search for an element in a balanced binary search tree with 𝑛2𝑛 elements is
A. Θ(𝑛log𝑛)
B. Θ(𝑛2n)
C. Θ(𝑛)
D. Θ(log𝑛)
107. In the balanced binary tree in the below figure, how many nodes will become unbalanced when a node is
inserted as a child of the node “g”?
A. 1
B. 3
C. 7
D. 8
𝑎,𝑏,𝑐 will become unbalanced with Balance factor as +2,+2,+2 respectively. Balance factor should be −1,0,+1.
108. Which one of the following sequences when stored in an array at locations 𝐴[1],…,𝐴[10] forms a max-heap?
A. 23,17,10,6,13,14,1,5,7,12
B. 23,17,14,7,13,10,1,5,6,12
C. 23,17,14,6,13,10,1,5,7,15
D. 23,14,17,1,10,13,16,12,7,5
109. Let 𝐻 be a binary min-heap consisting of 𝑛 elements implemented as an array. What is the worst case time
complexity of an optimal algorithm to find the maximum element in 𝐻?
A. Θ(1)
B. Θ(log𝑛)
C. Θ(𝑛)
D. Θ(𝑛log𝑛)
110. Given a binary-max heap. The elements are stored in an arrays as 25,14,16,13,10,8,12. What is the content of the
array after two delete operations?
A. 14,13,8,12,10
B. 14,12,13,10,8
C. 14,13,12,8,10
D. 14,13,12,10,8 max-heap deletion is always done at the root node. first, we delete a root node and call heapify.
111. Consider a complete binary tree where the left and right subtrees of the root are max-heaps. The lower bound for
the number of operations to convert the tree to a heap is
112. A priority queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the
heap is: 10,8,5,3,2. Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the
heap after the insertion of the elements is:
113. A max-heap is a heap where the value of each parent is greater than or equal to the value of its children. Which of the following
is a max-heap?
A.
B.
C.
D.
In option A (a heap must have all levels till last but one completely filled and in the last level all nodes must be filled from the
left end without a gap till the last node)
114. In a binary max heap containing 𝑛 numbers, the smallest element can be found in time
A. 𝑂(𝑛)
B. 𝑂(log𝑛)
C. 𝑂(loglog𝑛)
D. O(1)
In a max heap, the smallest element is always present at a leaf node. Heap being a complete binary tree, there can be up to 𝑛2 leaf
nodes and to examine all of them we would need 𝑂(𝑛) time.
115. In a min-heap with 𝑛 elements with the smallest element at the root, the 7𝑡ℎ smallest element can be found in
time
A. Θ(𝑛log𝑛)
B. Θ(𝑛)
C. Θ(log𝑛)
D. Θ(1)
Time to find the smallest element on a min-heap- one retrieve operation - Θ(1)
Time to find the second smallest element on a min-heap- requires 22−1=3 check operations to find the second smallest element out
of 3 elements - Θ(1)
Time to find the 7𝑡ℎ smallest element - requires 𝑂(27−1)=𝑂(127) check operations to find the seventh smallest element out
of 127 possible ones - Θ(1)
In short if the number of required operations is independent of the input size 𝑛, then it is always Θ(1).
(Here, we are doing a level order traversal of the heap and checking the elements)
If we are not allowed to traverse the heap and allowed only default heap-operations, we will be restricted with doing Extract-
min 7 times which would be 𝑂(log𝑛).
116. Consider any array representation of an 𝑛 element binary heap where the elements are stored from index 1 to
index 𝑛 of the array. For the element stored at index 𝑖 of the array (𝑖≤𝑛), the index of the parent is
A. 𝑖−1
B. ⌊𝑖/2⌋ floor
C. ⌈𝑖/2⌉ ceil
D. (𝑖+1)/2
117. Of the following, which best approximates the ratio of the number of nonterminal nodes in the total number of
nodes in a complete 𝐾-ary tree of depth 𝑁 ?
A. 1/𝑁
B. 𝑁−1/𝑁
C. 1/𝐾
D. 𝐾−1/𝐾
118. Consider the following rooted tree with the vertex labeled 𝑃 as the root:
The order in which the nodes are visited during an in-order traversal of the tree is
A. 𝑆𝑄𝑃𝑇𝑅𝑊𝑈𝑉 The inorder traversal order of a ternary tree is left → root → middle → right.
B. 𝑆𝑄𝑃𝑇𝑈𝑊𝑅𝑉
C. 𝑆𝑄𝑃𝑇𝑊𝑈𝑉𝑅
D. 𝑆𝑄𝑃𝑇𝑅𝑈𝑊𝑉
119. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:
A. 𝑛/2
B. (𝑛−1)/3
C. (𝑛−1)/2
D. (2𝑛+1)/3
120. A complete 𝑛-ary tree is one in which every node has 0 or 𝑛 sons. If 𝑥 is the number of internal nodes of a
complete 𝑛-ary tree, the number of leaves in it is given by
A. 𝑥(𝑛−1)+1 1 for root and n-1 nodes to be inserted for every new node
B. 𝑥𝑛−1
C. 𝑥𝑛+1
D. 𝑥(𝑛+1)
121. In linear hashing, if blocking factor 𝑏𝑓𝑟, loading factor 𝑖 and file buckets 𝑁 are known, the number of records
will be
A. 𝑐𝑟=𝑖+𝑏𝑓𝑟+𝑁
B. 𝑟=𝑖−𝑏𝑓𝑟−𝑁
C. 𝑟=𝑖+𝑏𝑓𝑟−𝑁
D. 𝑟 = 𝑖∗𝑏𝑓𝑟∗𝑁
122. A Hash Function 𝑓 defined as 𝑓(𝑘𝑒𝑦)=𝑘𝑒𝑦mod7. With linear probing while inserting the
keys 37,38,72,48,98,11,56 into a table indexed from 0, in which location key 11 will be stored (Count table
index 0 as 0𝑡ℎ location)?
A. 3
B. 4 Location Key
C. 5 0 98
D. 6 1 56
2 37
3 38
4 72
123. Given that hash 5 11 table 𝑇 with 25 slots that
stores 2000 elements, the load 6 48 factor 𝑎 for 𝑇 is ___ 80______.
A. 2
B. 3
C. 4
D. 6
165 in loc 5
62 in loc 2
123 in loc 4 ( collission and next free space)
142 in loc 6 (collision in 2, and 3,4,5 already occupied)
A. I only
B. II only
C. I and II only the last digit of every digit given is equal in I and II.
D. III or IV
126. An advantage of chained hash table (external hashing) over the open addressing scheme is
C. Deletion is easier
Chaining having two draw back 1. pointer overhead 2. searching time is high