Coding Interview Examples (With Worked Solutions)

A coding interview example is a short programming problem you solve while an interviewer watches your reasoning. Below are 10 worked examples - each with a solution, its time and space complexity, and the pattern it tests - then the real-world examples that predict on-the-job performance better than any puzzle.

Adam Bullock
Adam Bullock
June 28, 2026 · 5 min read
#interview#coding#examples#getting-hired

A coding interview example is a short programming problem you solve live while an interviewer watches how you think. They are grading your problem-solving, data-structure choice, complexity awareness, and communication - not just whether the code compiles.

This page gives you 10 worked examples grouped by the pattern they test, each with a Python solution and its complexity. Then it covers the part most lists skip: the real-world examples that actually predict whether you can do the job.

The patterns coding interview examples test

Almost every classic example maps to a small set of patterns. Recognize the pattern and the solution follows.

Pattern Example below Typical complexity
Hashing Two Sum, Valid Anagram O(n)
Two pointers Reverse a string, Container with most water O(n)
Stack Valid parentheses O(n)
Binary search Search a sorted array O(log n)
Recursion / trees Max depth of a binary tree O(n)
Dynamic programming Max subarray (Kadane), Climbing stairs O(n)

Say the pattern out loud

Interviewers reward naming the approach before you code: "this looks like a hashing problem, so I can trade space for an O(n) pass." That single sentence signals seniority.

Example 1: Two Sum (hashing)

Given an array and a target, return the indices of the two numbers that add up to the target.

def two_sum(nums, target):
    seen = {}                      # value -> index
    for i, n in enumerate(nums):
        if target - n in seen:
            return [seen[target - n], i]
        seen[n] = i
    return []

A hash map turns the naive O(n^2) double loop into a single O(n) pass. Time O(n), space O(n). This is the most common warm-up example you will see.

Example 2: Valid Parentheses (stack)

Given a string of brackets, decide whether they are balanced.

def is_valid(s):
    pairs = {')': '(', ']': '[', '}': '{'}
    stack = []
    for ch in s:
        if ch in pairs:
            if not stack or stack.pop() != pairs[ch]:
                return False
        else:
            stack.append(ch)
    return not stack

A stack matches each closer to the most recent opener. Time O(n), space O(n).

Example 3: Reverse a String (two pointers)

def reverse(s):
    chars = list(s)
    i, j = 0, len(chars) - 1
    while i < j:
        chars[i], chars[j] = chars[j], chars[i]
        i, j = i + 1, j - 1
    return "".join(chars)

Two pointers swap inward from both ends. Time O(n), space O(n) (O(1) extra if the input is a mutable array).

Example 4: Valid Anagram (hashing)

from collections import Counter

def is_anagram(a, b):
    return len(a) == len(b) and Counter(a) == Counter(b)

Counting characters is O(n); comparing counts is O(1) over a fixed alphabet. Time O(n), space O(1).

Example 5: Binary Search (divide and conquer)

Find a target in a sorted array.

def binary_search(nums, target):
    lo, hi = 0, len(nums) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if nums[mid] == target:
            return mid
        if nums[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return -1

Halving the search space each step gives O(log n) time, O(1) space. Watch the off-by-one boundaries - that is what they are testing.

Example 6: Maximum Depth of a Binary Tree (recursion)

def max_depth(root):
    if not root:
        return 0
    return 1 + max(max_depth(root.left), max_depth(root.right))

Recursion mirrors the tree's shape. Time O(n), space O(h) for the call stack, where h is the height.

Example 7: Maximum Subarray (dynamic programming)

Find the contiguous subarray with the largest sum (Kadane's algorithm).

def max_subarray(nums):
    best = cur = nums[0]
    for n in nums[1:]:
        cur = max(n, cur + n)
        best = max(best, cur)
    return best

Each step decides "extend the current run or start fresh." Time O(n), space O(1).

Example 8: Climbing Stairs (dynamic programming)

How many distinct ways to climb n stairs taking 1 or 2 steps?

def climb_stairs(n):
    a, b = 1, 1
    for _ in range(n):
        a, b = b, a + b
    return a

It is the Fibonacci sequence in disguise. Time O(n), space O(1).

Always state complexity

End every example with its time and space complexity, unprompted. Forgetting to is one of the most common reasons strong coders get dinged.

Example 9: FizzBuzz (communication, not difficulty)

def fizzbuzz(n):
    for i in range(1, n + 1):
        out = ("Fizz" if i % 3 == 0 else "") + ("Buzz" if i % 5 == 0 else "")
        print(out or i)

Trivial logic - they are watching whether you write clean, readable code and handle the edge ordering (check both before the number).

Example 10: Merge Two Sorted Lists (pointers)

def merge(a, b):
    out = []
    i = j = 0
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            out.append(a[i]); i += 1
        else:
            out.append(b[j]); j += 1
    out.extend(a[i:]); out.extend(b[j:])
    return out

A single linear merge of two sorted inputs. Time O(n + m), space O(n + m).

The coding interview examples that actually predict job performance

Here is the uncomfortable part: the examples above test a narrow slice of the job. Plenty of strong engineers freeze on Kadane's algorithm and ship great production code every day - and plenty of puzzle-grinders cannot debug a real outage.

That is why more teams now run practical, real-world rounds - take-home tasks, pair-debugging, "here is a broken service, fix it." Those examples look nothing like the ten above:

These predict on-the-job performance because they are the job. If you want to practice the examples that map to real work - and walk away with a portfolio you can show a hiring manager - that is exactly what HeyDevJob is built for: real broken production systems you fix in a live cloud workspace.

How to practice coding interview examples

  1. Learn the pattern, not the problem. Ten patterns cover most examples; recognizing them beats memorizing hundreds of solutions.
  2. Say your reasoning out loud and state complexity every time.
  3. Mix in real-world examples - debugging, queries, reviews - so you are ready for practical and take-home rounds, not just the whiteboard.

Work through the patterns above until they are reflex, then go practice the real thing.

FAQ

What is a coding interview example?

A short programming problem you solve live while explaining your reasoning. Interviewers grade problem-solving, data-structure choice, complexity awareness, and communication - not just whether the code runs.

How many coding interview examples should I practice?

Breadth beats volume. Cover the core patterns - hashing, two pointers, binary search, recursion/trees, dynamic programming - with a handful of problems each, rather than grinding hundreds at random.

Are coding interview examples like real work?

Often not. Algorithm puzzles test a narrow slice. Real-world examples - debugging a failing service, fixing a slow query, reviewing a pull request - predict on-the-job performance far better, and increasingly show up as take-home and practical rounds.

What language should I use for coding interview examples?

Use the language you are fastest in. Python is popular for its concise syntax and rich standard library, but interviewers care about your reasoning and complexity, not the language.

Practice on real systems, not just examples. Fix real production work in a live cloud workspace - every fix lands on a portfolio hiring managers can open.

Start free →