Profile

Unblock all features

Sign in to track progress and visualise your code.

ntree.ai
Helper data structures and core patterns

Core patterns

The patterns below are used a lot in coding interview problems.

This lesson shows the main idea of each one on a simple example. In real problems, the same patterns are often combined or used in less obvious ways, so the best way to learn them is to practise them on problems.

Two pointers. O(n) time.

Two pointers traverse an array with one loop and two indexes instead of one, so elements from different places are handled together in a single pass instead of two nested loops.

Example

Given an array, reverse it in place.

Start the left index at the first element and the right index at the last one. Swap their elements, then move left to the right and right to the left, until they meet.

1
2
3
4
5
6
012345
left right
swap 1 and 6
1 / 4
Reverse an array
Python
def reverse(nums: list[int]) -> list[int]:
# Reverse nums in place and return it
return nums
print(reverse([1, 2, 3, 4, 5, 6]))

Binary search. O(log n) time.

Binary search checks a condition on the middle element and decides which half can hold the answer, so the other half is cut off. Halving the range at every step takes O(log⁡n)O(\log n) steps.

Example

Given a sorted array and a target, find the index of the target.

Compare the middle element of the range with the target. If it is less than the target, the target can only be in the right half, and if it is greater, only in the left half.

target = 13
1
3
5
7
9
11
13
15
01234567
left right
mid
nums[3] = 7 < 13, left = 4
1 / 3
Binary search
Python
def binary_search(nums: list[int], target: int) -> int:
# Return the index of target, or -1 if it is not there
return -1
nums = [1, 3, 5, 7, 9, 11, 13, 15]
print(binary_search(nums, 13))

Prefix sum. O(n) time.

A prefix sum is the sum of the elements before an index. Keeping it for every index gives the sum of any part of the array with one subtraction.

Example

Given an array, find the index where the sum of the elements on its left equals the sum of the elements on its right.

Keep the sum of the elements before the current index as left, adding the current element after each step. The sum on the right is then total - left - nums[i].

total = 28
1
7
3
6
5
6
012345
i
i = 0: left = 0, right = 28 - 0 - 1 = 27
1 / 4
Find the index
Python
def find_index(nums: list[int]) -> int:
# Return the index where the sum on its left equals the sum on its right, or -1
return -1
print(find_index([1, 7, 3, 6, 5, 6]))

Sliding window. O(n) time.

A sliding window is a range of consecutive elements that moves through the array.

Instead of computing the whole range again after every move, the result is updated with the element that enters and the element that leaves.

Example

Given an array and a number k, find the sum of every k consecutive elements.

Sum the first kk elements, then slide the window one element at a time, adding the element that enters and subtracting the element that leaves.

k = 3
2
1
5
1
3
2
012345
sum = 2 + 1 + 5 = 8
1 / 4
Sum of every window
Python
def window_sums(nums: list[int], k: int) -> list[int]:
# Return the sum of every k consecutive elements
return []
print(window_sums([2, 1, 5, 1, 3, 2], 3))
Was this lesson helpful?