Algorithms in Swift

Orithms

The algorithms of coding interviews, in very small steps.

The idea

Algorithm problems are harder than they should be. The usual path goes from "here is a dictionary" straight to a medium problem that needs several separate skills at once, and nothing in between builds those skills.

This course goes the other way, on four things:

  1. With enough small steps, everything is simple. The course spends time to buy ease. It never asks for a leap.
  2. Knowledge, not cleverness. A few people get by on raw problem solving. The aim here is different: to hold so many tools so firmly that a problem is recognised, not invented.
  3. Many small problems make the big ones obvious. Each tool gets a ladder of small problems. Then tools are joined, a little at a time. A LeetCode problem arrives only when everything in it is already familiar.
  4. It should always feel easy. If a step feels hard, the step is too big. That is a fault in the course, and the fix is to split the step.

The first goal is the 75 problems of LeetCode 75, in Swift. The course doesn't replace LeetCode. It gets you to each of those problems already knowing how, and LeetCode's own tests have the last word.

The course

24 units · 88 lessons

Each tool has one lesson.

Unit 1 · Lessons 1.1 to 1.8

Swift for problems

The parts of Swift that problems lean on. None of it is an algorithm, and all of it gets in the way until it is automatic.

  1. 1.1

    Loops and indexes

    There are four ways to walk an array (its values, its indexes, both, or backwards), and the job says which before you type.

  2. 1.2

    A string as an array of characters

    A String will not hand over the character at a position, so you make an array of its characters once and work on that.

  3. 1.3

    Characters as numbers

    A character is not a number, and three conversions get you from one to the other: a letter to its place in the alphabet, a digit to its value, and a number to its characters.

  4. 1.4

    A dictionary with a default

    A dictionary hands you a value when you ask for it by its key, tells you when the key is not there, and counts in one line once you give it a default.

  5. 1.5

    A set

    A set says whether a value is there in one step, however many values it holds, and it keeps no order and no repeats.

  6. 1.6

    Sorting by a key

    Swift does the sorting, and you say what "in order" means by answering one question about two elements.

  7. 1.7

    Whole-number arithmetic

    / counts how many whole times one number goes into another, % counts what is left over, and an Int has only so much room.

  8. 1.8

    A grid

    A grid is an array of rows, so the row comes first and the column second, and a position is used only once you know it is inside.

Unit 2 · Lessons 2.1 to 2.8

One pass, remembering something

Walk the array once, carrying one or two values. Most easy problems are exactly this, and most hard ones have it inside them.

  1. 2.1

    Best so far

    One variable holds the best thing seen so far, and every element gets one chance to replace it.

  2. 2.2

    Count as you go

    A counter goes up by one for each thing seen and is read at the end, and a yes or no is handed back the moment one element settles it.

  3. 2.3

    Running total

    One variable holds the total so far, and it is looked at after every step, not only at the end.

  4. 2.4

    Look at the neighbours

    A rule about what is next door reads the position before and the position after, and at either end the neighbour who is not there is never read: it counts as whatever lets the rule pass.

  5. 2.5

    Take it as soon as you can

    Go left to right, take whatever the rule allows the moment it allows it, and mark it taken so that the next decision can see it.

  6. 2.6

    Runs

    Keep the length of the run you are in, start again when the element changes, and remember that the last run ends where nothing changes to tell you.

  7. 2.7

    Two best so far

    Two variables, each with a meaning you can say in one sentence, kept true for every number the loop meets.

  8. 2.8

    How the work grows

    Before you write, count how the work grows with the input, and ask how big the input can be.

Unit 3 · Lessons 3.1 to 3.3

Pointers

Two indexes that move with a purpose, in place of one index that visits everything.

  1. 3.1

    Two lists at once

    One index for each list, moved by the rule of the problem, and when one list runs out, what is left of the other still has to be dealt with.

  2. 3.2

    Two ends moving inward

    One index at the front and one at the back, each step moving at least one of them toward the middle, until they meet.

  3. 3.3

    Read and write

    One index reads every element, another marks where the next kept element goes, and the array is changed in place with nothing new made.

Unit 4 · Lessons 4.1 to 4.2

Strings: words and repeats

Two string jobs that come up on their own: taking a sentence apart, and finding a piece that repeats.

  1. 4.1

    Words

  2. 4.2

    The largest piece that fits both

Unit 5 · Lessons 5.1 to 5.5

Counting and looking up

A set or a dictionary turns "search the whole array again" into one step.

  1. 5.1

    Seen it before

  2. 5.2

    Counting

  3. 5.3

    Comparing counts

  4. 5.4

    A whole value as a key

  5. 5.5

    What I still need

Unit 6 · Lessons 6.1 to 6.2

Windows

A stretch of neighbouring elements that moves along the array, keeping its own total up to date instead of adding it up again.

  1. 6.1

    A window of fixed size

  2. 6.2

    A window that grows and shrinks

Unit 7 · Lessons 7.1 to 7.3

Prefix sums

