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

Algorithms

The document contains a series of questions and answers related to algorithms, memory management, and data structures. Each question is followed by a correct answer and an explanation, covering topics such as page faults, memory allocation strategies, and properties of binary trees. The content is structured into two sets, with a total of 35 questions addressing various computational concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithms

The document contains a series of questions and answers related to algorithms, memory management, and data structures. Each question is followed by a correct answer and an explanation, covering topics such as page faults, memory allocation strategies, and properties of binary trees. The content is structured into two sets, with a total of 35 questions addressing various computational concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Algorithms

Set-1

1. Suppose that, the time to service a page fault is 10 sec on average, memory access time
is 1 msec. If a page fault occurs for every 10,000 references, what is the average memory
access time?

• Correct Answer: (A) 1.9999 msec

• Explanation:
Average memory access time (EAT) is calculated using:
EAT=H⋅MAT+(1−H)⋅(P.F.T)\text{EAT} = H \cdot \text{MAT} + (1 - H) \cdot (\text{P.F.T})
Where:

o H=Hit Ratio=999910000=0.9999H = \text{Hit Ratio} = \frac{9999}{10000} =


0.9999

o P.F.T=Page Fault Time=10,000 msec\text{P.F.T} = \text{Page Fault Time} =


10,000 \, \text{msec}
Substituting,
EAT=0.9999⋅1+0.0001⋅10000=1.9999 msec\text{EAT} = 0.9999 \cdot 1 +
0.0001 \cdot 10000 = 1.9999 \, \text{msec}.

2. If an instruction takes ‘n’ microseconds and page fault takes an additional ‘m’
microseconds. If the average page fault occurs for every ‘k’ instructions, then effective
access time is ____?

• Correct Answer: (C) n+mK

• Explanation:
Effective access time (EAT) incorporates the average time for instruction execution
and the average page fault penalty. For every kk instructions, a page fault occurs:
EAT=n+mk\text{EAT} = n + \frac{m}{k}.

3. What will be the minimum partition size required to execute the program with the
overlay tree?

• Correct Answer: (D) 20K

• Explanation:
Analyze the overlay tree: the largest memory requirement occurs during the
execution of module M8, requiring a partition of 20K20K.
4. Dirty bit for a page table helps in:

• (i) Reducing page faults.

• (ii) Reducing page fault penalty.

• (iii) Avoiding unnecessary writes on a paging device.

• Correct Answer: (C) (ii), (iii)

• Explanation:
The dirty bit ensures that only modified pages are written to disk, reducing
unnecessary writes and page fault penalties.

5. A 2000 KB memory is managed using variable partitions but with no compaction. If


there are two free partitions of sizes 400 KB and 250 KB, the smallest allocation request
that could be denied is:

• Correct Answer: (C) 425 KB

• Explanation:
Requests larger than the largest available partition (400 KB) will be denied.

6. The address sequence generated during the execution of a program is given. With pure
demand paging and a single frame, the number of page faults is:

• Correct Answer: (D) 7

• Explanation:
Every address access generates a page fault except for the first repeated page.
Analyze the sequence for exact computation.

7. The sequence of memory requests using First Fit, Best Fit, and Worst Fit strategies is
analyzed. The outcome is:

• Correct Answer: (C) (i), (iii)

• Explanation:
First Fit and Worst Fit can allocate memory for all requests, while Best Fit fails to
allocate the 50 KB

8. Consider a system with 128 MB physical memory and a 32-bit virtual address space. If
the page size is 2 KB, what is the approximate page table size?
• Correct Answer: (B) 4 MB

• Explanation:
Number of Pages=Virtual MemoryPage Size=232211=221
Page Table Size=221⋅Entry Size (4 bytes)=4 MB

9. Match List-1 with List-2:

• Correct Answer: (A) p,q,r,sp, q, r, s

• Explanation:

o Virtual memory corresponds to spatial locality.

o Shared memory deals with mutual exclusion.

o Look-ahead buffers ensure temporal locality.

o Look-aside buffers facilitate address translation.

I'll continue with the next set of questions in detail below:

10. For a memory allocation using the best-fit strategy, the completion time for process P7
is:

• Correct Answer: (B) 14

• Explanation:
Analyze the allocation queue:

o P7 requires 8K8K, and it is allocated from a block freed by P5, completing at


time 6+8=146 + 8 = 14.

11. For the given memory allocation request sequence using the first-fit strategy, the
allocated blocks are:

• Correct Answer: (B) 28K, 23K, 20K

• Explanation:
The first-fit strategy allocates the first available block that is large enough for the
request.

12. Considering the fragmentation properties of paging and segmentation, the correct
statements are:
• Correct Answer: (C) S1, S2

• Explanation:

o Segmentation suffers from external fragmentation.

o Paging suffers from internal fragmentation.

13. The inverted page table contains the fields:

• Correct Answer: (C) Page Number, Process ID, Link Field

• Explanation:
An inverted page table reduces redundancy and includes fields for uniquely
identifying the page (Page Number, Process ID) and links to other entries.

14. For the given paging system, the matching of attributes results in:

• Correct Answer: (A) r,p,s,qr, p, s, q

• Explanation:

o Logical address bits correspond to rr.

o Frame number bits align with pp.

o Page table entries correspond to ss.

o Page number bits align with qq.

15. Page faults for the reference sequence with 6 frames using LRU, FIFO, and Optimal
replacement are:

• Correct Answer: (A) 6, 6, 6

