Sub topic 22
GATE Problem Patterns and Strategies
This chapter assumes you have worked through the earlier chapters: asymptotic notation, loop analysis, the Master theorem, the standard sorts, and hashing with chaining and open addressing. Here you will learn to recognize the question types GATE CS repeats on these topics and to answer each one with a short, reliable procedure.
Triage: Identify the Pattern First
Most questions in this area fall into five families. Naming the family in the first few seconds tells you which method to use.
Pattern 1: Counting Loop Iterations
The loop variable usually changes in one of three ways. If it grows by a constant (j += 1), the loop runs about n times. If it is multiplied or divided (j *= 2), it runs about log n times. If the inner bound depends on the outer variable, you must sum.
The code below counts the inner iterations of a loop where i doubles and j runs up to i.
def count_ops(n):
c = 0
i = 1
while i <= n:
j = 1
while j <= i:
c += 1
j += 1
i *= 2
return c
for n in (1, 2, 4, 8, 16, 1024):
print(n, count_ops(n))For n = 16 the inner loop runs 1 + 2 + 4 + 8 + 16 = 31 times, which is 2n - 1. The sum is a geometric series dominated by its last term, so the total is Θ(n). Many candidates answer n log n here, because they multiply the two loop bounds without noticing that the inner bound is small for most outer iterations.
Pattern 2: Recurrences
Match the recurrence to one of these shapes.
| Recurrence | Method | Result |
|---|---|---|
| T(n) = 2T(n/2) + n | Master, case 2 | Θ(n log n) |
| T(n) = T(n/2) + 1 | Master, case 2 | Θ(log n) |
| T(n) = 4T(n/2) + n | Master, case 1 | Θ(n²) |
| T(n) = 2T(n/2) + n² | Master, case 3 | Θ(n²) |
| T(n) = T(n-1) + n | Unroll | Θ(n²) |
| T(n) = 2T(n-1) + 1 | Unroll | Θ(2ⁿ) |
| T(n) = T(√n) + 1 | Substitute n = 2^m | Θ(log log n) |
| T(n) = T(n/3) + T(2n/3) + n | Recursion tree | Θ(n log n) |
| T(n) = 2T(n/2) + n log n | Extended case 2 | Θ(n log² n) |
The Master theorem needs a constant a ≥ 1, a constant b > 1, and a polynomially comparable f(n). A recurrence like T(n) = 2T(n/2) + n/log n falls in the gap between cases, so the basic theorem does not apply. Subtract-style recurrences such as T(n-1) never fit it either.
Pattern 3: Property Lookups for Searching and Sorting
Many questions ask which statement is true. Memorize this table.
| Algorithm | Best | Worst | Stable | In place |
|---|---|---|---|---|
| Insertion sort | n | n² | Yes | Yes |
| Selection sort | n² | n² | No | Yes |
| Bubble sort (with early exit) | n | n² | Yes | Yes |
| Merge sort | n log n | n log n | Yes | No, O(n) extra |
| Quick sort (last pivot) | n log n | n² | No | Yes |
| Heap sort | n log n | n log n | No | Yes |
Related facts that GATE tests often:
- Binary search needs at most ⌊log₂ n⌋ + 1 comparisons on n sorted items. Linear search needs n in the worst case.
- Building a heap bottom-up costs O(n), but n separate insertions cost O(n log n).
- Selection sort makes at most n - 1 swaps. Insertion sort makes about as many shifts as there are inversions.
- Any comparison sort needs ⌈log₂ n!⌉ = Ω(n log n) comparisons in the worst case.
- Counting sort runs in O(n + k). Radix sort with d digits runs in O(d(n + k)) and needs a stable inner sort.
Pattern 4: Hand-Tracing a Hash Table
Questions give a table size, a hash function, a probing rule, and a key sequence. They then ask for the final table or for the slot of one key. Do not shortcut: write the probe sequence for every key in order.
def probe_insert(table, key):
m = len(table)
for i in range(m):
slot = (key % m + i) % m
if table[slot] is None:
table[slot] = key
return slot
raise OverflowError("table full")
table = [None] * 10
for k in (42, 23, 34, 52, 46, 33):
print(k, "->", probe_insert(table, k))
print(table)Key 52 hashes to slot 2, which holds 42. It then tries slots 3 and 4, which are also full, and lands in slot 5. Key 33 hashes to slot 3 and probes through 3, 4, 5, and 6 before landing in slot 7. The final table is [None, None, 42, 23, 34, 52, 46, 33, None, None]. The run of full slots at indexes 2 to 7 is primary clustering.
For quadratic probing, h(k, i) = (k mod m + c₁i + c₂i²) mod m. For double hashing, h(k, i) = (h₁(k) + i·h₂(k)) mod m. Check that h₂(k) is never 0. Check also that it is coprime to m, or the probe sequence will not visit every slot.
Pattern 5: Load Factor Formulas
The load factor is α = n/m, where n is the number of stored keys and m is the number of slots.
| Scheme | Unsuccessful search | Successful search |
|---|---|---|
| Chaining | 1 + α | 1 + α/2 |
| Uniform open addressing | 1/(1 - α) | (1/α) ln(1/(1 - α)) |
| Linear probing | ½(1 + 1/(1 - α)²) | ½(1 + 1/(1 - α)) |
With α = 0.5, uniform open addressing needs 2 probes for an unsuccessful search. With α = 0.9 it needs 10. Open addressing requires α < 1, while chaining allows α > 1.
Strategy and Common Errors
- Write the function's input size first. For hashing it is n and m. For graph-free problems here it is usually n.
- Check the answer with a small input, such as n = 1, 2, or 4. A one-minute trace catches most wrong formulas.
- Read whether the question asks for best, average, or worst case, and whether it asks for Θ, O, or Ω. Several options may be true under O, but only one is tight.
- In "which is true" questions, eliminate options that contradict a single remembered fact. Quick sort is not stable, and merge sort is not in place.
- When a hash question lists keys, process them strictly in the given order. Order changes the table under open addressing.
Exercise
A hash table has 7 slots and uses h(k) = k mod 7 with linear probing. Keys 15, 22, 8, 29 are inserted in that order. In which slots do they end up?
Show answer
15 mod 7 = 1, so 15 goes in slot 1. 22 mod 7 = 1 is taken, so 22 goes in slot 2. 8 mod 7 = 1 is taken, then slot 2 is taken, so 8 goes in slot 3. 29 mod 7 = 1 is taken, then slots 2 and 3 are taken, so 29 goes in slot 4. The slots are 1, 2, 3, and 4 for 15, 22, 8, and 29.
Exercise
What is the time complexity of the code below, and why is it not Θ(n log n)? i = 1; while i <= n: for j in 1..i: work(); i = i * 2
Show answer
The inner loop runs 1, 2, 4, ..., up to about n times. The sum is about 2n - 1, so the complexity is Θ(n). The outer loop runs log n times, but most iterations have a small inner loop, so multiplying log n by n overcounts.
Exercise
A chained hash table holds 200 keys in 100 slots. What is the expected number of probes for an unsuccessful search?
Show answer
The load factor is α = 200/100 = 2. The expected cost is 1 + α = 3 probes.