Greedy/Divide & Conquer Approach GATE CS PYQ Quiz

Last Updated :
Discuss
Comments

Question 1

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?

  • t>2n−2

  • t>3⌈n/2⌉ and t≤2n−2

  • t>n and t≤3⌈n/2⌉

  • t>⌈log2(n)⌉ and t≤n

Question 2

Suppose you are provided with the following function declaration in the C programming language.

   int partition (int a[], int n); 

The function treats the first element of a[] as a pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part. The following partially given function in the C programming language is used to find the kth smallest element in an array a[ ] of size n using the partition function. We assume k ≤ n

C
int kth_smallest (int a[], int n, int k)
{
   int left_end = partition (a, n);
   if (left_end+1==k)
   {
       return a [left_end];
   }
   if (left_end+1 > k)
   {
      return kth_smallest (____________________);
   }
   else
   {
      return kth_smallest (____________________);
    }
}

The missing argument lists are respectively

  • (a, left_end, k) and (a+left_end+1, n–left_end–1, k–left_end–1)

  • (a, left_end, k) and (a, n–left_end–1, k–left_end–1)

  • (a, left_end+1, N–left_end–1, K–left_end–1) and(a, left_end, k)

  • (a, n–left_end–1, k–left_end–1) and (a, left_end, k)

Question 3

Consider the string abbccddeee. Each letter in the string must be assigned a binary code satisfying the following properties:

  • For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter.
  • For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter.

Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?

  • 21

  • 23

  • 25

  • 30

Question 4

Consider the weights and values of items listed below. Note that there is only one unit of each item.

Question


The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by Vopt. A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by Vgreedy. The value of Vopt − Vgreedy is ______ . 

  • 16

  • 8

  • 44

  • 60

Question 5

Suppose the letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. What is the average length of Huffman codes?

  • 3

  • 2.1875

  • 2.25

  • 1.9375

Question 6

Suppose the letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. Which of the following is the Huffman code for the letter a, b, c, d, e, f?

  • 0, 10, 110, 1110, 11110, 11111

  • 11, 10, 011, 010, 001, 000

  • 11, 10, 01, 001, 0001, 0000

  • 110, 100, 010, 000, 001, 111

Question 7

We are given 9 tasks T1, T2.... T9. The execution of each task requires one unit of time. We can execute one task at a time. Each task Ti has a profit Pi and a deadline di Profit Pi is earned if the task is completed before the end of the dith unit of time.

Task     T1  T2	 T3  T4  T5  T6	 T7 T8  T9
Profit 15 20 30 18 18 10 23 16 25
Deadline 7 2 5 3 4 5 2 7 3

Are all tasks completed in the schedule that gives maximum profit?

  • All tasks are completed

  • T1 and T6 are left out

  • T1 and T8 are left out

  • T4 and T6 are left out

Question 8

We are given 9 tasks T1, T2.... T9. The execution of each task requires one unit of time. We can execute one task at a time. Each task Ti has a profit Pi and a deadline di Profit Pi is earned if the task is completed before the end of the dith unit of time.

Task     T1  T2	 T3  T4  T5  T6	 T7 T8  T9
Profit 15 20 30 18 18 10 23 16 25
Deadline 7 2 5 3 4 5 2 7 3

What is the maximum profit earned?

  • 147

  • 165

  • 167

  • 175

Question 9

Given the intervals [(1, 4), (3, 6), (5, 7), (8, 9)], what would be the output of calling a function that solves the Job Scheduling Algorithm?

  • [(1, 4), (5, 7), (8, 9)]

  •  [(1, 4), (3, 6), (8, 9)]

  •  [(1, 4), (3, 6)]

  •  [(1, 4), (3, 6), (5, 7)]

Question 10

Four matrices M1, M2, M3 and M4 of dimensions pxq, qxr, rxs and sxt respectively can be multiplied is several ways with different number of total scalar multiplications. For example, when multiplied as ((M1 X M2) X (M3 X M4)), the total number of multiplications is pqr + rst + prt. When multiplied as (((M1 X M2) X M3) X M4), the total number of scalar multiplications is pqr + prs + pst. If p = 10, q = 100, r = 20, s = 5 and t = 80, then the number of scalar multiplications needed is:

  • 248000

  • 44000

  • 19000

  • 25000

There are 14 questions to complete.

Take a part in the ongoing discussion