Spiral 50

0 of 19 lessons completed0 of 50 problems solved

Helper data structures and core patterns
Hash table
Stack
Queue
Heap
Core patterns
Problems
Linked lists, trees and graphs
Linked list
Tree
Binary Search Tree
Trie
Graph
Graph algorithms
Problems
Backtracking, greedy and dynamic programming
Backtracking
Greedy vs dynamic programming
Problems
Fundamentals

Combinatorics

Let me give you some advice that will help you approach almost any problem.

Always start with a brute force solution.

A lot of people skip it because they think it is not fast enough to pass the interview. It might or might not be, but it is still worth starting with it.

First, an interviewer might ask you to implement it or to find something better. In both cases they know that you can find a first approach, and it is easier for them to give hints towards an optimised solution if you are stuck.

Second, it gives you something to build on top of. It usually shows what can be optimised and where the repeated work is. Software engineering works exactly like this: you start with a solution that works, then optimise it.

So here are the most common ways to generate candidates for a brute force solution.

Pairs. O(n²) time.

Given an array, generate every pair of its elements.

This can be solved with two nested loops. The outer loop picks the first element with index i, and the inner loop picks every element after it with index j, so each pair appears once.

There are n(n−1)2\frac{n(n - 1)}{2} possible pairs.

A
B
C
D
E
01234
i j
A and B
1 / 10
Every pair
Python
def every_pair(nums: list[int]) -> None:
# Print every pair
pass
every_pair([3, 1, 4, 1, 5])

Triplets. O(n³) time.

Given an array, generate every triplet of its elements.

This can be solved with three nested loops. The outer loop picks the first element with index i, the middle loop picks every element after it with index j, and the inner loop picks every element after that with index k, so each triplet appears once.

There are n(n−1)(n−2)6\frac{n(n - 1)(n - 2)}{6} possible triplets.

A
B
C
D
E
01234
i j k
A, B and C
1 / 10
Every triplet
Python
def every_triplet(nums: list[int]) -> None:
# Print every triplet
pass
every_triplet([3, 1, 4, 1, 5])

Contiguous subarrays. O(n²) time.

Given an array, generate every contiguous subarray.

This can be solved with two nested loops. The outer loop picks where the subarray starts with index start, and the inner loop picks where it ends with index end.

There are n(n+1)2\frac{n(n + 1)}{2} possible subarrays.

3
1
4
1
0123
startend
[3]
1 / 10
Every subarray
Python
def every_subarray(nums: list[int]) -> None:
# Print every contiguous subarray
pass
every_subarray([3, 1, 4, 1])

Other examples

There are also other examples, like generating every permutation, every subset or every way to choose k elements. Those are solved with the backtracking technique, which is described in chapter 4. No problem needs them before that.

Some motivation

I tried to analyse all 3000 LeetCode problems (excluding non-algorithm ones), and 73% of them can be solved with a brute force solution that generates and checks each candidate one by one. Most of the remaining 27% are straightforward easy problems that do not require any optimisation at all.

More importantly, this number should be even higher for actual interview problems, as interviewers are encouraged to ask problems that have multiple solutions and follow-ups.

So now you can approach most problems with a simple solution.

Was this lesson helpful?