Sub topic 18
GATE Problem Patterns and Strategies
What to Assume Before Solving
This chapter assumes that you can analyze simple loops, solve basic recurrences, use logarithms, and perform modular arithmetic. It also assumes that you know the main properties of searching, sorting, heaps, and hash tables.
GATE questions often test a small set of patterns. The fastest approach is to identify the pattern before calculating every detail.
Complexity Patterns
For loops, track how the control variable changes:
- Increasing or decreasing by a constant usually gives a linear term.
- Multiplying or dividing by a constant usually gives a logarithmic term.
- A loop inside a loop usually multiplies costs, unless the inner limit depends on the outer variable.
- Consecutive loops add costs, but the largest asymptotic term usually dominates.
For example, the following loop runs logarithmically because x doubles:
def count_steps(n):
x = 1
steps = 0
while x < n:
x *= 2
steps += 1
return steps
print(count_steps(100))A common trap is a loop whose total work forms a series. If the inner loop runs i times while i ranges from 1 to n, the total is
\[ 1 + 2 + \cdots + n = \Theta(n^2). \]
For recursion, write a recurrence before choosing an answer. For example, merge sort gives
\[ T(n) = 2T(n/2) + \Theta(n) = \Theta(n\log n). \]
Binary search gives
\[ T(n) = T(n/2) + \Theta(1) = \Theta(\log n). \]
Searching and Sorting Questions
Check the input condition first. Binary search requires sorted data and takes O(log n) time in the worst case. Linear search does not require sorting and takes O(n) in the worst case.
For sorting questions, separate these properties:
| Property | Question to ask |
|---|---|
| Stability | Do equal keys keep their original order? |
| In-place | Does the algorithm use constant or small extra storage? |
| Adaptivity | Does nearly sorted input make it faster? |
Insertion sort is stable, in-place, and adaptive. Merge sort is stable but normally uses O(n) extra space. Quicksort is usually in-place and has average O(n log n) time, but a poor pivot can produce O(n²) time. Heap sort guarantees O(n log n) time and uses little extra space, but it is not stable.
A sorting question may also test the comparison lower bound. Every comparison sort needs Ω(n log n) comparisons in the worst case. Counting or radix sort can avoid this bound because they use key structure rather than only comparisons.
Hashing and GATE Tactics
For hashing, write every probe explicitly. With linear probing,
\[ h(k,i) = (h(k)+i)\bmod m. \]
Stop at the first empty slot. For chaining, several keys may occupy the same slot, so count the chain length instead.
Use this decision process:
In a multiple-choice question, test boundary cases: an empty array, one element, sorted input, duplicate keys, a full hash table, or a pivot at an endpoint. These cases often distinguish average complexity from worst-case complexity.
Exercise
A loop starts with i = 1 and repeatedly replaces i with 2*i until i >= n. What is its time complexity, and what search algorithm has the same recurrence?
Show answer
Its time complexity is Θ(log n). Binary search has the recurrence T(n) = T(n/2) + Θ(1), so it also runs in Θ(log n).