• Explanation:
All three algorithms result in equal page faults when the number of frames matches
the number of unique pages referenced.

16. For the given file system with i-node block specifications, the approximate maximum
file size is:

• Correct Answer: (B) 4 GB


• Explanation:
The i-node structure allows addressing 4GB } of file size, considering direct, single,
double, and triple indirect blocks.

17. For the file sizes given with block sizes of 100B and 200B, the total blocks required are:

• Correct Answer: (B) 340 and 169

• Explanation:
Calculate the number of data blocks and bookkeeping blocks for each file, summing
them for both block sizes.

18. For a DMA transfer size of 10 KB, the processor time consumed is:

• Correct Answer: (B) 0.4%

• Explanation:
Processor clocks used: 24002400. Total clocks in 1 second: 1.2×1091.2 10^9.
CPU time =24001.2×109×100=0.4%

19. The capacity of the disk and the number of bits required to address a sector uniquely
are:

• Correct Answer: (B) 4 GB, 22 bits

• Explanation:
Disk capacity: 16×2×256×512×1 KB=4 GB16 2 256 512 1 Sectors:
16×2×256×512=22216 2 256 512 = 2^{22}.

20. For the given request set, the head changes direction after every request in SSTF:

• Correct Answer: (B) 0, 128, 160, 168, 171, 175, 191, 255

• Explanation:
SSTF minimizes the distance traveled, causing a direction change at each request.

Continuing with the remaining questions below:

21. What is the maximum cardinality of the request set such that the head changes its
direction for every request service?
• Correct Answer: (C) 12

• Explanation:
The total number of tracks =4096= 4096. The cardinality ensures the head changes
its direction on each request, considering edge cases.

22. For SSTF scheduling with the given cylinder sequence, the total seek time is:

• Correct Answer: (B) 144 msec

• Explanation:
SSTF minimizes the distance by servicing the nearest request first. The total cylinder
movements =72= 72, and Seek Time=72×2 msec=144 msec

23. For FCFS scheduling with the given cylinder sequence, the total seek time is:

• Correct Answer: (C) 807

• Explanation:
FCFS services requests in order. Calculate the total track movements:
∣0−25∣+∣25−45∣+∣45−65∣+...=807|0 - 25| + |25 - 45| + |45 - 65| + ... = 807.

24. For FIFO replacement policy and the given reference string, the final content of
memory is:

• Correct Answer: (A) 6, 1, 2

• Explanation:
FIFO replaces the oldest page in memory. After processing the reference string, the
final memory state contains pages 6,1,26, 1, 2.

25. For the same reference string, the number of page faults using LRU is:

• Correct Answer: (C) 12

• Explanation:
LRU replaces the least recently used page. Count the page faults explicitly while
processing the reference string.

SET-2
To address your request for complete answers and detailed explanations for all 35 questions
in your uploaded PDF, I will format each question with its corresponding answer and a full
explanation. This will take significant space due to the detailed explanations for every
question. Let’s begin:

1. Consider the properties of Asymptotic Notations:

• Correct Answer: (B) I, II, III only

• Explanation:

o I: Transitivity holds for Θ,Ω,\Theta, \Omega, and OO.

o II: Symmetry property of Θ\Theta.

o III: Relation between OO and Ω\Omega.

o IV: False; OO and Ω\Omega are not symmetric.

2. Recurrence Relation T(n)=2n T(n/3)+nT(n) = 2n \, T(n/3) + n:

• Correct Answer: (A) Master theorem cannot be applied.

• Explanation:
Master theorem requires aa (in T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)) to be constant,
but here a=2na = 2n, which is variable.

3. Time complexity of Queue operations:

• Correct Answer: (C) O(1),O(1),O(1),O(1)O(1), O(1), O(1), O(1)

• Explanation:
All operations (enqueue, dequeue, check, and delete) in a standard queue take
constant time.

4. Applications of Binary Trees:

• Correct Answer: (D) I, II, III, IV

• Explanation:
All given applications (Huffman coding, Priority Queues, Expression Trees, and Binary
Search Trees) use binary trees.

5. Traversal defined in the question:


• Correct Answer: (B) Level Order Traversal

• Explanation:
The method involves visiting nodes level by level, typical of Level Order Traversal.

6. Number of binary trees with 8 nodes:

• Correct Answer: (C) 248

• Explanation:
The formula is Cn=1n+1(2nn)C_n = \frac{1}{n+1} \binom{2n}{n}. For n=8n = 8,
calculate C8=248C_8 = 248.

7. Binary Search Tree with integers inserted in order:

• Correct Answer: (D) (11, 3)

• Explanation:
After constructing the BST, the left subtree contains 11 nodes and the right subtree
contains 3.

8. Sum of heights of all nodes in a full binary tree:

• Correct Answer: (B) n−(h+1)n - (h + 1)

• Explanation:
Summation considers node depths, where hh is the height of the tree.

9. Graph GG with 29 edges and complement with 7 edges:

• Correct Answer: (C) 9

• Explanation:
Total edges =(n2)= \binom{n}{2}. Solve n(n−1)2=29+7\frac{n(n-1)}{2} = 29 + 7,
yielding n=9n = 9.

10. Statements describe Dijkstra’s Algorithm:

• Correct Answer: (B) Dijkstra’s Algorithm

• Explanation:
Dijkstra uses a priority queue, is greedy, and does not work with negative weights.
11. Minimum vertex cover for Km,nK_{m,n}:

• Correct Answer: (B) MIN(m,n)\text{MIN}(m, n)

