Algorithms in Python: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Measure how four functions grow as the input grows.
Search a list one item at a time, and count the comparisons.
Search a sorted list by halving the range each time.
Use the standard library's binary search.
Write bubble sort and count what it costs.
Repeatedly select the smallest remaining item.
Build the sorted result one item at a time.
Use Python's own sort, and see what it does that yours does not.
Write your first recursive functions, and see the call stack.
Compute Fibonacci three ways and compare the cost.
Use a dictionary or set to replace a scan.
Count things in one pass.
Walk a list from both ends at once.
Find the best window of a fixed size without recomputing it.
Answer many range-sum questions with one pass of setup.
Use a list as a stack, and solve a classic with it.
Use the right structure for first-in, first-out.
Keep the smallest item always to hand.
Sort by splitting, sorting each half, and merging.
Sort by partitioning around a pivot.
Sort without comparing, when the values are small integers.
Work with a grid of rows and columns.
Solve the classic string problems.
Solve the classic number problems.
Solve one problem five ways and compare.
Find the first and last occurrence of a value, not just any one.
Search a sorted list that has been rotated.
Binary search over a range of possible answers, not over a list.
Rearrange a list in place with a read pointer and a write pointer.
Find triples that sum to a target, without a triple loop.
Grow and shrink a window to find the best one.
Match a window against a required set of counts.
Count subarrays with a given sum in one pass.
Find the best contiguous run in one pass.
Group by a computed key and rank the results.
Solve problems with set operations instead of loops.
Sort first, and the problem becomes easy.
Merge, insert and subtract overlapping ranges.
Use the list's own indices as a lookup table.
Generate every subset and every permutation.
Search a space of choices, undoing each one that fails.
Turn an exponential recursion into a linear one.
Fill a table when each answer depends on smaller ones.
Represent a graph and explore it.
Explore level by level, and find the shortest path.
Explore as deep as possible, and use it to find components and cycles.
Find the cheapest route when edges have different costs.
Track groups that merge, and answer "are these connected?" instantly.
Take the locally best choice, and know when that is enough.
Recognise which technique a problem wants.
Answer real questions about a large log with the right structure for each.
Suggest completions for a prefix, three ways.
Find near-duplicates, not just exact ones.
Schedule jobs to meet deadlines and minimise waiting.
Recommend items from what similar users liked.
Plan a journey with changes, costs and constraints.
Build a search index over documents.
Implement an LRU cache and measure the hit rate.
Distribute work across servers, and see what each strategy costs.
Compare two sequences and report the changes.
Build a Huffman code and measure the saving.
Suggest corrections for a misspelled word.
Implement three rate limiters and compare their behaviour.
Page through ranked results without re-sorting everything.
Solve the classic grid questions with flood fill and BFS.
Process data that does not fit in memory.
Match two sides of a market with preferences.
Spot unusual values in a series.
Encode and decode data with bit-level tricks.
Measure properly before deciding anything.
This "fast" duplicate check is slower than the naive one. Explain and fix it.
def has_duplicate(values):
seen = []
for value in values:
if value in seen:
return True
seen.append(value)
return False
print(has_duplicate(list(range(30000))))Find the operations that cost more than they look.
Hit Python's recursion limit, and work around it.
A cache that silently returns the wrong answer.
Sorting that quietly does the wrong thing.
Find the input that makes a good algorithm behave badly.
Count memory, not just time.
Numeric results that are subtly wrong.
Stop as soon as the answer is known.
Optimise the wrong thing, then find the real cost.
The index bugs that hide in loops and slices.
The same algorithm, ruined by the wrong container.
The inputs that break an otherwise correct algorithm.
Randomised algorithms, and how to test them.
Work out the complexity of code you did not write.
Build a Search Engine with an index, ranking and suggestions.
Build a Route Planner with a map, costs and constraints.
Build a Recommendation Engine with several strategies.
Build a Timetable Scheduler with constraints and backtracking.
Build a Data Pipeline that streams, transforms and aggregates.
Build a Puzzle Solver for Sudoku with constraint propagation.
Build a Text Analysis toolkit with several algorithms.
Build a Warehouse Optimiser with packing, picking and routing.
Build an Algorithm Visualiser that traces execution step by step.
Build an Algorithm Benchmark Suite that measures growth.
Analyse the complexity of every operation you use, and justify the numbers.
Given a problem, choose an approach and defend it.
Explain when to write an algorithm and when to call the standard library.
Take a slow function through a full optimisation pass.
Build an Algorithms Workbench โ the complete demonstration of this topic. Solve one realistic problem end to end, choosing a technique at each stage, and prove every choice with measurements.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session