Dataset Viewer
Auto-converted to Parquet Duplicate
slug
stringlengths
8
31
title
stringlengths
16
49
pattern
stringclasses
8 values
difficulty
stringclasses
3 values
statement
stringlengths
113
544
examples
listlengths
3
3
constraints
listlengths
1
4
python_function
stringlengths
10
51
python_starter
stringlengths
45
86
tests
listlengths
6
24
url
stringlengths
46
69
balanced-brackets
Balanced brackets
stack
Easy
A string s contains only the characters ( ) [ ] { }. Return true if the brackets are balanced: every opening bracket is closed by a bracket of the same type, and brackets close in the reverse order to the one in which they opened. Otherwise return false.
[ { "input": "s = \"([]{})\"", "output": "true" }, { "input": "s = \"([)]\"", "output": "false" }, { "input": "s = \"((\"", "output": "false" } ]
[ "1 ≤ s.length ≤ 10⁴", "s contains only ( ) [ ] { }" ]
is_balanced(s)
def is_balanced(s): # your code here pass
[ { "args": [ "()" ], "out": true }, { "args": [ "()[]{}" ], "out": true }, { "args": [ "(]" ], "out": false }, { "args": [ "([)]" ], "out": false }, { "args": [ "{[]}" ], "out": true }, { "args": [ ...
https://geodesicpartners.co.uk/coding-balanced-brackets
connect-ropes
Cheapest way to join ropes
heap
Medium
You have ropes of the given lengths. Joining two ropes of lengths a and b produces one rope of length a + b and costs a + b. Join ropes until one remains. Return the minimum possible total cost.
[ { "input": "ropes = [2, 4, 3]", "output": "14" }, { "input": "ropes = [1, 8, 3, 5]", "output": "30" }, { "input": "ropes = [5]", "output": "0" } ]
[ "1 ≤ n ≤ 10⁴", "1 ≤ ropes[i] ≤ 10⁴" ]
connect_ropes(ropes)
def connect_ropes(ropes): # your code here pass
[ { "args": [ [ 2, 4, 3 ] ], "out": 14 }, { "args": [ [ 1, 8, 3, 5 ] ], "out": 30 }, { "args": [ [ 5 ] ], "out": 0 }, { "args": [ [ 1, 2, ...
https://geodesicpartners.co.uk/coding-connect-ropes
top-k-frequent
The k most frequent values
arrays-hashing
Medium
Given an array nums and an integer k, return the k values that occur most often. Order them by decreasing frequency; when two values occur equally often, the smaller value comes first. k is at most the number of distinct values.
[ { "input": "nums = [1, 1, 1, 2, 2, 3], k = 2", "output": "[1, 2]" }, { "input": "nums = [4, 4, 4, 6, 6, 6, 5, 5], k = 2", "output": "[4, 6]" }, { "input": "nums = [9, 8, 7], k = 3", "output": "[7, 8, 9]" } ]
[ "1 ≤ n ≤ 10⁵", "−10⁴ ≤ nums[i] ≤ 10⁴", "1 ≤ k ≤ number of distinct values" ]
top_k_frequent(nums, k)
def top_k_frequent(nums, k): # your code here pass
[ { "args": [ [ 1, 1, 1, 2, 2, 3 ], 2 ], "out": [ 1, 2 ] }, { "args": [ [ 4, 4, 4, 6, 6, 6, 5, 5 ], 2 ], "out": [ 4, ...
https://geodesicpartners.co.uk/coding-top-k-frequent
subarray-sum-k
Count subarrays with a given sum
arrays-hashing
Medium
Given an array nums of integers, which may be negative, and an integer k, return the number of contiguous subarrays whose elements sum to exactly k. Subarrays are counted by position, so two subarrays covering different index ranges count separately even if they hold the same values.
[ { "input": "nums = [1, 1, 1], k = 2", "output": "2" }, { "input": "nums = [3, 4, 7, 2, -3, 1, 4, 2], k = 7", "output": "4" }, { "input": "nums = [1, -1, 0], k = 0", "output": "3" } ]
[ "1 ≤ n ≤ 2 × 10⁴", "−1000 ≤ nums[i] ≤ 1000", "−10⁷ ≤ k ≤ 10⁷", "Values may be negative or zero" ]
count_subarrays(nums, k)
def count_subarrays(nums, k): # your code here pass
[ { "args": [ [ 1, 1, 1 ], 2 ], "out": 2 }, { "args": [ [ 3, 4, 7, 2, -3, 1, 4, 2 ], 7 ], "out": 4 }, { "args": [ [ 1, -1, 0 ...
https://geodesicpartners.co.uk/coding-subarray-sum-k
max-non-adjacent-sum
Largest sum with no two chosen positions adjacent
dynamic-programming
Medium
Given an array nums of non-negative integers, choose a subset of positions such that no two chosen positions are adjacent, and return the largest possible sum of the chosen values. Choosing nothing is allowed, so the answer is never negative.
[ { "input": "nums = [1, 2, 3, 1]", "output": "4" }, { "input": "nums = [2, 7, 9, 3, 1]", "output": "12" }, { "input": "nums = [4, 1, 1, 4, 2, 1]", "output": "9" } ]
[ "1 ≤ n ≤ 10⁵", "0 ≤ nums[i] ≤ 10⁴" ]
max_non_adjacent(nums)
def max_non_adjacent(nums): # your code here pass
[ { "args": [ [ 1, 2, 3, 1 ] ], "out": 4 }, { "args": [ [ 2, 7, 9, 3, 1 ] ], "out": 12 }, { "args": [ [ 5 ] ], "out": 5 }, { "args": [ [ ...
https://geodesicpartners.co.uk/coding-max-non-adjacent-sum
three-sum-zero
All distinct triplets summing to zero
two-pointers
Medium
Given an integer array nums, return every triplet [a, b, c] of values from distinct positions such that a + b + c = 0. Each triplet must be sorted in non-decreasing order, no two triplets may be equal, and the list of triplets must itself be in non-decreasing order.
[ { "input": "nums = [-1, 0, 1, 2, -1, -4]", "output": "[[-1, -1, 2], [-1, 0, 1]]" }, { "input": "nums = [1, 2, 3]", "output": "[]" }, { "input": "nums = [0, 0, 0, 0]", "output": "[[0, 0, 0]]" } ]
[ "3 ≤ n ≤ 3000", "−10⁵ ≤ nums[i] ≤ 10⁵", "Positions are distinct; values need not be" ]
three_sum(nums)
def three_sum(nums): # your code here pass
[ { "args": [ [ -1, 0, 1, 2, -1, -4 ] ], "out": [ [ -1, -1, 2 ], [ -1, 0, 1 ] ] }, { "args": [ [ 0, 0, 0, 0 ] ], ...
https://geodesicpartners.co.uk/coding-three-sum-zero
count-islands
Count the islands in a grid
graphs
Easy
A grid is given as a list of equal-length strings of '1' (land) and '0' (water). Two land cells belong to the same island if they share an edge: up, down, left or right, not diagonally. Return the number of islands.
[ { "input": "grid = [\"11000\", \"11000\", \"00100\", \"00011\"]", "output": "3" }, { "input": "grid = [\"101\", \"010\", \"101\"]", "output": "5" }, { "input": "grid = [\"000\"]", "output": "0" } ]
[ "1 ≤ rows, cols ≤ 300", "Every row has the same length", "Cells are '0' or '1'" ]
num_islands(grid)
def num_islands(grid): # your code here pass
[ { "args": [ [ "11000", "11000", "00100", "00011" ] ], "out": 3 }, { "args": [ [ "101", "010", "101" ] ], "out": 5 }, { "args": [ [ "000" ] ], "out": 0 }, { "args": ...
https://geodesicpartners.co.uk/coding-count-islands
matrix-to-list
Adjacency matrix to adjacency list
graphs
Easy
A graph on n nodes, numbered 0 to n − 1, is given as an adjacency matrix: matrix[i][j] is 1 if there is an edge from i to j and 0 otherwise. The graph may be directed, so the matrix need not be symmetric. Return its adjacency list: a list of n lists, where list i holds the nodes that i has an edge to, in increasing ord...
[ { "input": "matrix = [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]]", "output": "[[1,2],[0,3],[0,3],[1,2]]" }, { "input": "matrix = [[0,1,0],[0,0,1],[1,0,0]]", "output": "[[1],[2],[0]]" }, { "input": "matrix = [[0,0],[0,0]]", "output": "[[],[]]" } ]
[ "1 ≤ n ≤ 500", "matrix is n × n", "matrix[i][j] is 0 or 1, and matrix[i][i] = 0" ]
matrix_to_list(matrix)
def matrix_to_list(matrix): # your code here pass
[ { "args": [ [ [ 0, 1, 1, 0 ], [ 1, 0, 0, 1 ], [ 1, 0, 0, 1 ], [ 0, 1, 1, 0 ] ...
https://geodesicpartners.co.uk/coding-matrix-to-list
dfs-order
Depth-first order of a graph
graphs
Easy
A graph has n nodes numbered 0 to n − 1 and a list of undirected edges, each a pair [u, v]. Traverse it depth-first under these rules, and return the nodes in the order they are first visited: Start at node 0. From a node, move to its smallest-numbered unvisited neighbour, and continue from there. When a node has no un...
[ { "input": "n = 7, edges = [[0,1],[0,2],[1,3],[2,3],[3,4],[5,6]]", "output": "[0, 1, 3, 2, 4, 5, 6]" }, { "input": "n = 3, edges = []", "output": "[0, 1, 2]" }, { "input": "n = 4, edges = [[0,3],[3,2],[2,1]]", "output": "[0, 3, 2, 1]" } ]
[ "1 ≤ n ≤ 200", "0 ≤ edges.length ≤ n(n − 1)/2", "No self-loops and no repeated edges" ]
dfs_order(n, edges)
def dfs_order(n, edges): # your code here pass
[ { "args": [ 7, [ [ 0, 1 ], [ 0, 2 ], [ 1, 3 ], [ 2, 3 ], [ 3, 4 ], [ 5, 6 ] ] ...
https://geodesicpartners.co.uk/coding-dfs-order
bfs-order
Breadth-first order of a graph
graphs
Easy
A graph has n nodes numbered 0 to n − 1 and a list of undirected edges. Traverse it breadth-first and return the nodes in the order they are taken from the queue: Start with node 0 in the queue. Repeatedly take the node at the front. Add each of its unvisited neighbours to the back of the queue, in increasing order of ...
[ { "input": "n = 7, edges = [[0,1],[0,2],[1,3],[2,3],[3,4],[5,6]]", "output": "[0, 1, 2, 3, 4, 5, 6]" }, { "input": "n = 5, edges = [[0,4],[4,3],[3,2],[2,1]]", "output": "[0, 4, 3, 2, 1]" }, { "input": "n = 4, edges = []", "output": "[0, 1, 2, 3]" } ]
[ "1 ≤ n ≤ 10⁴", "0 ≤ edges.length ≤ 2 · 10⁴", "No self-loops and no repeated edges" ]
bfs_order(n, edges)
def bfs_order(n, edges): # your code here pass
[ { "args": [ 7, [ [ 0, 1 ], [ 0, 2 ], [ 1, 3 ], [ 2, 3 ], [ 3, 4 ], [ 5, 6 ] ] ...
https://geodesicpartners.co.uk/coding-bfs-order
flood-fill
Repaint a region of an image
graphs
Easy
An image is a grid of integers, each a pixel colour. Given a starting pixel (sr, sc) and a new color, repaint the starting pixel and every pixel connected to it that has the same original colour. Two pixels are connected if they share a side (up, down, left or right), directly or through a chain of such pixels. Return ...
[ { "input": "image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2", "output": "[[2,2,2],[2,2,0],[2,0,1]]" }, { "input": "image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0", "output": "[[0,0,0],[0,0,0]]" }, { "input": "image = [[1,2,1],[2,1,2],[1,2,1]], sr = 1, sc = 1, color = 7...
[ "1 ≤ rows, cols ≤ 50", "0 ≤ image[r][c], color < 2¹⁶", "0 ≤ sr < rows, 0 ≤ sc < cols" ]
flood_fill(image, sr, sc, color)
def flood_fill(image, sr, sc, color): # your code here pass
[ { "args": [ [ [ 1, 1, 1 ], [ 1, 1, 0 ], [ 1, 0, 1 ] ], 1, 1, 2 ], "out": [ [ 2, 2, 2 ], [ ...
https://geodesicpartners.co.uk/coding-flood-fill
cycle-undirected
Does an undirected graph contain a cycle?
graphs
Easy
A graph has n nodes numbered 0 to n − 1 and a list of undirected edges. No edge joins a node to itself and no edge is repeated. Return true if the graph contains a cycle, and false otherwise. The graph need not be connected.
[ { "input": "n = 6, edges = [[0,1],[1,2],[0,3],[3,4],[4,1],[2,5]]", "output": "true" }, { "input": "n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]", "output": "false" }, { "input": "n = 6, edges = [[0,1],[2,3],[3,4],[4,2]]", "output": "true" } ]
[ "1 ≤ n ≤ 10⁵", "0 ≤ edges.length ≤ 10⁵", "No self-loops and no repeated edges" ]
has_cycle(n, edges)
def has_cycle(n, edges): # your code here pass
[ { "args": [ 6, [ [ 0, 1 ], [ 1, 2 ], [ 0, 3 ], [ 3, 4 ], [ 4, 1 ], [ 2, 5 ] ] ...
https://geodesicpartners.co.uk/coding-cycle-undirected
cycle-directed
Does a directed graph contain a cycle?
graphs
Medium
A directed graph has n nodes numbered 0 to n − 1, and each pair [u, v] in edges is an edge from u to v. No edge is repeated and none joins a node to itself. Return true if some sequence of edges leads from a node back to itself, and false otherwise.
[ { "input": "n = 6, edges = [[0,1],[0,2],[2,3],[1,3],[3,4],[4,5],[5,3]]", "output": "true" }, { "input": "n = 4, edges = [[0,1],[0,2],[1,3],[2,3]]", "output": "false" }, { "input": "n = 2, edges = [[0,1],[1,0]]", "output": "true" } ]
[ "1 ≤ n ≤ 2000", "0 ≤ edges.length ≤ 5000", "No self-loops and no repeated edges" ]
has_directed_cycle(n, edges)
def has_directed_cycle(n, edges): # your code here pass
[ { "args": [ 6, [ [ 0, 1 ], [ 0, 2 ], [ 2, 3 ], [ 1, 3 ], [ 3, 4 ], [ 4, 5 ], ...
https://geodesicpartners.co.uk/coding-cycle-directed
course-order
An order to take every course
graphs
Medium
There are n courses numbered 0 to n − 1. Each pair [a, b] in prereqs means course b must be taken before course a. One course is taken at a time. Return an order in which all n courses can be taken. Many orders may be valid: return the one that is smallest in dictionary order, that is, at every step take the smallest-n...
[ { "input": "n = 4, prereqs = [[1,0],[2,0],[3,1],[3,2]]", "output": "[0, 1, 2, 3]" }, { "input": "n = 2, prereqs = [[1,0],[0,1]]", "output": "[]" }, { "input": "n = 4, prereqs = [[0,1],[1,2],[2,3]]", "output": "[3, 2, 1, 0]" } ]
[ "1 ≤ n ≤ 2000", "0 ≤ prereqs.length ≤ 5000", "a ≠ b; no pair is repeated" ]
course_order(n, prereqs)
def course_order(n, prereqs): # your code here pass
[ { "args": [ 4, [ [ 1, 0 ], [ 2, 0 ], [ 3, 1 ], [ 3, 2 ] ] ], "out": [ 0, 1, 2, 3 ] }, { "args": [ 2,...
https://geodesicpartners.co.uk/coding-course-order
dijkstra-distances
Shortest distances from one node
graphs
Medium
A directed graph has n nodes numbered 0 to n − 1. Each triple [u, v, w] in edges is an edge from u to v with non-negative weight w; there may be several edges between the same two nodes. Return a list whose entry v is the smallest total weight of a path from src to v, or −1 if v cannot be reached. The entry for src is ...
[ { "input": "n = 3, edges = [[0,1,5]], src = 0", "output": "[0, 5, -1]" }, { "input": "n = 4, edges = [[0,1,1],[1,2,1],[0,2,5],[2,3,0]], src = 0", "output": "[0, 1, 2, 2]" }, { "input": "n = 4, edges = [[0,1,4],[0,1,1],[1,3,2]], src = 0", "output": "[0, 1, -1, 3]" } ]
[ "1 ≤ n ≤ 10⁴", "0 ≤ edges.length ≤ 5 · 10⁴", "0 ≤ w ≤ 10⁴" ]
shortest_distances(n, edges, src)
def shortest_distances(n, edges, src): # your code here pass
[ { "args": [ 6, [ [ 0, 1, 7 ], [ 1, 0, 7 ], [ 0, 2, 2 ], [ 2, 0, 2 ], [ 2, 1, 3...
https://geodesicpartners.co.uk/coding-dijkstra-distances
bellman-ford
Shortest distances with negative edges
graphs
Medium
A directed graph has n nodes numbered 0 to n − 1. Each triple [u, v, w] in edges is an edge from u to v of weight w, which may be negative. If a cycle of negative total weight can be reached from src, return an empty list. Otherwise return a list whose entry v is the smallest total weight of a path from src to v, using...
[ { "input": "n = 3, edges = [[0,1,2],[0,2,5],[2,1,-4]], src = 0", "output": "[0, 1, 5]" }, { "input": "n = 4, edges = [[0,1,1],[1,2,2],[2,3,-6],[3,1,3]], src = 0", "output": "[]" }, { "input": "n = 4, edges = [[1,2,-1],[2,1,-1],[0,3,2]], src = 0", "output": "[0, 1000000000, 1000000000...
[ "1 ≤ n ≤ 500", "0 ≤ edges.length ≤ 5000", "−10⁴ ≤ w ≤ 10⁴" ]
bellman_ford(n, edges, src)
def bellman_ford(n, edges, src): # your code here pass
[ { "args": [ 5, [ [ 3, 4, 2 ], [ 2, 3, 1 ], [ 1, 2, -2 ], [ 0, 1, 4 ], [ 0, 2, ...
https://geodesicpartners.co.uk/coding-bellman-ford
floyd-warshall
Distances between every pair of nodes
graphs
Medium
A directed graph has n nodes numbered 0 to n − 1. Each triple [u, v, w] in edges is an edge from u to v with non-negative weight w; the same pair may appear more than once. Return an n × n matrix whose entry [i][j] is the length of the shortest path from i to j, or −1 if there is none. The diagonal is 0.
[ { "input": "n = 2, edges = [[0,1,5]]", "output": "[[0,5],[-1,0]]" }, { "input": "n = 3, edges = [[0,1,9],[0,1,4],[1,2,0]]", "output": "[[0,4,4],[-1,0,0],[-1,-1,0]]" }, { "input": "n = 4, edges = [[0,1,3],[1,2,1],[0,2,7],[2,3,2],[3,0,1],[3,1,6]]", "output": "[[0,3,4,6],[4,0,1,3],[3,6,...
[ "1 ≤ n ≤ 150", "0 ≤ edges.length ≤ n(n − 1)", "0 ≤ w ≤ 10⁴" ]
all_pairs(n, edges)
def all_pairs(n, edges): # your code here pass
[ { "args": [ 4, [ [ 0, 1, 3 ], [ 1, 2, 1 ], [ 0, 2, 7 ], [ 2, 3, 2 ], [ 3, 0, 1...
https://geodesicpartners.co.uk/coding-floyd-warshall
redundant-edge
The connection that closes a loop
graphs
Medium
A tree on nodes 1 to n had one extra edge added between two of its nodes, so the n edges below contain exactly one cycle. edges[i] = [a, b] is an undirected edge with a < b. Return an edge whose removal leaves a tree on all n nodes. If several edges qualify, return the one that appears last in edges.
[ { "input": "edges = [[1,2],[1,3],[2,3]]", "output": "[2, 3]" }, { "input": "edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]", "output": "[1, 4]" }, { "input": "edges = [[1,2],[2,3],[1,3],[3,4]]", "output": "[1, 3]" } ]
[ "3 ≤ n ≤ 1000", "edges.length = n", "1 ≤ a < b ≤ n; no edge repeats", "The graph is connected" ]
redundant_edge(edges)
def redundant_edge(edges): # your code here pass
[ { "args": [ [ [ 1, 2 ], [ 1, 3 ], [ 2, 3 ] ] ], "out": [ 2, 3 ] }, { "args": [ [ [ 1, 2 ], [ 2, ...
https://geodesicpartners.co.uk/coding-redundant-edge
max-flow
Maximum flow through a network
graphs
Hard
A directed network has n nodes numbered 0 to n − 1. Each triple [u, v, c] in edges is an edge from u to v with capacity c; the same pair may appear more than once, and both [u, v, …] and [v, u, …] may be present. Return the maximum flow from source to sink: the largest total that can leave the source when no edge carri...
[ { "input": "n = 4, edges = [[0,1,1],[0,2,1],[1,2,1],[1,3,1],[2,3,1]], source = 0, sink = 3", "output": "2" }, { "input": "n = 3, edges = [[0,1,5]], source = 0, sink = 2", "output": "0" }, { "input": "n = 4, edges = [[0,1,3],[0,1,4],[1,3,5],[0,2,2],[2,3,9]], source = 0, sink = 3", "ou...
[ "2 ≤ n ≤ 100", "0 ≤ edges.length ≤ 1000", "1 ≤ c ≤ 10⁶", "source ≠ sink" ]
max_flow(n, edges, source, sink)
def max_flow(n, edges, source, sink): # your code here pass
[ { "args": [ 6, [ [ 0, 1, 10 ], [ 0, 2, 10 ], [ 1, 3, 4 ], [ 1, 2, 2 ], [ 1, 4, ...
https://geodesicpartners.co.uk/coding-max-flow
max-matching
Place as many applicants as possible
graphs
Medium
There are N applicants and M jobs. matrix[i][j] is 1 if applicant i is qualified for job j and 0 otherwise. Each job can be given to at most one applicant and each applicant can take at most one job. Return the largest number of applicants who can be given a job they are qualified for.
[ { "input": "matrix = [[1,1,0,0],[1,0,1,0],[0,1,1,0],[0,0,1,1]]", "output": "4" }, { "input": "matrix = [[1,1,1],[1,0,0],[1,0,0]]", "output": "2" }, { "input": "matrix = [[1],[1],[1]]", "output": "1" } ]
[ "1 ≤ N, M ≤ 200", "matrix[i][j] is 0 or 1" ]
max_matching(matrix)
def max_matching(matrix): # your code here pass
[ { "args": [ [ [ 1, 1, 0, 0 ], [ 1, 0, 1, 0 ], [ 0, 1, 1, 0 ], [ 0, 0, 1, 1 ] ...
https://geodesicpartners.co.uk/coding-max-matching
lis-length
Longest increasing subsequence
dynamic-programming
Medium
Given a list of integers nums, return the length of its longest strictly increasing subsequence: elements taken in their original order, not necessarily adjacent, each larger than the one before.
[ { "input": "nums = [10,9,2,5,3,7,101,18]", "output": "4" }, { "input": "nums = [7,7,7]", "output": "1" }, { "input": "nums = [0,1,0,3,2,3]", "output": "4" } ]
[ "1 ≤ nums.length ≤ 2500", "−10⁴ ≤ nums[i] ≤ 10⁴" ]
lis_length(nums)
def lis_length(nums): # your code here pass
[ { "args": [ [ 3, 1, 4, 1, 5, 9, 2, 6 ] ], "out": 4 }, { "args": [ [ 10, 9, 2, 5, 3, 7, 101, 18 ] ], "out": 4 }, { "args": [ ...
https://geodesicpartners.co.uk/coding-lis-length
lcs-length
Longest common subsequence
dynamic-programming
Medium
Given two strings text1 and text2, return the length of their longest common subsequence: a string that can be obtained from each of them by deleting some characters without reordering the rest. Return 0 if they share no character.
[ { "input": "text1 = \"abcde\", text2 = \"ace\"", "output": "3" }, { "input": "text1 = \"abc\", text2 = \"def\"", "output": "0" }, { "input": "text1 = \"ABCBDAB\", text2 = \"BDCABA\"", "output": "4" } ]
[ "1 ≤ text1.length, text2.length ≤ 1000", "Lowercase or uppercase English letters" ]
lcs_length(text1, text2)
def lcs_length(text1, text2): # your code here pass
[ { "args": [ "abcde", "ace" ], "out": 3 }, { "args": [ "abc", "abc" ], "out": 3 }, { "args": [ "abc", "def" ], "out": 0 }, { "args": [ "ABCBDAB", "BDCABA" ], "out": 4 }, { "args": [ "a", "a...
https://geodesicpartners.co.uk/coding-lcs-length
longest-common-substring
Longest common block
dynamic-programming
Medium
Given two lists of integers nums1 and nums2, return the length of the longest contiguous block of values that appears in both. (For strings this is the longest common substring.)
[ { "input": "nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]", "output": "3" }, { "input": "nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]", "output": "5" }, { "input": "nums1 = [1,2], nums2 = [3,4]", "output": "0" } ]
[ "1 ≤ nums1.length, nums2.length ≤ 1000", "0 ≤ nums[i] ≤ 100" ]
longest_common_block(nums1, nums2)
def longest_common_block(nums1, nums2): # your code here pass
[ { "args": [ [ 1, 2, 3, 2, 1 ], [ 3, 2, 1, 4, 7 ] ], "out": 3 }, { "args": [ [ 0, 0, 0, 0, 0 ], [ 0, 0, 0, ...
https://geodesicpartners.co.uk/coding-longest-common-substring
knapsack-01
The 0/1 knapsack
dynamic-programming
Medium
There are n items; item i weighs weights[i] and is worth values[i]. Choose a set of items, each at most once, whose total weight is at most capacity, and return the largest total value possible.
[ { "input": "weights = [1,3,4,5], values = [1,4,5,7], capacity = 7", "output": "9" }, { "input": "weights = [5,4,6,3], values = [10,40,30,50], capacity = 10", "output": "90" }, { "input": "weights = [10], values = [5], capacity = 9", "output": "0" } ]
[ "1 ≤ n ≤ 100", "1 ≤ weights[i] ≤ 1000", "1 ≤ values[i] ≤ 10⁴", "0 ≤ capacity ≤ 10⁴" ]
knapsack_01(weights, values, capacity)
def knapsack_01(weights, values, capacity): # your code here pass
[ { "args": [ [ 1, 3, 4, 5 ], [ 1, 4, 5, 7 ], 7 ], "out": 9 }, { "args": [ [ 10 ], [ 5 ], 9 ], "out": 0 }, { "args": [ [ 2, ...
https://geodesicpartners.co.uk/coding-knapsack-01
knapsack-bounded
The bounded knapsack
dynamic-programming
Medium
Item i weighs weights[i], is worth values[i], and has counts[i] identical copies. Choose how many copies of each to take (between 0 and its count) so that the total weight is at most capacity, and return the largest total value.
[ { "input": "weights = [2,3], values = [3,4], counts = [2,1], capacity = 7", "output": "10" }, { "input": "weights = [1,2,3], values = [10,15,40], counts = [2,1,1], capacity = 6", "output": "65" }, { "input": "weights = [4], values = [5], counts = [3], capacity = 9", "output": "10" ...
[ "1 ≤ n ≤ 100", "1 ≤ weights[i] ≤ 1000", "1 ≤ counts[i] ≤ 1000", "0 ≤ capacity ≤ 10⁴" ]
knapsack_bounded(weights, values, counts, capacity)
def knapsack_bounded(weights, values, counts, capacity): # your code here pass
[ { "args": [ [ 2, 3 ], [ 3, 4 ], [ 2, 1 ], 7 ], "out": 10 }, { "args": [ [ 1, 2, 3 ], [ 10, 15, 40 ], [ 2, 1, ...
https://geodesicpartners.co.uk/coding-knapsack-bounded
knapsack-unbounded
The unbounded knapsack
dynamic-programming
Medium
There are n item types; type i weighs weights[i] and is worth values[i], and you may take any number of each. Return the largest total value whose total weight is at most capacity.
[ { "input": "weights = [2,3,5], values = [3,5,9], capacity = 9", "output": "15" }, { "input": "weights = [1,3,4], values = [15,50,60], capacity = 8", "output": "130" }, { "input": "weights = [3], values = [5], capacity = 2", "output": "0" } ]
[ "1 ≤ n ≤ 100", "1 ≤ weights[i] ≤ 1000", "1 ≤ values[i] ≤ 10⁴", "0 ≤ capacity ≤ 10⁴" ]
knapsack_unbounded(weights, values, capacity)
def knapsack_unbounded(weights, values, capacity): # your code here pass
[ { "args": [ [ 2, 3, 5 ], [ 3, 5, 9 ], 9 ], "out": 15 }, { "args": [ [ 1, 50 ], [ 1, 30 ], 100 ], "out": 100 }, { "args": [ [ 3...
https://geodesicpartners.co.uk/coding-knapsack-unbounded
coin-change-ways
Ways to make an amount
dynamic-programming
Medium
Given an amount and a list of distinct coin values coins, with an unlimited supply of each, return the number of combinations of coins that add up to the amount. Combinations that differ only in order count once. If the amount is 0, there is one way: no coins.
[ { "input": "amount = 5, coins = [1,2,5]", "output": "4" }, { "input": "amount = 3, coins = [2]", "output": "0" }, { "input": "amount = 0, coins = [7]", "output": "1" } ]
[ "1 ≤ coins.length ≤ 300", "1 ≤ coins[i] ≤ 5000, all distinct", "0 ≤ amount ≤ 5000", "The answer fits in a 64-bit integer" ]
count_ways(amount, coins)
def count_ways(amount, coins): # your code here pass
[ { "args": [ 5, [ 1, 2, 5 ] ], "out": 4 }, { "args": [ 3, [ 2 ] ], "out": 0 }, { "args": [ 10, [ 10 ] ], "out": 1 }, { "args": [ 0, [ 7 ] ], ...
https://geodesicpartners.co.uk/coding-coin-change-ways
longest-palindromic-subsequence
Longest palindromic subsequence
dynamic-programming
Medium
Given a string s, return the length of its longest palindromic subsequence: characters taken in order, not necessarily adjacent, that read the same forwards and backwards.
[ { "input": "s = \"bbbab\"", "output": "4" }, { "input": "s = \"cbbd\"", "output": "2" }, { "input": "s = \"agbdba\"", "output": "5" } ]
[ "1 ≤ s.length ≤ 1000", "Lowercase English letters" ]
longest_pal_subseq(s)
def longest_pal_subseq(s): # your code here pass
[ { "args": [ "bbbab" ], "out": 4 }, { "args": [ "cbbd" ], "out": 2 }, { "args": [ "a" ], "out": 1 }, { "args": [ "abcd" ], "out": 1 }, { "args": [ "agbdba" ], "out": 5 }, { "args": [ "racecar" ...
https://geodesicpartners.co.uk/coding-longest-palindromic-subsequence
longest-palindromic-substring
Longest palindromic substring
dynamic-programming
Medium
Given a string s, return its longest substring (contiguous) that reads the same forwards and backwards. If several have the maximum length, return the one that starts earliest.
[ { "input": "s = \"babad\"", "output": "\"bab\"" }, { "input": "s = \"cbbd\"", "output": "\"bb\"" }, { "input": "s = \"ac\"", "output": "\"a\"" } ]
[ "1 ≤ s.length ≤ 1000", "Letters and digits" ]
longest_palindrome(s)
def longest_palindrome(s): # your code here pass
[ { "args": [ "babad" ], "out": "bab" }, { "args": [ "cbbd" ], "out": "bb" }, { "args": [ "a" ], "out": "a" }, { "args": [ "ac" ], "out": "a" }, { "args": [ "forgeeksskeegfor" ], "out": "geeksskeeg" }, { ...
https://geodesicpartners.co.uk/coding-longest-palindromic-substring
palindrome-partition-min
The fewest cuts into palindromes
dynamic-programming
Hard
Given a string s, cut it into pieces so that every piece is a palindrome. Return the smallest number of cuts needed. A string that is already a palindrome needs 0 cuts.
[ { "input": "s = \"aab\"", "output": "1" }, { "input": "s = \"aabcbd\"", "output": "2" }, { "input": "s = \"racecar\"", "output": "0" } ]
[ "1 ≤ s.length ≤ 2000", "Lowercase English letters" ]
min_cut(s)
def min_cut(s): # your code here pass
[ { "args": [ "aab" ], "out": 1 }, { "args": [ "a" ], "out": 0 }, { "args": [ "ab" ], "out": 1 }, { "args": [ "aabcbd" ], "out": 2 }, { "args": [ "racecar" ], "out": 0 }, { "args": [ "abcde" ], ...
https://geodesicpartners.co.uk/coding-palindrome-partition-min
matrix-chain
The cheapest order to multiply matrices
dynamic-programming
Hard
A chain of matrices is described by dims: matrix i has dims[i] rows and dims[i + 1] columns, so there are dims.length − 1 matrices. Multiplying a p × q matrix by a q × r matrix costs p · q · r scalar multiplications. Return the minimum total cost of computing the product of the whole chain, over all ways of placing the...
[ { "input": "dims = [10,30,5,60]", "output": "4500" }, { "input": "dims = [40,20,30,10,30]", "output": "26000" }, { "input": "dims = [10,20]", "output": "0" } ]
[ "2 ≤ dims.length ≤ 200", "1 ≤ dims[i] ≤ 500" ]
matrix_chain(dims)
def matrix_chain(dims): # your code here pass
[ { "args": [ [ 10, 30, 5, 60 ] ], "out": 4500 }, { "args": [ [ 40, 20, 30, 10, 30 ] ], "out": 26000 }, { "args": [ [ 1, 2, 3, 4, 3 ] ...
https://geodesicpartners.co.uk/coding-matrix-chain
binary-search-index
Find a value in a sorted array
binary-search
Easy
Given an array nums of distinct integers sorted in increasing order, and an integer target, return the index of target in nums, or −1 if it is not there. The search must take O(log n) comparisons.
[ { "input": "nums = [-4, 0, 3, 8, 12, 19], target = 8", "output": "3" }, { "input": "nums = [-4, 0, 3, 8, 12, 19], target = 5", "output": "-1" }, { "input": "nums = [7], target = 7", "output": "0" } ]
[ "1 ≤ n ≤ 10⁵", "−10⁹ ≤ nums[i], target ≤ 10⁹", "nums is strictly increasing" ]
search(nums, target)
def search(nums, target): # your code here pass
[ { "args": [ [ -4, 0, 3, 8, 12, 19 ], 8 ], "out": 3 }, { "args": [ [ -4, 0, 3, 8, 12, 19 ], 5 ], "out": -1 }, { "args": [ [ 7 ], ...
https://geodesicpartners.co.uk/coding-binary-search-index
search-insert
Where a value belongs in a sorted array
binary-search
Easy
Given a sorted array nums of distinct integers and an integer target, return the index of target if it is present. Otherwise return the index where it would have to be inserted to keep the array sorted. Use O(log n) comparisons.
[ { "input": "nums = [1, 3, 5, 6], target = 5", "output": "2" }, { "input": "nums = [1, 3, 5, 6], target = 2", "output": "1" }, { "input": "nums = [1, 3, 5, 6], target = 7", "output": "4" } ]
[ "1 ≤ n ≤ 10⁴", "−10⁴ ≤ nums[i], target ≤ 10⁴", "nums is strictly increasing" ]
search_insert(nums, target)
def search_insert(nums, target): # your code here pass
[ { "args": [ [ 1, 3, 5, 6 ], 5 ], "out": 2 }, { "args": [ [ 1, 3, 5, 6 ], 2 ], "out": 1 }, { "args": [ [ 1, 3, 5, 6 ], 7 ], ...
https://geodesicpartners.co.uk/coding-search-insert
integer-sqrt
Integer square root without a library
binary-search
Easy
Given a non-negative integer x, return ⌊√x⌋, the largest integer whose square is at most x. Do not use a square-root function or exponentiation from a library.
[ { "input": "x = 8", "output": "2" }, { "input": "x = 16", "output": "4" }, { "input": "x = 2147483647", "output": "46340" } ]
[ "0 ≤ x ≤ 2³¹ − 1" ]
my_sqrt(x)
def my_sqrt(x): # your code here pass
[ { "args": [ 0 ], "out": 0 }, { "args": [ 1 ], "out": 1 }, { "args": [ 2 ], "out": 1 }, { "args": [ 3 ], "out": 1 }, { "args": [ 4 ], "out": 2 }, { "args": [ 8 ], "out": 2 }, { "arg...
https://geodesicpartners.co.uk/coding-integer-sqrt
max-sum-k-window
Best total over k consecutive days
sliding-window
Easy
nums[i] is the profit on day i, and may be negative. Return the largest total profit over any k consecutive days.
[ { "input": "nums = [2,1,5,1,3,2], k = 3", "output": "9" }, { "input": "nums = [2,3,4,1,5], k = 2", "output": "7" }, { "input": "nums = [-3,-1,-4], k = 2", "output": "-4" } ]
[ "1 ≤ k ≤ n ≤ 10⁵", "−10⁴ ≤ nums[i] ≤ 10⁴" ]
max_sum_k(nums, k)
def max_sum_k(nums, k): # your code here pass
[ { "args": [ [ 2, 1, 5, 1, 3, 2 ], 3 ], "out": 9 }, { "args": [ [ 2, 3, 4, 1, 5 ], 2 ], "out": 7 }, { "args": [ [ -3, -1, -4 ...
https://geodesicpartners.co.uk/coding-max-sum-k-window
longest-k-distinct
Longest run with at most k distinct letters
sliding-window
Medium
Given a string s and an integer k, return the length of the longest substring that contains at most k distinct characters.
[ { "input": "s = \"eceba\", k = 2", "output": "3" }, { "input": "s = \"aa\", k = 1", "output": "2" }, { "input": "s = \"abc\", k = 0", "output": "0" } ]
[ "0 ≤ |s| ≤ 5 × 10⁴", "0 ≤ k ≤ 50" ]
longest_k_distinct(s, k)
def longest_k_distinct(s, k): # your code here pass
[ { "args": [ "eceba", 2 ], "out": 3 }, { "args": [ "aa", 1 ], "out": 2 }, { "args": [ "abc", 0 ], "out": 0 }, { "args": [ "", 3 ], "out": 0 }, { "args": [ "abaccc", 2 ], "out": 4 ...
https://geodesicpartners.co.uk/coding-longest-k-distinct
shortest-subarray-sum
Shortest subarray with a sum at least the target
sliding-window
Medium
Given an array nums of positive integers and an integer target, return the length of the shortest contiguous subarray whose sum is at least target. If no such subarray exists, return 0.
[ { "input": "nums = [2, 3, 1, 2, 4, 3], target = 7", "output": "2" }, { "input": "nums = [1, 1, 1, 1, 1, 1, 1, 1], target = 11", "output": "0" }, { "input": "nums = [1, 4, 4], target = 4", "output": "1" } ]
[ "1 ≤ n ≤ 10⁵", "1 ≤ nums[i] ≤ 10⁴", "1 ≤ target ≤ 10⁹", "Every value is strictly positive" ]
shortest_subarray(nums, target)
def shortest_subarray(nums, target): # your code here pass
[ { "args": [ [ 2, 3, 1, 2, 4, 3 ], 7 ], "out": 2 }, { "args": [ [ 1, 4, 4 ], 4 ], "out": 1 }, { "args": [ [ 1, 1, 1, 1, 1, ...
https://geodesicpartners.co.uk/coding-shortest-subarray-sum
count-product-below
Count runs whose product is below k
sliding-window
Medium
Given an array nums of positive integers and an integer k, return the number of contiguous subarrays whose product is strictly less than k.
[ { "input": "nums = [10,5,2,6], k = 100", "output": "8" }, { "input": "nums = [1,2,3], k = 0", "output": "0" }, { "input": "nums = [1,1,1], k = 2", "output": "6" } ]
[ "1 ≤ n ≤ 3 × 10⁴", "1 ≤ nums[i] ≤ 1000", "0 ≤ k ≤ 10⁶" ]
count_product_below(nums, k)
def count_product_below(nums, k): # your code here pass
[ { "args": [ [ 10, 5, 2, 6 ], 100 ], "out": 8 }, { "args": [ [ 1, 2, 3 ], 0 ], "out": 0 }, { "args": [ [ 1, 1, 1 ], 2 ], "out": 6 }, { ...
https://geodesicpartners.co.uk/coding-count-product-below
window-maximum
Maximum of every window
sliding-window
Hard
Given an array nums and a window size k, return the maximum of every contiguous window of length k, from left to right.
[ { "input": "nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3", "output": "[3, 3, 5, 5, 6, 7]" }, { "input": "nums = [9, 8, 7, 6], k = 2", "output": "[9, 8, 7]" }, { "input": "nums = [4, 2, 12, 3], k = 1", "output": "[4, 2, 12, 3]" } ]
[ "1 ≤ k ≤ n ≤ 10⁵", "−10⁴ ≤ nums[i] ≤ 10⁴" ]
window_maximum(nums, k)
def window_maximum(nums, k): # your code here pass
[ { "args": [ [ 1, 3, -1, -3, 5, 3, 6, 7 ], 3 ], "out": [ 3, 3, 5, 5, 6, 7 ] }, { "args": [ [ 1 ], 1 ], "out": [ 1 ] }, { "a...
https://geodesicpartners.co.uk/coding-window-maximum

Coding interview problems with test cases

I put these 39 algorithm problems together for people preparing for the coding round of a software engineering or quant developer interview, and I've kept them close to what you'll actually be asked. Fourteen of them are graph problems and twelve are dynamic programming, while the other thirteen come from sliding window, binary search and a few smaller patterns like a stack or a heap. I'd call 11 of them easy, 24 medium and 4 hard, though you may well disagree once you've tried a couple.

Each row has the problem statement in plain text with its worked examples and constraints, a Python starter, and the test cases I use to mark answers on the site, which come to 574 across the whole set. I've checked every problem's solutions against a separate reference solution on those same tests, so if you want a small code-generation benchmark you can call a candidate function on each test's args and compare whatever comes back with its out.

I haven't included the solutions, because the point is that you write them yourself. Every row has a url for its problem page, and there you can write your answer in Python, C++, Java, JavaScript, Rust or Haskell and run it against the same tests, with hints on the page if you get stuck halfway.

What a row looks like

{"slug": "count-islands", "title": "...", "pattern": "graphs", "difficulty": "Easy",
 "statement": "...", "examples": [{"input": "...", "output": "..."}], "constraints": ["..."],
 "python_function": "...", "python_starter": "...",
 "tests": [{"args": [...], "out": ...}], "url": "https://geodesicpartners.co.uk/coding-count-islands"}

You'll get each graph as a grid, an edge list or an adjacency list depending on the problem, and the statement tells you which one to expect. None of the outputs is a floating point number, so a plain equality check is all you need when you score a run.

If you'd like the lessons that go with these, the whole course is sorted by pattern at https://geodesicpartners.co.uk/coding-interview-course

Downloads last month
27