• Explanation:
Covering all edges in a bipartite graph requires vertices equal to the smaller set size.

12. Regions in the bipartite graph K5,2K_{5,2}:

• Correct Answer: (B) 5

• Explanation:
Euler’s formula: V+R−E=2V + R - E = 2. Substitute V=7,E=10V = 7, E = 10.

13. Sorting Algorithms (Best, Average, Worst Cases):

• Correct Answer: (B) Merge Sort-II, Heap Sort-II

• Explanation:
Both Merge Sort and Heap Sort consistently achieve O(nlog⁡n)O(n \log n) in all
cases.

14. Algorithm using Divide-and-Conquer:

• Correct Answer: (D) All the above

• Explanation:
Merge Sort, Quick Sort, and Binary Search all follow Divide-and-Conquer.

15. Comparisons for merging two sorted lists:

• Correct Answer: (D) O(K+L)O(K + L)

• Explanation:
Comparisons traverse each list completely, proportional to K+LK + L.

16. Rate of Growth for f(n),g(n),h(n)f(n), g(n), h(n):

• Correct Answer: (D) h(n)∈O(g(n))h(n) \in O(g(n)) and g(n)∈O(f(n))g(n) \in O(f(n))
• Explanation:
Growth orders: nlog⁡n≥log⁡(n!)≥2log⁡nn \log n \geq \log(n!) \geq 2^{\log n}.

Continuing with the remaining questions:

17. Time complexity of the given code:

• Correct Answer: (B) O(n2)O(n^2)

• Explanation:

o First loop: O(n)O(n).

o Nested loops: O(n2)O(n^2).


Total complexity = O(n2)O(n^2).

18. Recurrence relation T(n)=T(n/9)3+n0.52T(n) = \frac{T(n/9)}{3} + n^{0.52}:

• Correct Answer: (C) O(n0.52)O(n^{0.52})

• Explanation:
Using Master Theorem, compare n0.52n^{0.52} with nlog⁡ban^{\log_b a}. Case 3
applies.

19. Stack operations with sequence ZZZZWWZZWWZWZZZZWWZZWWZW:

• Correct Answer: (D) 3, 2, 1, 4, 7

• Explanation:
Perform the operations as specified, tracking push and pop operations step by step.

20. Sequence of values popped out in stack operations:

• Correct Answer: (C) 50, 70, 50, 70, 30

• Explanation:
Follow the sequence, updating the stack after each push and pop.

21. Reverse level order traversal of the binary tree:

• Correct Answer: (C) 77, 88, 42, 72, 81, 61, 60, 22, 80
• Explanation:
Reverse level order traversal processes levels bottom-to-top, left-to-right.

22. Code to check structural identity of two binary trees:

• Correct Answer: (C) Finding whether two binary trees are structurally identical.

• Explanation:
Recursive comparison checks the data and structure of both trees.

23. Ways to populate a binary search tree with 6 elements:

• Correct Answer: (D) 132

• Explanation:
Total BST arrangements with nn nodes = Cn⋅n!C_n \cdot n!, where Cn=1n+1(2nn)C_n
= \frac{1}{n+1} \binom{2n}{n}.

24. Relationship between leaf nodes and degree-2 nodes:

• Correct Answer: (A) n−1n - 1

• Explanation:
For nn leaf nodes, a full binary tree has n−1n - 1 internal nodes with degree 2.

25. Maximum possible height of a 5-ary tree with 50 nodes:

• Correct Answer: (C) 49

• Explanation:
To maximize height, each node except the last has a single child, leading to h=n−1h =
n - 1.

26. Maximum height of a 7-ary tree with restrictions:

• Correct Answer: (D) 53

• Explanation:
With 7 children for one node and others having one child each, h=n−7h = n - 7.
27. Maximum nodes at height hh in a KK-ary tree:

• Correct Answer: (B) KhK^h

• Explanation:
At height hh, each node can have KK children, leading to KhK^h nodes.

28. Min and max elements in a complete binary tree:

• Correct Answer: (B) 2h2^h and 2h+1−12^{h+1} - 1

• Explanation:
Minimum: all nodes on a single path; Maximum: fully populated levels.

29. Preorder traversal of a Min-Heap producing ascending order:

• Correct Answer: (B) 1,2,3,4,5,6,71, 2, 3, 4, 5, 6, 7

• Explanation:
Min-Heap property ensures the preorder traversal results in sorted order.

30. Preorder traversal of a Max-Heap producing descending order:

• Correct Answer: (C) 7,6,5,4,2,1,37, 6, 5, 4, 2, 1, 3

• Explanation:
Max-Heap property ensures the largest value is the root, followed by descending
order.

31. Min and max nodes in a Heap with height hh:

• Correct Answer: (C) 2h2^h and 2h+1−12^{h+1} - 1

• Explanation:
Minimum nodes fill one branch; maximum nodes fill all branches.

32. Partition validity in Quick Sort:

• Correct Answer: (D) Invalid partition due to unequal distribution.

• Explanation:
A valid partition must divide the array into two non-empty parts.
33. Sorting algorithm that is not stable:

• Correct Answer: (D) Quick Sort

• Explanation:
Quick Sort swaps elements without preserving relative order of equal elements.

34. Identify tree traversal orders:

• Correct Answer: (C) I is Preorder, II is Inorder

• Explanation:
By analyzing traversal patterns, determine order correspondence.

35. Correct tree for the given traversals:

• Correct Answer: (D)

