Practice: Which Technique?
Problems don't come labelled. Practise spotting the technique.
- 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.
- What is
n? Estimate the cost of the brute-force approach. - Is the data sorted, or could sorting help?
- Is there a graph hiding (states and moves, dependencies, networks)?
- Do subproblems repeat? Can you define the answer recursively?
- Is a locally best choice provably safe (greedy)?
- What do you do repeatedly β insert, look up, take the max? Pick the structure to make that cheap.
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.
Identify the technique
A sorted list of 10β· ticket numbers. You must answer many queries 'is number X sold?'
A maze is given as a grid with walls. Find the minimum number of steps from the entrance to the exit.
Courses have prerequisites. Output an order in which a student can take them all.
A road network has travel times (all positive). Find the fastest route from city A to every other city.
Connect 200 offices with cable at minimum total length.
Given n intervals, select the maximum number of non-overlapping ones.
Two DNA strings: find the longest sequence of bases that appears in both in the same order (gaps allowed).
Number of ways to climb n stairs taking 1 or 2 steps at a time.
Find the 10 largest values in a stream of 10βΈ numbers without storing them all.
Check whether any two of 10βΆ numbers add up to a given target.
Edges between friends are added one by one; after each addition, report whether two given people are in the same friend circle.
How many islands (connected groups of land cells) are there in a grid map?
Count the number of pairs (i < j) with a[i] > a[j] in an array of 10β΅ elements.
Locate every occurrence of a 50-character pattern in a 10βΈ-character text.
Assign workers to jobs where each worker can do only certain jobs, maximising the number of jobs done.
Make change for βΉ63 using coins {1, 5, 10, 25} with the fewest coins. Which is the safest approach in general?
Spell-checker suggestion: which dictionary word is closest to the typed word?
Shortest paths from one source in a graph where some edges have negative weight (no negative cycle).
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 n | Plan that is usually OK | Too slow |
|---|---|---|
| β€ 20 | O(2βΏ) or O(n!)-ish brute force | β |
| β€ 500 | O(nΒ³) | O(2βΏ) |
| β€ 5,000 | O(nΒ²) | O(nΒ³) |
| β€ 10βΆ | O(n log n) or O(n) | O(nΒ²) |
| β₯ 10βΈ | O(log n) or O(1) | anything linear in Python |
n = 10β΅ and your idea needs a double loop over all pairs. What should you do?
A DP table has 2,000 Γ 2,000 cells, each computed in O(1). Roughly how many operations?
What does this print? (type the exact output)
n = 1000 pairs = n * (n - 1) // 2 print(pairs)
Mixed practice
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'])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])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))Which of these can be solved in polynomial time?
Dynamic programming recipe.
Practice challenge β pick the technique
Count the number of distinct words in a 10-million-word document.
Find a path through a graph where each edge has capacity, to push as much material as possible from s to t.
Find the length of the longest increasing subsequence of an array of 2,000 numbers.
Detect whether a directed dependency graph contains a cycle.
Merge k sorted lists into a single sorted list of N total elements.
A BST with 10β΅ keys becomes slow after inserting the keys in sorted order. A fix is to useβ¦
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]))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:
| Chapter | Topic | Set | |
|---|---|---|---|
| Week 1 | Python Refresher | 6 problems | Open list β |
| Week 2 | Complexity, Notations, Sorting & Searching | 10 problems | Open list β |
| Week 3 | Arrays, Lists, Stacks, Queues, Hashing | 13 problems | Open list β |
| Week 4 | Graph Algorithms | 10 problems | Open list β |
| Week 5 | Graph Algorithms (Continued) | 9 problems | Open list β |
| Week 6 | Union-Find, Priority Queue, Heap, BST | 14 problems | Open list β |
| Week 7 | Balanced Trees & Greedy Algorithms | 11 problems | Open list β |
| Week 8 | Divide and Conquer | 10 problems | Open list β |
| Week 9 | Dynamic Programming | 15 problems | Open list β |
| Week 10 | String / Pattern Matching | 11 problems | Open list β |
| Week 11 | Network Flows, LP & Complexity Classes | 6 problems | Open list β |
| Week 12 | Summary & Final Review | 12 problems | Open list β |
| Week + | Recursion & Backtracking (bonus) | 6 problems | Open 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.