Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Algorithms & Competitive Programming — Alpha School

Lesson 20: Capstone — A Full Contest Round, Solved Live

This final lesson is a full contest round, worked end to end. The round has three problems of increasing difficulty, and the aim is not just to solve them but to show the process: reading, planning, coding, testing, and moving on. Problem A is a warm-up: given a list of numbers, find the largest sum of any two different numbers. The answer is simply the sum of the two largest values — no cleverness needed, just the right observation, and a reminder that the easiest problem deserves a quick, clean solution, not an over-engineered one.

Problem B is a classic: given a string, find the length of the longest substring without a repeating character. You met this in Lesson 18 — the sliding window with a dictionary of last positions. In a contest you would recognise the pattern from the words "longest" and "without repeating" and reach straight for the window. Problem C is harder: given a grid of heights, find the cheapest path from the top-left corner to the bottom-right, moving only right or down. The grid is a graph, the moves are edges, and the answer is a shortest path — Dijkstra's algorithm from Lesson 16, or dynamic programming, since movement is restricted to two directions and the grid has no cycles.

# Capstone problem A: largest sum of two different numbers.
def largest_pair_sum(nums):
    if len(nums) < 2:
        raise ValueError("need at least two numbers")
    first = second = float("-inf")
    for x in nums:
        if x > first:
            second, first = first, x
        elif x > second:
            second = x
    return first + second


print(largest_pair_sum([3, 1, 9, 4]))  # 13 — 9 + 4
print(largest_pair_sum([-5, -1, -3]))  # -4 — -1 + -3


# Capstone problem C (sketch): cheapest path in a grid, moving right/down.
# State: dp[r][c] = cheapest cost to reach (r, c).
# Recurrence: dp[r][c] = grid[r][c] + min(dp[r-1][c], dp[r][c-1]).
def cheapest_path(grid):
    rows, cols = len(grid), len(grid[0])
    dp = [[0] * cols for _ in range(rows)]
    for r in range(rows):
        for c in range(cols):
            if r == 0 and c == 0:
                dp[r][c] = grid[r][c]
            elif r == 0:
                dp[r][c] = grid[r][c] + dp[r][c - 1]
            elif c == 0:
                dp[r][c] = grid[r][c] + dp[r - 1][c]
            else:
                dp[r][c] = grid[r][c] + min(dp[r - 1][c], dp[r][c - 1])
    return dp[rows - 1][cols - 1]


print(cheapest_path([[1, 3, 1], [1, 5, 1], [4, 2, 1]]))  # 7

Your next contest

The round is over, and the pattern is clear: every problem in it was solved by recognising a pattern from this course — two largest values, a sliding window, a shortest path on a grid. That recognition is the real skill competitive programming builds, and it only comes from practice. Your next step is concrete: create accounts on the platforms from Lesson 19's reading — Codeforces, AtCoder, and the CSES Problem Set are good places to start — and enter a beginner contest. Expect to solve one or two problems in your first round. That is normal, and it is the beginning, not the verdict.

Keep the habits from this course with you. Read the limits before you code. Name the complexity of your solution before you submit it. Test the edge cases by hand. Keep a notebook of your mistakes, and re-read it before each contest. And keep building: the algorithms you have learned — sorting, searching, recursion, dynamic programming, graphs, greedy methods, number theory — are the same ideas that professional programmers use every day, whether or not they ever enter a contest. You now think like a competitive programmer. Go and compete.

Going further

Looking back at the round, notice how little code each problem needed. Problem A was a single pass tracking two values. Problem B was a sliding window of a dozen lines. Problem C was a small table filled by a recurrence. None of them required a clever trick beyond recognising the pattern — and that is typical. Most contest problems are solved not by inventing something new, but by recognising something you have seen before and adapting it. The more problems you solve, the larger your library of patterns becomes, and the faster the recognition gets.

The same patterns appear outside contests, which is worth remembering when the scoreboard feels like the whole point. Sliding windows process streams of data in monitoring tools. Shortest paths route packets across networks and deliveries across cities. Dynamic programming aligns DNA sequences and compares files. Union-Find groups connected users in a social network. The course taught you competitive programming, but the ideas are general — they are simply the clearest form of thinking about problems that involve structure, choice, and limited resources.

One last piece of advice for your first contest: solve the problems you can, and do not be discouraged by the ones you cannot. Every competitive programmer, including the ones at the top of the leaderboard, started by solving one problem in a round and staring at the rest. The rating system measures progress over months, not performance in a single sitting, and the gap between a beginner and an expert is mostly a count of problems solved — a few hundred, patiently, one at a time. This course gave you the patterns; the contests will give you the practice. Go and begin.

Watch & Learn

One video to watch for this unit, from a trusted educator.

Key Terms

Contest round
One sitting of a competition: a fixed set of problems in a fixed time.
Warm-up problem
The easiest problem in a round, meant to be solved quickly and cleanly.
Pattern recognition
Seeing which known technique a new problem needs — the core skill of competitive programming.
Grid DP
Dynamic programming over a grid, where each cell's answer follows from its neighbours.
Codeforces
A popular competitive programming platform with regular contests and a rating system.
AtCoder
A competitive programming platform known for beginner-friendly contests.
CSES Problem Set
A curated collection of practice problems covering the standard competitive programming topics.

Discussion Questions

  1. Problem A is solved by the two largest values. Why is no sorting or searching needed — and when would sorting be the simpler answer?
  2. Problem C can be solved by Dijkstra's algorithm or by dynamic programming. Why does DP work here, and when would it not?
  3. What will your practice routine look like after this course? How many problems a week, on which platform, and how will you review your mistakes?

Practice Questions

  1. What does largest_pair_sum([3, 1, 9, 4]) return, and why?
  2. Why does the function raise a ValueError for a list with fewer than two numbers?
  3. In the grid DP, why does dp[r][c] only look at the cell above and the cell to the left?
  4. Trace cheapest_path([[1, 3, 1], [1, 5, 1], [4, 2, 1]]). What is dp[1][1]?
  5. Name three platforms from the lesson where you can practise and compete.
Show answers
  1. 13, from 9 + 4. The two largest values are 9 and 4; the function tracks the largest and second-largest in one pass.
  2. Because a pair needs two different numbers. Raising with a clear message is better than returning a meaningless answer.
  3. Because movement is restricted to right and down. A cell can only be reached from above or from the left, so the cheapest route comes through one of those two.
  4. 7. dp[1][1] = 5 + min(dp[0][1] = 4, dp[1][0] = 2) = 5 + 2 = 7; the final answer is 7.
  5. Codeforces, AtCoder, and the CSES Problem Set. All three offer beginner-friendly contests and practice problems.
Keep reading to complete this lesson