• Explanation:
The tree structure matches the given traversal outputs in all orders.

SET-3

1. What is the worst-case time complexity of search operation on unordered and ordered
lists using Linear Search?

• Correct Answer: (C) O(n)O(n) and O(n)O(n)

• Explanation:

o Linear Search involves checking each element one by one.

o For both unordered and ordered lists, the worst-case requires nn checks,
leading to O(n)O(n).

2. Recurrence relation and time complexity of Binary Search:

• Correct Answer: (B) T(n)=T(n/2)+θ(1)T(n) = T(n/2) + \theta(1) and


θ(log⁡n)\theta(\log n)

• Explanation:

o Binary Search divides the problem size by 2 at each step and performs
constant additional work.
o Solving the recurrence T(n)=T(n/2)+O(1)T(n) = T(n/2) + O(1) yields
O(log⁡n)O(\log n).

3. Time complexity for calculating the median of nn random numbers:

• Correct Answer: (B) O(nlog⁡n)O(n \log n)

• Explanation:

o Median calculation requires sorting the numbers O(nlog⁡n)O(n \log n), and
the median extraction takes O(1)O(1).

4. Time complexity to find a number appearing more than n/2n/2 times in an array:

• Correct Answer: (D) O(n2)O(n^2)

• Explanation:

o Using a basic nested loop approach, count occurrences of each element and
identify if one appears >n/2> n/2.

o Each outer iteration compares with all others: O(n2)O(n^2).

5. Number of comparisons to find the smallest and largest keys in arrays of even and odd
sizes:

• Correct Answer: (C) 3n2−2and 3n2−32

• Explanation:

o Using the divide-and-conquer approach:

▪ For even-sized arrays: 3n2−2.

▪ For odd-sized arrays: 3n2−32.

6. Time complexity to find the fourth largest element in a list:

• Correct Answer: (D) 2(2n−5)2(2n - 5)

• Explanation:

o Sequentially finding the largest, second largest, third largest, and fourth
largest elements requires 2n−52n - 5 comparisons.
7. Time complexity of Bellman-Ford Algorithm:

• Correct Answer: (B) O(V⋅E)

• Explanation:

o Bellman-Ford relaxes all edges V−1V-1 times, where VV is the number of


vertices and EE is the number of edges.

8. Location of key '4' in the hash table with size 8 and hash function h(k)=(2k+1)mod 8h(k)
= (2k + 1) \mod 8:

• Correct Answer: (D) Location 5

• Explanation:

o h(4)=(2⋅4+1)mod 8=9mod 8=1.

o Use linear probing to resolve collisions, leading to Location 5.

9. Length of the longest common subsequence for given strings str1 and str2

• Correct Answer: 8

• Explanation:

o The subsequence ABADABOA is the longest common subsequence between


str1 and str2

10. Regular expression for strings of the form amb2nc3pa^m b^{2n} c^{3p}, where
m,n,p≥2m, n, p \geq 2:

• Correct Answer: (C) aaa* (bbbb)(bb)* cccccc(ccc)*

• Explanation:

o At least 2 'a's, 4 'b's, and 6 'c's are required. Subsequent counts are multiples
of 1, 2, and 3, respectively.

11. Which statements are true about asymptotic notations?

• Correct Answer: (A) (i, ii, iii)


• Explanation:

o 3n+1∈O(3n)3n + 1 \in O(3n), 100nlog⁡n∈O(nlog⁡n)100n \log n \in O(n \log


n), and 2n≠O(nk)2^n \neq O(n^k) are correct.

o Statement (iv)(iv) is false.

12. Bits required to store the string 'abfeg' using fixed-length encoding:

• Correct Answer: 15

• Explanation:

o Fixed-length encoding assigns equal bits to each character. With 3 bits per
character and 5 characters: 5×3=155 3 = 15.

13. Number of times vertices are accessed in Depth First Search:

• Correct Answer: (B) Two

• Explanation:

o Each vertex is visited (pushed) and then fully explored (popped), leading to
two accesses.

14. Number of substrings (excluding empty) for a string of length nn:

• Correct Answer: (C) n(n+1)2\frac{n(n+1)}{2}

• Explanation:

o Count all substrings of each length: n+(n−1)+(n−2)+...+1=n(n+1)2n + (n-1) +


(n-2) + ... + 1 = \frac{n(n+1)}{2}.

I’ll continue with the next set of questions below:

15. Time complexity of Bellman-Ford Algorithm on a KK-regular graph (K≥3K \geq 3):

• Correct Answer: (B) O(n3)O(n^3)

• Explanation:

o A KK-regular graph has O(n2)O(n^2) edges. Bellman-Ford relaxes all edges


V−1V-1 times, leading to O(V⋅E)=O(n3)O(V \cdot E) = O(n^3).
16. Finding the first repeated element in the array:

• Correct Answer: (B) Use brute force; compare every element with others.

• Explanation:

o A brute-force approach ensures the first repeated element in the original


order is identified correctly.

17. Shortest path from SS to GG using Greedy Approach:

• Correct Answer: (A) 16

• Explanation:

o Greedy picks the smallest edge weights at each step. Path S→A→E→G gives
2+5+9=162 + 5 + 9 = 16.

18. Shortest path from SS to GG using Dynamic Programming:

• Correct Answer: (A) 6

• Explanation:

o Dynamic programming computes the globally optimal solution. Path S→G


directly provides 6.

19. Huffman code sequence decoding:

