GATE 2014 (Session 3)
65 questions
MCQ 47 · NAT 181-mark 30 · 2-mark 35
Questions 21–25 of 65
The minimum number of arithmetic operations required to evaluate the polynomial for a given value of x, using only one temporary variable is _____.
Answer: 7
Rewrite P(x) in this form:
Steps with one temporary t:
- t = x * x
- t = t + 4
- t = t * x
- t = t * x. Now t holds:
- t = t + 6
- t = t * x. Now t holds:
- t = t + 5
This takes 7 operations with one temporary.
Answer: 7
Steps with one temporary t:
- t = x * x
- t = t + 4
- t = t * x
- t = t * x. Now t holds:
- t = t + 6
- t = t * x. Now t holds:
- t = t + 5
This takes 7 operations with one temporary.
Answer: 7
Consider the following rooted tree with the vertex labelled P as the root
The order in which the nodes are visited during an in-order traversal of the tree is
The order in which the nodes are visited during an in-order traversal of the tree is
Answer: (A)
For a general tree, the in-order rule is: visit the first child's subtree, then the node, then the remaining children's subtrees.
- P: (Q-subtree) P (R-subtree)
- Q-subtree: S Q
- R-subtree: T R (U-subtree) V, where the U-subtree is W U
Putting these together, the order is: S Q P T R W U V
Answer: SQPTRWUV
- P: (Q-subtree) P (R-subtree)
- Q-subtree: S Q
- R-subtree: T R (U-subtree) V, where the U-subtree is W U
Putting these together, the order is: S Q P T R W U V
Answer: SQPTRWUV
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier. Then the maximum possible recursion depth (including the initial call) is _________.
Answer: 19
Each recursive call is on a new unvisited vertex, so the recursion depth is the number of vertices on the current DFS path. DFS may take the neighbours in any order, so from a suitable start it can follow any simple path. The answer is therefore the number of vertices on the longest simple path in the graph.
The graph has a left 3 3 grid, a bridge vertex (bottom middle) with a pendant vertex above it, and a right 3 3 grid that has a pendant vertex on its middle-right vertex. A simple path can cross only once, and a pendant vertex can only be an end of the path.
- Left grid: a snake path covers all 9 vertices and ends at its bottom-right corner, next to . At most 9.
- Bridge : 1 vertex. The pendant above it is a dead end, so skip it.
- Right grid, entered at its bottom-left corner: a 3 3 grid is bipartite with 5 corner-type vertices and 4 of the other type. A path that covers all 9 must start and end on the 5-type, so it cannot end at the middle-right vertex (the other type). So either cover all 9 and skip the pendant (9 vertices), or end at the pendant after at most 8 grid vertices plus the pendant (8 + 1 = 9). At most 9 either way.
Longest path . Any other route is shorter, for example a path that uses the pendant at has at most vertices.
Answer: 19
The graph has a left 3 3 grid, a bridge vertex (bottom middle) with a pendant vertex above it, and a right 3 3 grid that has a pendant vertex on its middle-right vertex. A simple path can cross only once, and a pendant vertex can only be an end of the path.
- Left grid: a snake path covers all 9 vertices and ends at its bottom-right corner, next to . At most 9.
- Bridge : 1 vertex. The pendant above it is a dead end, so skip it.
- Right grid, entered at its bottom-left corner: a 3 3 grid is bipartite with 5 corner-type vertices and 4 of the other type. A path that covers all 9 must start and end on the 5-type, so it cannot end at the middle-right vertex (the other type). So either cover all 9 and skip the pendant (9 vertices), or end at the pendant after at most 8 grid vertices plus the pendant (8 + 1 = 9). At most 9 either way.
Longest path . Any other route is shorter, for example a path that uses the pendant at has at most vertices.
Answer: 19
You have an array of n elements. Suppose you implement quick sort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is
Answer: (A)
Picking the middle element is still a fixed rule. Someone can build the input so that the middle element is always the smallest or the largest. Then every split gives parts of size n1 and 0.
The recurrence is:
Answer:
The recurrence is:
Answer:
The length of the shortest string NOT in the language (over ={a,b}) of the following regular expression is _________.
a*b* (ba)* a*
a*b* (ba)* a*
Answer: 3
Every string of length 0, 1 and 2 is in the language: , a, b, aa, ab, bb (they come from a*b*), and ba (it comes from (ba)*).
Now take "bab". It starts with b, so the leading a* is empty. The b* part can take the first b only, because the next letter is a. Or b* can be empty.
- If b* takes "b", the rest is "ab". (ba)* must start with b, so it is empty, and a* takes "a" and leaves "b". This fails.
- If b* is empty, (ba)* can take "ba" and leaves "b", which a* cannot produce. Or (ba)* is empty and a* cannot start with b. This fails.
So bab is not in the language, and the shortest string not in it has length 3.
Answer: 3
Now take "bab". It starts with b, so the leading a* is empty. The b* part can take the first b only, because the next letter is a. Or b* can be empty.
- If b* takes "b", the rest is "ab". (ba)* must start with b, so it is empty, and a* takes "a" and leaves "b". This fails.
- If b* is empty, (ba)* can take "ba" and leaves "b", which a* cannot produce. Or (ba)* is empty and a* cannot start with b. This fails.
So bab is not in the language, and the shortest string not in it has length 3.
Answer: 3