Back to Home

Python Examples

A collection of Python tutorials and example projects.

For beginners

Patterns at a Glance

Most "hard" interview problems are easy once you spot one of a few recurring patterns. Here are three that solve a dozen of the tutorials below.

Hash-map memory “remember what you've seen”

Walk the data once. For each item, ask "have I already seen what I need?" Keep a dictionary of what you've passed so the answer is an O(1) lookup — no nested loop, no scanning backwards.

Used in: Two Sum, Longest Substring, Group Anagrams, Happy Number.

Dynamic programming “subproblems repeat”

When a recursive solution computes the same subproblem over and over, cache the answers. Then graduate to bottom-up iteration from the base case — same answer, no recursion overhead, often O(1) extra space.

Used in: Climbing Stairs, Coin Change, Decode Ways, Longest Increasing Subsequence.

Problem reframing “this isn't really an array”

When constraints rule out the obvious moves (read-only input, O(1) memory), check if the problem can be reframed as a different structure — a graph, a cycle, a tree, intervals. The technique you need probably already exists for that other structure.

Used in: Find the Duplicate, Happy Number.

More patterns to watch for as you browse: two pointers, sliding window, stacks, BFS/DFS, backtracking. Each shows up in several of the tutorials below.

Basic Python

A quick-reference instruction sheet covering variables, lists, loops, functions, classes, and more.

Interview Challenge: Find the Duplicate

A deceptively simple problem with a surprising O(n) time, O(1) space solution using Floyd's Cycle Detection. Four approaches from brute force to mind-blowing.

Interview Challenge: Longest Substring Without Repeating

Sliding window technique — find the longest substring without repeating characters in a single O(n) pass using a hash map.

Interview Challenge: Valid Parentheses

Stack-based bracket matching — detect balanced parentheses, brackets, and braces in O(n). A classic stack problem.

Interview Challenge: Group Anagrams

Two clever keying strategies — sort key vs. character count key — using defaultdict to group anagram strings together.

Interview Challenge: Two Sum

The classic hash map complement trick — find two numbers that add to a target in a single O(n) pass. Every interviewer knows this one.

Interview Challenge: Product of Array Except Self

Prefix and suffix arrays — compute the product of every element except itself in O(n) without using division.

Interview Challenge: Merge Intervals

Sort then greedily merge overlapping intervals in O(n log n). A common scheduling and calendar problem pattern.

Interview Challenge: Move Zeros

Two-pointer in-place technique — move all zeros to the end while preserving the order of non-zero elements.

Interview Challenge: Coin Change

Bottom-up dynamic programming to find minimum coins for a target — and a proof of why greedy fails on this problem.

Interview Challenge: Climbing Stairs

A disguised Fibonacci problem — four solutions from naive recursion to O(1) space two-variable DP.

Interview Challenge: Decode Ways

A tricky DP problem where zeros break everything — count the ways to decode a digit string as letters A–Z.

Interview Challenge: Longest Increasing Subsequence

Two solutions: an intuitive O(n²) DP and a brilliant O(n log n) patience-sort trick using Python's bisect module.

Interview Challenge: Number of Islands

Classic grid BFS/DFS flood-fill — count connected land regions. A technique that appears in dozens of grid problems.

Interview Challenge: Word Ladder

BFS on an implicit graph — shortest transformation path between words. Includes the bidirectional BFS optimization for a 100x speedup.

Interview Challenge: LRU Cache

Design a cache with O(1) get and put — using OrderedDict or a from-scratch doubly linked list + hashmap with sentinel nodes.

Interview Challenge: Min Stack

Design a stack with O(1) getMin — store (value, current_min) pairs so the minimum is always at your fingertips.

Interview Challenge: Happy Number

A number theory puzzle in disguise — detect cycles in a digit-square sequence using a hash set or Floyd's cycle detection.

Interview Challenge: Josephus Problem

Classic circle elimination puzzle with a beautiful O(n) recursive formula — and a famous O(1) bit rotation trick for k=2.

Interview Challenge: Flatten Nested List

Three Pythonic approaches — recursive, iterative stack, and a generator using yield from for lazy evaluation at any depth.