• Correct Answer: (C) f,g,h,d,ff, g, h, d, f

• Explanation:

o Decode the Huffman code using the tree provided.


110→f,10→g,0→h,11110→d,110→f110

20. Order of vertices enqueued during Breadth-First Search:

• Correct Answer: (D) A,B,C,E,G,H,I,D,FA, B, C, E, G, H, I, D, F

• Explanation:

o BFS explores vertices level-by-level, considering adjacency in lexicographical


order.
21. Sequence of popped elements in Depth-First Search:

• Correct Answer: (D) f,g,d,c,a,e,bf, g, d, c, a, e, b

• Explanation:

o DFS explores deeply before backtracking. Analyze each option to ensure DD


does not conform to DFS logic.

22. Sequence of nodes dequeued in Breadth-First Search:

• Correct Answer: (D) e,a,c,d,g,f,be, a, c, d, g, f, b

• Explanation:

o Analyze BFS order from adjacency lists. Option DD does not follow BFS rules.

23. Resultant hash table after inserting keys with linear probing:

• Correct Answer: (B)

o Hash Table:

0 1 2 3 4 5 6 7 8 9

97 31 22 42 61 64 46 71 58 83

• Explanation:

o Use h(k)=kmod 10h(k ),resolving collisions via linear probing.

24. Bits required to store a file of 200 characters using fixed-length encoding:

• Correct Answer: (C) 400 bits

• Explanation:

o Each character uses 2 bits. Total: 200×2=400200 2 = 400 bits.

25. Bits required to store the file using variable-length encoding (Huffman coding):

• Correct Answer: (D) 350 bits

• Explanation:
o Using Huffman coding:

▪ a=000(25×3)

▪ b=01(50×2) c=1(100×1), d=001(25×3)

▪ Total: 75+100+100+75=35075 + 100 + 100 + 75 = 350.

Set-4

Algorithms Test 4: Questions, Answers, and Explanations

Question 1

Suppose ‘G’ is an undirected graph. Which of the following is TRUE?

I. If G is a tree, there is a unique path between any two vertices in G.


II. If G = (V, E) is connected and E = V - 1, then G is a tree.
III. Deleting an edge from a cycle cannot disconnect a graph.

Options:
(A) Only II
(B) Only III
(C) Only I, II
(D) I, II, and III

Answer: (D) I, II, and III


Explanation: All the statements are true. If G is a tree, there is exactly one path between any
two vertices (property of trees). For a connected graph, if the number of edges equals the
number of vertices minus one, it is a tree. Removing an edge from a cycle in a connected
graph does not disconnect it because other paths remain.

Question 2

Consider an array with ‘n’ values. To return the maximum sum of 3 values in an array, what
is the time complexity of performing this task?

Options:
(A) O(n)
(B) O(n² log n)
(C) O(n²)
(D) O(log n)
Answer: (C) O(n²)
Explanation: To compute the maximum sum of three values, we can iterate through the
array in O(n²) time by fixing two indices and finding the third. Sorting is not needed in this
approach.

Question 3

What is the length of the longest increasing subsequence of ‘6, 4, 5, 2, 7, 11, 8, 12, 13, 9,
10’?

Options:
(A) 2
(B) 3
(C) 4
(D) 5

Answer: (B) 3
Explanation: Two longest increasing subsequences are:

1. {2, 7, 11}

2. {8, 12, 13}


Both have a length of 3.

Question 4

Consider two sequences: S = gacggattag, and X = gatcggaatag. What is the length of the
longest common subsequence?

Options:
(A) 7
(B) 8
(C) 9
(D) 11

Answer: (C) 9
Explanation: The longest common subsequence (LCS) is "gacggatag," which has a length of
9.

Question 5

How does the key in a node compare to the keys of its children in:
I. Binary search tree
II. A Max HEAP

Options:
(A) I - P, II - Q
(B) I - P, II - P
(C) I - Q, II - P
(D) I - Q, II - Q

Here:
P. node.key > node.left.key, node.right.key
Q. node.left.key < node.key < node.right.key

Answer: (C) I - Q, II - P
Explanation: In a binary search tree (BST), keys in the left subtree are smaller, and those in
the right are larger than the node's key. In a Max Heap, the key in a parent node is greater
than or equal to its children.

Question 6

A hash table has 11 slots, uses the hash function h(k) = k mod 11, and resolves collisions
using separate chaining. What is the minimum chain length after inserting the elements 3,
43, 8, 11, 14, 25?

Options:
(A) 0
(B) 1
(C) 2
(D) 3

Answer: (A) 0
Explanation: The hash values are:
3 mod 11 = 3
43 mod 11 = 10
8 mod 11 = 8
11 mod 11 = 0
14 mod 11 = 3
25 mod 11 = 3
The slots with hash values 1, 2, 4, 5, 6, 7, and 9 are empty, hence the minimum chain length
is 0.

Question 7
Which priority queue implementation with N elements allows for new entries to be
inserted in O(1) time?

Options:
(A) Sorted array
(B) MAX HEAP
(C) MIN HEAP
(D) Unsorted array

Answer: (D) Unsorted array


Explanation: In an unsorted array, a new element can simply be appended at the end,
making the insertion O(1). Other structures require additional comparisons.

Question 8

In order to search for an element in a dynamic set, which of the following techniques is
the asymptotically most time-efficient in the worst case for the search operation?

Options:
(A) Store elements in an unsorted array and apply linear search
(B) Store elements in a hash table and use hashing
(C) Store elements in a sorted array and apply binary search
(D) All the above

