50 Coding Interview Questions V2
50 Coding Interview Questions V2
Table of Contents
Introduction
Hey there! Sam here from B
yte by Byte.
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,
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 3
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.
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 4
3. Optimize the brute force solution
Here is where you have the chance to shine. You shouldn’t spend more than 5-10
minutes on this step before moving on to coding, but this is your chance to make your
algorithm as efficient as you can.
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.
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.
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 5
1. Median of Arrays
Question: Find the median of two sorted arrays.
eg.
Solution: 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.
Solution: https://www.byte-by-byte.com/01knapsack/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 6
3. Matrix product
Question: Given a matrix, find the path from top left to bottom right with the greatest
product by moving only down and right.
eg.
[1, 2
, 3 ]
[4, 5 , 6 ]
[7, 8 , 9 ]
Solution: https://www.byte-by-byte.com/matrixproduct/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 7
4. Find Duplicates
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.
Solution: 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.
consecutive([4, 2
, 1 , 6 , 5]) = 3 , [4, 5, 6]
consecutive([5, 5 , 3 , 1 ]) = 1, [1], [3], or [5]
Solution: https://www.byte-by-byte.com/consecutivearray/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 8
6. Zero Matrix
Question: Given a boolean matrix, update it so that if any cell is true, all the cells in that
row and column are true.
eg.
Solution: 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.
subarray([1, 1
, 1 , 0 ]
[1, 1 , 1 , 1 ]
[1, 1 , 0 , 0 ]) = 2
Solution: https://www.byte-by-byte.com/squaresubmatrix/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 9
8. Merge K Arrays
Question: Given k sorted arrays, merge them into a single sorted array.
eg.
merge({{1, 4, 7},{2, 5, 8},{3, 6, 9}}) = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Solution: 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, write a
function to determine whether the array contains an element x.
eg.
Solution: https://www.byte-by-byte.com/matrixsearch/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 10
10. Merge Arrays
Question: Given 2 sorted arrays, A and B, where A is long enough to hold the contents of
A and B, write a function to copy the contents of B into A without using any buffer or
additional memory.
eg.
A = {1,3,5,0,0,0}
B = {2,4,6}
mergeArrays(A, B)
A = {1,2,3,4,5,6}
Solution: https://www.byte-by-byte.com/mergearrays/
eg.
zeroSum({1, 2, -5, 1, 2, -1}) = [2, -5, 1, 2]
Solution: https://www.byte-by-byte.com/zerosum/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 11
12. Permutations
Question: Write a function that returns all permutations of a given list.
eg.
Solution: https://www.byte-by-byte.com/permutations/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 12
13. N Stacks
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;
Solution: https://www.byte-by-byte.com/nstacks/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 13
14. Anagrams
Question: Given two strings, write a function to determine whether they are anagrams.
eg.
Solution: https://www.byte-by-byte.com/anagrams/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 14
15. Build Order
Question: Given a list of packages that need to be built and the dependencies for each
package, determine a valid order in which to build the packages.
eg.
0:
1: 0
2: 0
3: 1, 2
4: 3
output: 0, 1, 2, 3, 4
Solution: https://www.byte-by-byte.com/buildorder/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 15
16. Shortest Path
Question: Given a directed graph, find the shortest path between two nodes if one
exists.
eg.
Solution: https://www.byte-by-byte.com/shortestpath/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 16
17. Random Binary Tree
Question: Implement a binary tree with a method getRandomNode() that returns a
random node.
eg.
getRandomNode() = 5
getRandomNode() = 8
getRandomNode() = 1
Solution: https://www.byte-by-byte.com/randombinarytree/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 17
18. Lowest Common Ancestor
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
Solution: https://www.byte-by-byte.com/lowestcommonancestor/
19. Sum
Question: Given two integers, write a function to sum the numbers without using any
arithmetic operators.
Solution: https://www.byte-by-byte.com/sum/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 18
eg.
reverse(1->2->3) = 3->2->1
Solution: https://www.byte-by-byte.com/reversestack/
eg.
<- 4 <-> 2 <-> 5 <-> 1 <-> 6 <-> 3 <-> 7 ->
Solution: https://www.byte-by-byte.com/treetolist/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 19
eg.
length = 3
Solution: https://www.byte-by-byte.com/longestbranch/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 20
23. Print Reversed Linked List
Question: Given a linked list, write a function that prints the nodes of the list in reverse
order.
eg.
Solution: https://www.byte-by-byte.com/printreversedlist/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 21
eg.
Solution: https://www.byte-by-byte.com/balancedtree/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 22
eg.
Valid:
Invalid:
Solution: https://www.byte-by-byte.com/binarysearchtree/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 23
change(1) = 1
change(3) = 3
change(7) = 3
change(32) = 4
Solution: https://www.byte-by-byte.com/smallestchange/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 24
eg.
printTree(tree)
1
2
3
5
6
7
8
Solution: https://www.byte-by-byte.com/inordertraversal/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 25
eg.
Solution: https://www.byte-by-byte.com/sortstacks/
Solution: https://www.byte-by-byte.com/stackfromqueues/
30. Palindromes
Question: Given a linked list, write a function to determine whether the list is a
palindrome.
eg.
Solution: https://www.byte-by-byte.com/palindromes/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 26
eg.
push(1)
max() = 1
push(2)
max() = 2
push(1)
max() = 2
pop() = 1
max() = 2
pop() = 2
max() = 1
Solution: https://www.byte-by-byte.com/maxstack/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 27
eg.
Solution: https://www.byte-by-byte.com/twomissingnumbers/
eg.
Solution: https://www.byte-by-byte.com/bigintmod/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 28
Solution: https://www.byte-by-byte.com/swapvariables/
eg.
gray(0, 1
) = true
gray(1, 2 ) = false
Solution: https://www.byte-by-byte.com/graycode/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 29
eg.
Solution: https://www.byte-by-byte.com/rotatebits/
eg.
ones(0) = 0
ones(1) = 1
ones(2) = 1
ones(3) = 2
ones(7) =
3
Solution: https://www.byte-by-byte.com/onesinbinary/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 30
eg.
Solution: https://www.byte-by-byte.com/listcycles/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 31
eg.
Solution: https://www.byte-by-byte.com/randomlinkedlist/
eg.
dedup(1 -> 2 -> 3 -> 2 -> 1) = 1 -> 2 -> 3
Solution: https://www.byte-by-byte.com/deduplinkedlist/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 32
eg.
divide(1 -> 2 -> 3 -> 4) = 1 -> 2 , 3 -> 4
divide(1 -> 2 -> 3
-> 4
-> 5 ) = 1 -> 2 -> 3, 4 -> 5
Solution: https://www.byte-by-byte.com/splitlinkedlist/
eg.
list = 1 -> 2 -> 3 -> 4 -> 5 -> null
nthToLast(list, 0) = 5
nthToLast(list, 1) = 4
nthToLast(list, 4) = 1
nthToLast(list, 5) = null
Solution: https://www.byte-by-byte.com/nthtolastelement/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 33
eg.
Solution: https://www.byte-by-byte.com/threesum/
44. Tree Level Order
Question: Given a tree, write a function that prints out the nodes of the tree in level
order.
eg.
Solution: https://www.byte-by-byte.com/treelevelorder/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 34
45. Autocomplete
Question: Write an autocomplete class that returns all dictionary words with a given
prefix.
eg.
Solution: https://www.byte-by-byte.com/autocomplete/
46. String Deletion
Question: Given a string and a dictionary HashSet, write a function to determine the
minimum number of characters to delete to make a word.
eg.
Solution: https://www.byte-by-byte.com/stringdeletion/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 35
eg.
Solution: https://www.byte-by-byte.com/longestsubstring/
eg.
compress("a") = "a"
compress("aaa") = "a3"
compress("aaabbb") = "a3b3"
compress("aaabccc") = "a3b1c3"
Solution: https://www.byte-by-byte.com/stringcompression/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 36
eg.
fibonacci(1) = 1
fibonacci(5) = 5
fibonacci(10) = 55
Solution: https://www.byte-by-byte.com/fibonacci/
Solution: https://www.byte-by-byte.com/priorityqueue/
51. Kth Most Frequent String
● Question: Given a list of strings, write a function to get the kth most frequently
occurring string.
● Eg.
Solution: https://www.byte-by-byte.com/kthmostfrequentstring/
50 Coding Interview Questions • b
yte-by-byte.com • © 2
020 Byte by Byte LLC 37