50 Coding Interview Questions
50 Coding Interview Questions
And that’s when I realized what I was missing: Really solid practice.
When I was doing actual interviews over and over again, I found that it started to get easier. I
wasn’t as nervous. I was comfortable in the interviews.
After landing multiple great job offers at companies like Amazon and Yext, I knew that I wanted
to help others accomplish what I had just done. I started Byte by Byte to do just that.
Since then, I’ve helped thousands of students get jobs at FAANG companies (Facebook,
Amazon, Apple, Netflix, Google) and many others. I’ve helped bootcamp graduates finally break
into tech. I’ve helped 10-year industry veterans move into that next position.
Thank you so much for joining me on this journey. I can’t wait to help you find your dream job in
tech.
Best,
But here’s the thing. It’s not going to be enough for you just to read through the whole thing and
head off to your interview. If you really want to get the most out of this guide, you’re going to
have to put in the work.
The key when you study these problems is to solve them exactly as you will when you go into
your interview. Developing a systematic approach and practicing it will allow you not only to
solve these problems, but solve so many others in your interview.
Understanding exactly what is being asked of you is critical to your success. Ask any
clarifying questions necessary (you can also ask later so you don’t have to figure out all
your questions now). Also consider the example you’re given. Look at the input and
figure out how that resulted in the given output. If you see any obvious problem areas or
edge cases, add in your own example. Based on the input and output, what should be
the signature of your function?
Finding a brute force solution also guarantees that you find a solution. When searching
for a brute force solution, there are a couple of things you can try. If a question asks for
the most something (longest path, largest weight, etc), you can solve it by finding every
possible solution and comparing them.
If you’re unsure how to approach a problem, try solving it by hand first and then
There are plenty of approaches you can try here, including brainstorming more efficient
data structures, looking at duplicated or unnecessary work that you’re performing, or
just looking for more efficient solutions unrelated to your brute force solution. The key
is, though, to not spend too much time here before simply selecting the best solution
you have and starting to code it up.
The key to testing your code is to actually go through the code line by line. Too many
people simply talk through the logic. This is a good first step, but it guarantees that you
miss small typos or indexing errors. Missing these could leave a bad taste in your
interviewer’s mouth even if you did well otherwise. Therefore you should pick a simple
example and go through the code in its entirety.
With a defined process at your side, there is no reason to be nervous in an interview. Even if you
see a problem that confuses the hell out of you, you know where to begin and can simply take it
one step at a time.
1. Median of Arrays
arr1 = [1, 3, 5]
arr2 = [2, 4, 6]
median(arr1, arr2) = 3.5
● Answer: https://www.byte-by-byte.com/median/
2. 0-1 Knapsack
● Question: Given a list of items with values and weights, as well as a max weight,
find the maximum value you can generate from items where the sum of the
weights is less than the max.
● Eg.
knapsack(items, maxWeight) = 22
● Answer: https://www.byte-by-byte.com/01knapsack/
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[‑1, 2, 3]
[4, 5, ‑6]
[7, 8, 9]
● Answer: https://www.byte-by-byte.com/matrixproduct/
● Question: Given an array of integers where each value 1 <= x <= len(array), write
a function that finds all the duplicates in the array.
● Eg.
dups([1, 2, 3]) = []
dups([1, 2, 2]) = [2]
● Answer: https://www.byte-by-byte.com/findduplicates/
5. Consecutive Array
● Question: Given an unsorted array, find the length of the longest sequence of
consecutive numbers in the array.
● eg.
● Answer: https://www.byte-by-byte.com/consecutivearray/
● Answer: https://www.byte-by-byte.com/zeromatrix/
7. Square Submatrix
● Question: Given a 2D array of 1s and 0s, find the largest square subarray of all
1s.
● Eg. Given a 2D array of 1s and 0s, find the largest square subarray of all 1s.
● Answer: https://www.byte-by-byte.com/squaresubmatrix/
● Question: Given k sorted arrays, merge them into a single sorted array.
● Eg.
● Answer: https://www.byte-by-byte.com/mergekarrays/
9. Matrix Search
● Question: Given an n x m array where all rows and columns are in sorted order,
contains([1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]) = True
Answer: https://www.byte-by-byte.com/matrixsearch/
A = {1,3,5,0,0,0}
B = {2,4,6}
mergeArrays(A, B)
A = {1,2,3,4,5,6}
● Answer: https://www.byte-by-byte.com/mergearrays/
● Answer: https://www.byte-by-byte.com/zerosum/
● Eg.
permutations({1, 2, 3})
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
● Answer: https://www.byte-by-byte.com/permutations/
● Question: Implement N > 0 stacks using a single array to store all stack data
(you may use auxiliary arrays in your stack object, but all of the objects in all of
the stacks must be in the same array). No stack should be full unless the entire
array is full.
● Eg.
N = 3;
capacity = 10;
Stacks stacks = new Stacks(N, capacity);
stacks.put(0, 10);
stacks.put(2, 11);
stacks.pop(0) = 10;
stacks.pop(2) = 11;
● Answer: https://www.byte-by-byte.com/nstacks/
● Answer: https://www.byte-by-byte.com/anagrams/
0:
1: 0
2: 0
3: 1, 2
4: 3
output: 0, 1, 2, 3, 4
● Answer: https://www.byte-by-byte.com/buildorder/
● Question: Given a directed graph, find the shortest path between two nodes if
one exists.
● Eg.
Directed graph:
● Answer: https://www.byte-by-byte.com/shortestpath/
a random node.
● Eg.
getRandomNode() = 5
getRandomNode() = 8
getRandomNode() = 1
● Answer: https://www.byte-by-byte.com/randombinarytree/
● Question: Given two nodes in a binary tree, write a function to find the lowest
common ancestor.
● Eg.
lcs(4, 3) = 1
lcs(6, 7) = 3
● Answer: https://www.byte-by-byte.com/lowestcommonancestor/
● Question: Given two integers, write a function to sum the numbers without using
any arithmetic operators.
● Answer: https://www.byte-by-byte.com/sum/
● Question: Given a stack, reverse the items without creating any additional data
structures.
● Eg.
reverse(1‑>2‑>3) = 3‑>2‑>1
● Answer: https://www.byte-by-byte.com/reversestack/
● Question: Given a tree, write a function to convert it into a circular doubly linked
● Answer: https://www.byte-by-byte.com/treetolist/
● Question: Given a tree, write a function to find the length of the longest branch of
nodes in increasing consecutive order.
● Eg.
length = 3
● Answer: https://www.byte-by-byte.com/longestbranch/
● Question: Given a linked list, write a function that prints the nodes of the list in
reverse order.
● Eg.
Answer: https://www.byte-by-byte.com/printreversedlist/
● Question: Given a binary tree, write a function to determine whether the tree is
balanced.
● Eg.
● Answer: https://www.byte-by-byte.com/balancedtree/
● Question: Given a binary tree, write a function to test if the tree is a binary search
tree.
● Eg.
Valid:
Invalid:
● Answer: https://www.byte-by-byte.com/binarysearchtree/
change(1) = 1
change(3) = 3
change(7) = 3
change(32) = 4
● Answer: https://www.byte-by-byte.com/smallestchange/
● Question: Given a binary search tree, print out the elements of the tree in order
without using recursion.
● Eg.
● Answer: https://www.byte-by-byte.com/inordertraversal/
● Question: Given a stack, sort the elements in the stack using one additional
stack.
● Eg.
● Answer: https://www.byte-by-byte.com/sortstacks/
● Question: Implement a LIFO stack with basic functionality (push and pop) using
FIFO queues to store the data.
● Answer: https://www.byte-by-byte.com/stackfromqueues/
● Question: Given a linked list, write a function to determine whether the list is a
palindrome.
● Eg.
● Answer: https://www.byte-by-byte.com/palindromes/
● Question: Implement a LIFO stack that has a push(), pop(), and max() function,
where max() returns the maximum value in the stack. All of these functions
should run in O(1) time.
● Eg.
push(1)
max() = 1
push(2)
max() = 2
push(1)
max() = 2
pop() = 1
max() = 2
pop() = 2
max() = 1
● Answer: https://www.byte-by-byte.com/maxstack/
● Question: Given an array containing all the numbers from 1 to n except two, find
the two missing numbers.
● Eg.
missing([4, 2, 3]) = 1, 5
● Answer: https://www.byte-by-byte.com/twomissingnumbers/
● Question: Given a list of bytes a, each representing one byte of a larger integer
(ie. {0x12, 0x34, 0x56, 0x78}represents the integer 0x12345678), and an integer
b, find a % b.
● Eg.
● Answer: https://www.byte-by-byte.com/bigintmod/
● Question: Given two integers, write a function that swaps them without using
any temporary variables.
● Answer: https://www.byte-by-byte.com/swapvariables/
● Question: Given two integers, write a function to determine whether or not their
binary representations differ by a single bit.
● Eg.
gray(0, 1) = true
gray(1, 2) = false
● Answer: https://www.byte-by-byte.com/graycode/
● Question: Given a number, write a function to rotate the bits (ie circular shift).
● Eg.
rotate(0xFFFF0000, 8) = 0x00FFFF00
rotate(0b10110011100011110000111110000000, 17) =
0b00011111000000010110011100011110
● Answer: https://www.byte-by-byte.com/rotatebits/
^ |
|_________|
● Answer: https://www.byte-by-byte.com/listcycles/
● Question: Given a linked list where each node has two pointers, one to the next
node and one to a random node in the list, clone the linked list.
● Eg.
| | | |
v v v v
3 1 3 2
Answer: https://www.byte-by-byte.com/randomlinkedlist/
● Question: Given an unsorted linked list, write a function to remove all the
duplicates.
● eg.
● Answer: https://www.byte-by-byte.com/deduplinkedlist/
● Question: Given a linked list, write a function to split the list into two equal
halves.
● Eg.
● Answer: https://www.byte-by-byte.com/splitlinkedlist/
● Question: Given a linked list, and an input n, write a function that returns the
nth-to-last element of the linked list.
● Eg. Given a linked list, and an input n, write a function that returns the nth-to-last
element of the linked list.
● Answer: https://www.byte-by-byte.com/nthtolastelement/
● Question: Given a list of integers, write a function that returns all sets of 3
numbers in the list, a, b, and c, so that a + b + c == 0.
● Eg.
[‑1, ‑1, 2]
[‑1, 0, 1]
● Answer: https://www.byte-by-byte.com/threesum/
● Question: Given a tree, write a function that prints out the nodes of the tree in
level order.
● Eg.
traverse(tree) = 1 2 3 4 5 6 7
● Answer: https://www.byte-by-byte.com/treelevelorder/
45. Autocomplete
● Question: Write an autocomplete class that returns all dictionary words with a
given prefix.
● Eg.
● Answer: https://www.byte-by-byte.com/autocomplete/
query: “abc”
output: 2
● Answer: https://www.byte-by-byte.com/stringdeletion/
● Question: Given two strings, write a function that returns the longest common
substring.
● Eg.
● Answer: https://www.byte-by-byte.com/longestsubstring/
repetitions. If the compressed string is longer than the original, you should return
the original string.
● Eg.
compress(“a”) = "a"
compress(“aaa”) = "a3"
compress(“aaabbb”) = "a3b3"
compress(“aaabccc”) = "a3b1c3"
● Answer: https://www.byte-by-byte.com/stringcompression/
number.
● Eg.
fibonacci(1) = 1
fibonacci(5) = 5
fibonacci(10) = 55
● Answer: https://www.byte-by-byte.com/fibonacci/
● Question: Given a list of strings, write a function to get the kth most frequently
occurring string.
● Eg.
kthMostFrequent({"a","b","c","a","b","a"}, 0) = "a"
kthMostFrequent({"a","b","c","a","b","a"}, 1) = "b"
kthMostFrequent({"a","b","c","a","b","a"}, 2) = "c"
kthMostFrequent({"a","b","c","a","b","a"}, 3) = null
● Answer: https://www.byte-by-byte.com/kthmostfrequentstring/