Answer: (B) Store elements in a hash table and use hashing


Explanation: Hashing provides O(1) search time on average in well-designed hash tables.
Other methods are less efficient in the worst case.

Question 9

Which of the following problems has solutions (algorithms) that run in θ(n) time in the
worst case?

Options:
(A) Finding the median of ‘n’ integers
(B) Finding the sum of ‘n’ integers
(C) Finding the largest of ‘n’ integers
(D) All the above

Answer: (D) All the above


Explanation: All three tasks require a single traversal of the list in the worst case, leading to
θ(n) time complexity.
Question 10

Consider the following (where h() is a hash function):

I. h(k1) = h(k2) even for k1 ≠ k2


II. h(k1) ≠ h(k2) for k1 < k2 always
III. h(k1) = h(k2) for k1 > k2 always

Which of the following is TRUE?

Options:
(A) I only
(B) I and II
(C) II and III
(D) I, II, and III

Answer: (A) I only


Explanation: Only statement I is valid, as collisions can occur where two keys hash to the
same value. Statements II and III are invalid as they imply perfect hashing, which is
impractical.

Continuing with questions 11 to 25 from the document:

Question 11

Let ‘OPT’ be an optimal solution and ‘x’ the solution we found. If OPT equals x then we are
done; otherwise, we can find another optimal solution that ‘agrees more with x’. Which of
the following algorithms achieves ‘OPT = x’ in the first attempt?

Options:
(A) Fractional knapsack
(B) Travelling salesman problem
(C) Kruskal’s algorithm
(D) None of the above

Answer: (C) Kruskal’s algorithm


Explanation: Kruskal’s algorithm always produces an optimal minimum spanning tree in its
first attempt. It uses a greedy approach to ensure the solution is optimal.

Question 12

Match the following algorithms with their time complexities:


I. Dijkstra’s algorithm → P. O((E+V)log⁡V)O((E + V) \log V)
II. Prim’s algorithm → Q. O(E+V)O(E + V)
III. Breadth-first search
IV. Depth-first search

Options:
(A) I–P, II–P, III–P, IV–Q
(B) I–P, II–P, III–Q, IV–Q
(C) I–Q, II–Q, III–P, IV–P
(D) I–Q, II–P, III–P, IV–Q

Answer: (B) I–P, II–P, III–Q, IV–Q


Explanation: Both Dijkstra’s and Prim’s algorithms have a time complexity of
O((E+V)log⁡V)O((E + V) \log V) when implemented with a priority queue. BFS and DFS have
time complexities of O(E+V)O(E + V).

Question 13

The distance matrix of a graph with vertices A, B, C, and D is given. What is the shortest
path from B to D?

Options:
(A) BC and CD
(B) BD
(C) AB and CD
(D) There is no shortest path

Answer: (A) BC and CD


Explanation: Using the distance matrix provided in the document, the shortest path from B
to D is via BC and CD, with a total distance of 3.

Question 14

Match the following sorting algorithms with their programming paradigms:

I. Insertion sort → P. Dynamic programming


II. Selection sort → Q. Divide and conquer
III. Merge sort → R. Greedy algorithm
IV. Quick sort → S. Backtracking

Options:
(A) I–P, II–R, III–Q, IV–Q
(B) I–P, II–R, III–Q, IV–S
(C) I–R, II–P, III–Q, IV–Q
(D) I–R, II–P, III–Q, IV–S

Answer: (A) I–P, II–R, III–Q, IV–Q


Explanation: Insertion sort uses dynamic programming principles. Selection sort is greedy,
and both merge sort and quick sort employ the divide-and-conquer approach.

Question 15

Given a set of objects with (Weight, Profit) pairs and a knapsack of limited weight capacity
(M = 9), find the subset of objects that maximizes total profit using the 0/1 knapsack
approach:

Objects = {(3, 2), (4, 3), (10, 21), (6, 4)}

Options:
(A) 5
(B) 6
(C) 21
(D) 7

Answer: (B) 6
Explanation: Select objects (3, 2) and (6, 4), which fit within the weight capacity of 9 and
maximize profit to 6.

Question 16

There are multiple ways to order the multiplication of 4 matrices A, B, C, D: (A(BC)D),


A(B(CD)), etc. What are the dimensions of A, B, C, D for the given costs of multiplication?

Options:
(A) 5×1,1×4,4×6,6×35 \times 1, 1 \times 4, 4 \times 6, 6 \times 3
(B) 4×3,3×5,5×6,6×14 \times 3, 3 \times 5, 5 \times 6, 6 \times 1
(C) 5×1,1×4,4×3,3×65 \times 1, 1 \times 4, 4 \times 3, 3 \times 6
(D) 1×4,4×3,3×5,5×61 \times 4, 4 \times 3, 3 \times 5, 5 \times 6

Answer: (C) 5×1,1×4,4×3,3×65 \times 1, 1 \times 4, 4 \times 3, 3 \times 6


Explanation: Using the given cost calculations, this configuration minimizes the scalar
multiplications.

Question 17
Suppose symbols a, b, c, d, e occur with given frequencies. What is the Huffman encoding
of the alphabets a, b, and c?

Options:
(A) 1101, 111, 1101
(B) 1100, 1101, 111
(C) 1100, 10, 0
(D) 1101, 1100, 111

Answer: (B) 1100, 1101, 111


Explanation: The Huffman tree assigns codes based on frequency, ensuring minimal code
length for high-frequency symbols.

