PDSA Interactive Book
Practice

Practice: Which Technique?

Problems don't come labelled. Practise spotting the technique.

After this chapter you can…
  • map a problem statement to the right technique
  • estimate whether a plan is fast enough
  • practise recognition across all twelve weeks

How to use this page

Real problems do not announce which chapter they come from. The skill tested in exams β€” and in interviews and contests β€” is recognising the technique behind a problem statement. Each question below describes a task in plain words; choose the approach that fits best and read why.

A routine that works
  1. What is n? Estimate the cost of the brute-force approach.
  2. Is the data sorted, or could sorting help?
  3. Is there a graph hiding (states and moves, dependencies, networks)?
  4. Do subproblems repeat? Can you define the answer recursively?
  5. Is a locally best choice provably safe (greedy)?
  6. What do you do repeatedly β€” insert, look up, take the max? Pick the structure to make that cheap.
Want more problems?

For a large collection of competitive-programming style problems and notes, see Vikrant Mehta's DSA resource: [vikrantmehta123.github.io/dsa](https://vikrantmehta123.github.io/dsa/). It is an independent resource (not part of the IIT Madras course); all credit for it goes to its author.

Open Vikrant Mehta's DSA resource β†—

Identify the technique

Technique chooser β€” describe the problem, get candidatesInteractive
Check yourself

A sorted list of 10⁷ ticket numbers. You must answer many queries 'is number X sold?'

Check yourself

A maze is given as a grid with walls. Find the minimum number of steps from the entrance to the exit.

Check yourself

Courses have prerequisites. Output an order in which a student can take them all.

Check yourself

A road network has travel times (all positive). Find the fastest route from city A to every other city.

Check yourself

Connect 200 offices with cable at minimum total length.

Check yourself

Given n intervals, select the maximum number of non-overlapping ones.

Check yourself

Two DNA strings: find the longest sequence of bases that appears in both in the same order (gaps allowed).

Check yourself

Number of ways to climb n stairs taking 1 or 2 steps at a time.

Check yourself

Find the 10 largest values in a stream of 10⁸ numbers without storing them all.

Check yourself

Check whether any two of 10⁢ numbers add up to a given target.

Check yourself

Edges between friends are added one by one; after each addition, report whether two given people are in the same friend circle.

Check yourself

How many islands (connected groups of land cells) are there in a grid map?

Check yourself

Count the number of pairs (i < j) with a[i] > a[j] in an array of 10⁡ elements.

Check yourself

Locate every occurrence of a 50-character pattern in a 10⁸-character text.

Check yourself

Assign workers to jobs where each worker can do only certain jobs, maximising the number of jobs done.

Check yourself

Make change for β‚Ή63 using coins {1, 5, 10, 25} with the fewest coins. Which is the safest approach in general?

Check yourself

Spell-checker suggestion: which dictionary word is closest to the typed word?

Check yourself

Shortest paths from one source in a graph where some edges have negative weight (no negative cycle).

Check yourself

Autocomplete: given a prefix, list all dictionary words that start with it.

Estimate before you code

A practical test. Python manages roughly 10⁷ simple operations per second. Decide whether the plan fits in about a second.

Input size nPlan that is usually OKToo slow
≀ 20O(2ⁿ) or O(n!)-ish brute forceβ€”
≀ 500O(nΒ³)O(2ⁿ)
≀ 5,000O(nΒ²)O(nΒ³)
≀ 10⁢O(n log n) or O(n)O(nΒ²)
β‰₯ 10⁸O(log n) or O(1)anything linear in Python
Check yourself

n = 10⁡ and your idea needs a double loop over all pairs. What should you do?

Check yourself

A DP table has 2,000 Γ— 2,000 cells, each computed in O(1). Roughly how many operations?

Predict the output

What does this print? (type the exact output)

n = 1000
pairs = n * (n - 1) // 2
print(pairs)

Mixed practice

Predict the output

What does this program print?

