GATE 2015 (Session 2)
64 questions
MCQ 42 · NAT 221-mark 29 · 2-mark 35
Questions 46–50 of 64
Given below are some algorithms, and some algorithm design paradigms.
Match the above algorithms on the left to the corresponding design paradigm they follow.
Match the above algorithms on the left to the corresponding design paradigm they follow.
Answer: (C)
Match each algorithm with the idea it uses:
- 1. Dijkstra's shortest path: greedy design (iii). At each step it picks the closest unvisited vertex and never changes that choice later.
- 2. Floyd-Warshall: dynamic programming (ii). It builds shortest paths through vertices 1 to from the stored answers for 1 to , so the same smaller subproblems are reused.
- 3. Binary search on a sorted array: divide and conquer (i). It compares with the middle element and then searches only one half.
- 4. Backtracking search on a graph: depth-first search (iv). It goes as deep as it can along one path and, when it gets stuck, steps back and tries another branch. Breadth-first search (v) explores level by level and does not step back along a path.
So the match is 1-iii, 2-ii, 3-i, 4-iv.
Answer: (C)
- 1. Dijkstra's shortest path: greedy design (iii). At each step it picks the closest unvisited vertex and never changes that choice later.
- 2. Floyd-Warshall: dynamic programming (ii). It builds shortest paths through vertices 1 to from the stored answers for 1 to , so the same smaller subproblems are reused.
- 3. Binary search on a sorted array: divide and conquer (i). It compares with the middle element and then searches only one half.
- 4. Backtracking search on a graph: depth-first search (iv). It goes as deep as it can along one path and, when it gets stuck, steps back and tries another branch. Breadth-first search (v) explores level by level and does not step back along a path.
So the match is 1-iii, 2-ii, 3-i, 4-iv.
Answer: (C)
A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with , and hence there cannot be any entry to the right of, or below a . The following Young tableau consists of unique entries.
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is _______.
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is _______.
Answer: 5
Taking out 1 leaves a hole. Keep filling the hole with the smaller of the entry on its right and the entry below it:
- Hole at (1,1): min(2, 3) = 2, so 2 moves left
- Hole at (1,2): min(5, 4) = 4, so 4 moves up
- Hole at (2,2): min(6, 12) = 6, so 6 moves left
- Hole at (2,3): min(23, 18) = 18, so 18 moves up
- Hole at (3,3): min(25, ) = 25, so 25 moves left
- Hole at (3,4) is filled with
Entries shifted: 2, 4, 6, 18, 25. That is 5 entries.
Answer: 5
- Hole at (1,1): min(2, 3) = 2, so 2 moves left
- Hole at (1,2): min(5, 4) = 4, so 4 moves up
- Hole at (2,2): min(6, 12) = 6, so 6 moves left
- Hole at (2,3): min(23, 18) = 18, so 18 moves up
- Hole at (3,3): min(25, ) = 25, so 25 moves left
- Hole at (3,4) is filled with
Entries shifted: 2, 4, 6, 18, 25. That is 5 entries.
Answer: 5
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 smallest element in an array a[ ] of size n using the partition function. We assume
The missing argument lists are respectively
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 smallest element in an array a[ ] of size n using the partition function. We assume
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
Answer: (A)
Read the code as it is written: it returns a[left_end] when left_end + 1 == k, so left_end is the 0-based index of the pivot after partition. The pivot is then the (left_end + 1)-th smallest element.
- If left_end + 1 k, the k-th smallest lies left of the pivot, in a[0 .. left_end 1]. That part starts at a and has left_end elements, so the call is (a, left_end, k).
- Otherwise the answer lies right of the pivot. That part starts at a + left_end + 1 and has n left_end 1 elements. The pivot and the left_end elements before it are all smaller, so we want the (k left_end 1)-th smallest of the right part: (a+left_end+1, n-left_end-1, k-left_end-1).
Option B starts the right-part search at a, so it searches the left part again. Options C and D put the two argument lists in the wrong cases.
Answer: (A)
- If left_end + 1 k, the k-th smallest lies left of the pivot, in a[0 .. left_end 1]. That part starts at a and has left_end elements, so the call is (a, left_end, k).
- Otherwise the answer lies right of the pivot. That part starts at a + left_end + 1 and has n left_end 1 elements. The pivot and the left_end elements before it are all smaller, so we want the (k left_end 1)-th smallest of the right part: (a+left_end+1, n-left_end-1, k-left_end-1).
Option B starts the right-part search at a, so it searches the left part again. Options C and D put the two argument lists in the wrong cases.
Answer: (A)
Which one of the following hash functions on integers will distribute keys most uniformly over 10 buckets numbered 0 to 9 for i ranging from 0 to 2020?
Answer: (B)
Only the last digit of matters for .
(B) The last digits of for last digits 0 to 9 are 0, 1, 8, 7, 4, 5, 6, 3, 2, 9. Every bucket occurs once, so the keys spread evenly over all 10 buckets.
(A) The last digit of a square is only 0, 1, 4, 5, 6 or 9, so 4 buckets are never used.
(C) , so it is the same as (A).
(D) , which is always even, so the odd buckets are never used. In fact only buckets 0, 2 and 8 are used.
Answer: (B)
(B) The last digits of for last digits 0 to 9 are 0, 1, 8, 7, 4, 5, 6, 3, 2, 9. Every bucket occurs once, so the keys spread evenly over all 10 buckets.
(A) The last digit of a square is only 0, 1, 4, 5, 6 or 9, so 4 buckets are never used.
(C) , so it is the same as (A).
(D) , which is always even, so the odd buckets are never used. In fact only buckets 0, 2 and 8 are used.
Answer: (B)
The secant method is used to find the root of an equation f(x)=0. It is started from two distinct estimates and for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if is very small and then is the solution. The procedure is given below. Observe that there is an expression which is missing and is marked by ?. Which is the suitable expression that is to be put in place of ? so that it follows all steps of the secant method?
Answer: (C)
The secant line passes through and , so its slope is . The line through the second point is . Setting gives the point where it meets the x-axis:
This is the new estimate, and the procedure then sets and . This matches option (C).
Options (A) and (B) multiply by the slope instead of dividing by it, so they are wrong. Option (D) uses , which the procedure never defines (it keeps only ).
Answer: (C)
This is the new estimate, and the procedure then sets and . This matches option (C).
Options (A) and (B) multiply by the slope instead of dividing by it, so they are wrong. Option (D) uses , which the procedure never defines (it keeps only ).
Answer: (C)