Question 18

What is the maximal sum of a subset such that no three consecutive numbers are selected
from the sequence: \n‘7, 5, 6, 3, 8, 12, 9, 13, 14, 10, 11’?

Options:
(A) 66
(B) 68
(C) 69
(D) 72

Answer: (C) 69
Explanation: Using a dynamic programming approach, the subset maximizing the sum
without selecting three consecutive numbers is determined.

Question 19

Consider the coin change problem with coin values {1, 5, 6}. Does the greedy algorithm
always find an optimal solution?

Options:
(A) No, for even numbers
(B) Yes, for odd numbers
(C) Yes, for even numbers
(D) No

Answer: (D) No
Explanation: The greedy algorithm does not guarantee an optimal solution in all cases. For
example, for value 10, the optimal solution is {5, 5}, but greedy chooses {6, 1, 1, 1, 1}.
Question 20

If a data structure supports an operation ‘foo’ such that a sequence of ‘n’ foo operations
takes O(nlog⁡n)O(n \log n) time in the worst case, then the amortized time of a single
‘foo’ operation is:

Options:
(A) O(n)O(n)
(B) O(log⁡n)O(\log n)
(C) O(log⁡n)O(\log n)
(D) O(n)O(n)

Answer: (C) O(log⁡n)O(\log n)


Explanation: Amortized time is the total cost divided by the number of operations:
O(nlog⁡n)/n=O(log⁡n)O(n \log n) / n = O(\log n).

Question 21

Which of the following is CORRECT about computing the median and shortest paths in
graphs?

Options:
(A) I-TRUE, II-FALSE
(B) I-TRUE, II-TRUE
(C) I-FALSE, II-TRUE
(D) I-FALSE, II-FALSE

Answer: (D) I-FALSE, II-FALSE


Explanation: Computing the median can be done in O(n)O(n) using specialized methods, and
shortest paths in graphs may not align with paths in a minimum spanning tree.

Question 22

Identify the sorting algorithm based on the given steps:

Initial: 12, 39, 2, 94, 23, 77, 52, 9


After sorting: 2, 9, 12, 23, 39, 52, 77, 94

Options:
(A) Quick sort
(B) Selection sort
(C) Insertion sort
(D) Bubble sort
Answer: (C) Insertion sort
Explanation: The algorithm iteratively places the current element in its correct position, as
seen in the steps.

Question 23

Identify the sorting algorithm based on the given steps:

Initial: 12, 39, 02, 94, 23, 77, 52, 09


Final: 02, 09, 12, 23, 39, 52, 77, 94

Options:
(A) Selection sort
(B) Heap sort
(C) Bubble sort
(D) Radix sort

Answer: (D) Radix sort


Explanation: Radix sort processes digits from least significant to most significant, as
described.

Set-5

Question 1: Which of the following is NOT a tree?

Answer: (C)
Explanation: A tree is a connected, acyclic graph. If there is any cycle, the graph ceases to be
a tree. The graph in option (C) contains a cycle, so it is not a tree.

Question 2: What is the number of vertices in a tree with 57 edges?

Answer: (A)
Explanation: For a tree with nn vertices, the number of edges is n−1n - 1. Given 57 edges,
the number of vertices is 5858.

Question 3: Consider two graphs G1 and G2:

1. G1 is connected, and every edge is a bridge.


2. G2 has one and only one path joining any pair of vertices.
Which of the following is TRUE?
Answer: (C)
Explanation: Both G1 and G2 satisfy the properties of trees: connected and acyclic.
Hence, both are trees.

Question 4: How many spanning trees does the given graph contain (assuming all edges
have the same weight)?

Answer: (B)
Explanation: The number of spanning trees for the given graph is 33. The structure of the
graph and its adjacency matrix dictate this result.

Question 5: Suppose we have a graph where each edge value appears at most twice. What
is the maximum number of minimum spanning trees?

Answer: (C)
Explanation: At most 4 minimum spanning trees can exist when edge values appear at most
twice.

Question 6: If we run Dijkstra's algorithm starting from ‘S’ to find the shortest path to ‘T,’
which of the following is TRUE?

1. Dijkstra’s algorithm returns the shortest path with minimum total weight.

2. Dijkstra’s algorithm returns the shortest path with the minimum number of edges.
Answer: (A)
Explanation: Dijkstra's algorithm minimizes total weight, not the number of edges.
Hence, statement 1 is true, and statement 2 is false.

Question 7: Why use Bellman-Ford for finding shortest paths instead of Dijkstra’s
algorithm?

1. Bellman-Ford is faster.

2. Bellman-Ford works with graphs containing negative weights.


Answer: (B)
Explanation: Bellman-Ford handles graphs with negative weights, but it is slower
than Dijkstra's algorithm. Hence, only statement 2 is correct.
Question 8: Consider the following statements:

1. BFS on a tree with equal weights finds the minimum distance from the root to any
node.

2. The greedy algorithm for fractional knapsack is optimal.


Answer: (C)
Explanation: Both statements are true. BFS ensures minimum distance in equal-
weight trees, and the greedy approach is optimal for fractional knapsack problems.

Question 9: Consider the set of keys {1, 4, 5, 10, 16, 17, 21}. Construct a binary search tree
with height ‘2.’ Which values appear at internal nodes?

Answer: (B)
Explanation: For height 2, the tree is constructed as follows:

• Root: 10

• Left internal node: 4

• Right internal node: 17.

Internal nodes are 10,4,1710, 4, 17.