from collections import deque
adj = {'A': ['B', 'C'], 'B': ['D'], 'C': ['D', 'E'], 'D': ['F'], 'E': ['F'], 'F': []}
dist, q = {'A': 0}, deque(['A'])
while q:
    u = q.popleft()
    for v in adj[u]:
        if v not in dist:
            dist[v] = dist[u] + 1
            q.append(v)
print(dist['F'])
Predict the output

What does this program print?

import heapq
h = []
for x in [8, 3, 5, 1, 9]:
    heapq.heappush(h, x)
print(heapq.heappop(h), heapq.heappop(h), h[0])
Predict the output

What does this program print?

memo = {}
def ways(n):
    if n <= 1:
        return 1
    if n not in memo:
        memo[n] = ways(n - 1) + ways(n - 2)
    return memo[n]
print(ways(10))
Check yourself

Which of these can be solved in polynomial time?

Put in order

Dynamic programming recipe.

Practice challenge β€” pick the technique

…
Check yourself

Count the number of distinct words in a 10-million-word document.

Check yourself

Find a path through a graph where each edge has capacity, to push as much material as possible from s to t.

Check yourself

Find the length of the longest increasing subsequence of an array of 2,000 numbers.

Check yourself

Detect whether a directed dependency graph contains a cycle.

Check yourself

Merge k sorted lists into a single sorted list of N total elements.

Check yourself

A BST with 10⁡ keys becomes slow after inserting the keys in sorted order. A fix is to use…

Predict the output

What does this print? (type the exact output)

def lis(a):
    best = [1] * len(a)
    for i in range(len(a)):
        for j in range(i):
            if a[j] < a[i]:
                best[i] = max(best[i], best[j] + 1)
    return max(best)
print(lis([3, 1, 4, 1, 5, 9, 2, 6]))
Check yourself

Which description matches 'Dijkstra with a heap'?

LeetCode practice, chapter by chapter

Every chapter ends with a LeetCode set matched to its technique. Start from the chapter you are studying, or jump straight to a list:

ChapterTopicSet
Week 1Python Refresher6 problemsOpen list β†’
Week 2Complexity, Notations, Sorting & Searching10 problemsOpen list β†’
Week 3Arrays, Lists, Stacks, Queues, Hashing13 problemsOpen list β†’
Week 4Graph Algorithms10 problemsOpen list β†’
Week 5Graph Algorithms (Continued)9 problemsOpen list β†’
Week 6Union-Find, Priority Queue, Heap, BST14 problemsOpen list β†’
Week 7Balanced Trees & Greedy Algorithms11 problemsOpen list β†’
Week 8Divide and Conquer10 problemsOpen list β†’
Week 9Dynamic Programming15 problemsOpen list β†’
Week 10String / Pattern Matching11 problemsOpen list β†’
Week 11Network Flows, LP & Complexity Classes6 problemsOpen list β†’
Week 12Summary & Final Review12 problemsOpen list β†’
Week +Recursion & Backtracking (bonus)6 problemsOpen list β†’

Key takeaways

  • Read the problem for clues: sorted data, shortest/fewest, dependencies, connect-everything, overlapping subproblems, 'always the largest', matching/capacity.
  • Estimate the brute-force cost first; with n around 10⁡ or more you need roughly O(n log n).
  • Pick the data structure for the operation you repeat most: hash for lookup, heap for extreme, union–find for groups, trie for prefixes.
  • Keep practising on varied problems β€” recognition improves with volume.

PDSA Interactive Book Β· Programming, Data Structures and Algorithms using Python (BSCS2002), IIT Madras BS Degree Programme.
Created by Ammar Hashmi Β· Β© 2026. An unofficial study companion β€” the syllabus follows the official course page and the examples are built around the PDSA Online Textbook; lectures by Prof. Madhavan Mukund (CMI). Always cross-check with the official course material.
Found a mistake or have a suggestion? Report it on GitHub β†— Β· Press / to search.