Totals worked out once and kept, so that a question about any stretch is one subtraction.

  1. 7.1

    Prefix sums

  2. 7.2

    From the left and from the right

  3. 7.3

    Prefix sums with a dictionary

Unit 8 · Lessons 8.1 to 8.4

Binary search

Throwing away half of what's left with every look.

  1. 8.1

    Halving

  2. 8.2

    The first place it becomes true

  3. 8.3

    Following the slope

  4. 8.4

    Searching on the answer

Unit 9 · Lessons 9.1 to 9.5

Stacks

The most recent thing comes off first.

  1. 9.1

    A stack

  2. 9.2

    Undo

  3. 9.3

    Collisions

  4. 9.4

    Nested brackets

  5. 9.5

    Waiting for something bigger

Unit 10 · Lessons 10.1 to 10.3

Queues

First in, first out. The standard library has no queue, and the course uses nothing else, so the first step is making one: an array and a head index.

  1. 10.1

    A queue

  2. 10.2

    Recent items

  3. 10.3

    Taking turns

Unit 11 · Lessons 11.1 to 11.2

Intervals

Pairs of start and end. Sorting them is nearly always the first move.

  1. 11.1

    Do two intervals overlap?

  2. 11.2

    Keep the most that don't overlap

Unit 12 · Lesson 12.1

Recursion

Lists, trees, graphs, backtracking and dynamic programming all stand on this one skill, so it gets a ladder of its own first.

  1. 12.1

    A function that calls itself

Unit 13 · Lessons 13.1 to 13.5

Linked lists

LeetCode hands you ListNode: a value and an optional next. Nearly every mistake here is a lost link, so the ladders are about what each variable points at.

  1. 13.1

    Nodes and walking

  2. 13.3

    Fast and slow

  3. 13.4

    Reversing a list

  4. 13.5

    Splitting and joining

Unit 14 · Lessons 14.1 to 14.6

Binary trees, depth first

LeetCode hands you TreeNode: a value, a left and a right. A tree problem is a question about where the answer travels: back up, down, or both.

  1. 14.1

    Visiting every node

  2. 14.2

    Answers that come back up

  3. 14.3

    Collecting as you go

  4. 14.4

    Passing something down

  5. 14.5

    Leave things as you found them

  6. 14.6

    Down and up together

Unit 15 · Lesson 15.1

Binary trees, level by level

The same trees, visited a row at a time.

  1. 15.1

    Level by level

Unit 16 · Lessons 16.1 to 16.2

Binary search trees

A tree kept in order, so that finding something means going one way at each node.

  1. 16.2

    Deleting from a search tree

Unit 17 · Lessons 17.1 to 17.5

Graphs, depth first

Things and the links between them. The work is in building the lists of neighbours and in never visiting anything twice.

  1. 17.1

    A graph from its edges

  2. 17.2

    Visited

  3. 17.3

    Counting groups

  4. 17.4

    Edges with a direction

  5. 17.5

    Edges with a weight

Unit 18 · Lessons 18.1 to 18.3

Graphs, breadth first

Exploring in rings: everything one step away, then everything two steps away. That is what makes the first arrival the shortest.

  1. 18.1

    Fewest steps

  2. 18.2

    A grid is a graph

  3. 18.3

    Many starts at once

Unit 19 · Lessons 19.1 to 19.5

Heaps

The standard library has no heap, and the course uses nothing else, so you write one and learn it by heart. A step below that teaches something else starts with the heap already in the file. Each LeetCode problem starts from an empty file, so there you type the heap first.

  1. 19.1

    A heap

  2. 19.2

    The k largest

  3. 19.3

    A heap with a set

  4. 19.4

    Sort by one thing, heap the other

  5. 19.5

    Two heaps

Unit 20 · Lessons 20.1 to 20.2

Backtracking

Trying every possibility in an orderly way: a tree of choices, walked depth first.

  1. 20.1

    Choose, go deeper, undo

  2. 20.2

    Choosing without repeats

Unit 21 · Lessons 21.1 to 21.5

Dynamic programming, one row

The name is the frightening part. It means: work out the small answers first, and build each bigger answer from them.

  1. 21.1

    From the last few answers

  2. 21.2

    Remember the answer

  3. 21.3

    The cheapest way here

  4. 21.4

    Take it or skip it

  5. 21.5

    Working out the rule

Unit 22 · Lessons 22.1 to 22.3

Dynamic programming, tables

The same idea when an answer depends on two things at once: a row and a column, or a place in each of two strings.

  1. 22.1

    Grid paths

  2. 22.2

    Two strings, one table

  3. 22.3

    States

Unit 23 · Lessons 23.1 to 23.3

Bits

Numbers as rows of 0s and 1s.

  1. 23.1

    Reading bits

  2. 23.2

    XOR cancels pairs

  3. 23.3

    Bit by bit

Unit 24 · Lessons 24.1 to 24.2

Tries

A tree in which each step down is one more letter.

  1. 24.1

    A tree of letters

  2. 24.2

    Words under a prefix

24 units, 88 lessons. Every piece of code and every number in a lesson comes from a file that was compiled and run.