Question 10: Consider the set of keys {2, 5, 6, 11, 17, 18, 22}. Draw a binary search tree
with height ‘6.’ What will the child node values be?

Answer: (C)
Explanation: Either 22 or 2222 can serve as a child node depending on the specific
arrangement of the tree.

Question 11: Identify the correct strongly connected components in the graph:

Answer: (C)
Explanation: The graph has five strongly connected components: a,b,c,d,e,f,g{a, b, c}, {d}, {e},
{f}, {g}.

Question 12: Why can't the bold edges in the graph form a spanning tree?

Answer: (A)
Explanation: The bold edges are not connected, so they cannot form a spanning tree.
Question 13: Consider the given statements:

1. A digraph is a graph with exactly 2 vertices.

2. A spanning tree of a graph must contain at least n/2n/2 edges.

3. The sorted edges algorithm for the traveling salesman problem always gives the
optimal result.
Answer: (D)
Explanation: Only statement 2 is true. The other two are incorrect.

Question 14: How many rotations are required to balance the AVL tree after inserting 12?

Answer: (B)
Explanation: One left rotation is required to balance the tree after inserting 12.

Question 15: What is the order of insertion into an AVL tree to avoid single rotations?

Answer: (A)
Explanation: The order 4,2,6,1,3,5,74, 2, 6, 1, 3, 5, 7 avoids the need for single rotations.

Question 16: For the given tree with MAX HEAP property applied, what is the best action
for node 6?

Answer: (C)
Explanation: Compare 66 with its children 1818 and 1515, and then swap with the larger
child if required.

Question 17: Consider the following statements about Depth First Search:

1. A graph with back edges has a cycle.

2. DFS on a directed graph with nn vertices and at least nn edges is guaranteed to find a
back edge.
Answer: (A)
Explanation: Only statement 1 is true. Statement 2 is false because DFS may not
always find a back edge.

Question 18: What happens if we add 6 to the weight of every edge in a graph?
Answer: (B)
Explanation: Adding 6 to every edge increases the total weight by 6(∣V∣−1)6(|V| - 1), where
∣V∣|V| is the number of vertices.

Question 19: Which ordering is NOT a valid topological sort?

Answer: (A)
Explanation: In BACEDFGHBACEDFGH, EE appears before DD, which violates the order
constraint.

Question 20: What is the time complexity to print all the keys of a binary search tree in
sorted order?

Answer: (B)
Explanation: Using in-order traversal, the complexity is O(n)O(n).

Question 21: Which tasks are not completed based on their deadlines and profits?

Tasks:

Task Deadline Profit

T1 2 6

T2 1 4

T3 3 3

T4 2 7

T5 3 2

T6 1 8

Answer: (C)
Explanation: Based on the maximum profit scheduling, tasks T1,T2,T5T1, T2, T5 are not
completed.

Question 22: What sorting algorithm does the given code implement?
Answer: (B)
Explanation: The code implements insertion sort, as it removes elements from the input
array and inserts them into the correct position in the sorted portion.

Question 23: What is the best-case time complexity of the insertion sort algorithm in
Question 22?

Answer: (C)
Explanation: The best case occurs when the array is already sorted, and the time complexity
is O(n)O(n).

Question 24: What is the acyclic component graph of the given directed graph?

Answer: (D)
Explanation: The strongly connected components are identified as {a,b,e}\{a, b, e\}, {f,g}\{f,
g\}, {c,d}and {h}The resulting acyclic component graph is shown as abe,cd,fg,habe, cd, fg, h.

Question 25: In the given undirected graph, which node has the highest degree?

Answer: (B)
Explanation: Node xx has the highest degree of 4, as it connects to the maximum number of
other nodes.

Question 26: What is the relationship between the number of vertices and edges in a
spanning tree?

Answer: n−1n - 1
Explanation: For a graph with nn vertices, a spanning tree contains n−1n - 1 edges.

Question 27: What does the Bellman-Ford algorithm ensure in graphs with negative
weights?

Answer: It guarantees finding the shortest path, unlike Dijkstra's algorithm, which does not
work with negative weights.

Question 28: How does Kruskal’s algorithm choose edges for a minimum spanning tree?

Answer: It selects edges in increasing order of weight while avoiding cycles.


Question 29: What is the primary limitation of DFS when finding shortest paths in
weighted graphs?

Answer: DFS does not account for edge weights, so it may not find the shortest path in
weighted graphs.

Question 30: How does the greedy algorithm solve the fractional knapsack problem?

Answer: By choosing items with the highest value-to-weight ratio until the knapsack is filled,
ensuring an optimal solution.

Question 31: What is the key difference between BFS and DFS in terms of traversal?

Answer: BFS explores all neighbors at the current depth before moving deeper, while DFS
goes as deep as possible along a branch before backtracking.

Question 32: What is the time complexity of Dijkstra's algorithm using a priority queue?

Answer: O((V+E)log⁡V)O((V + E) \log V)


Explanation: Here, VV is the number of vertices, and EE is the number of edges.

Question 33: What is the difference between a tree and a graph?

Answer: A tree is an acyclic connected graph, whereas a graph can have cycles and may not
be connected.

Question 34: What is the primary purpose of a minimum spanning tree?

Answer: To connect all vertices in a graph with the minimum total edge weight without
forming cycles.

Question 35: What does the term "back edge" signify in DFS?

Answer: A back edge points from a node to one of its ancestors in the DFS tree, indicating a
cycle in the graph.

You might also like