Byte by Byte is a coding interview prep website that provides many practice interview questions as well as detailed explanations. This repo contains working, tested code for the solutions on Byte by Byte.
I would love to compile solutions to all of the problems here, as well as offer solutions in different languages. Currently we only have Java solutions but Python, C, or any other languages would be most welcome. Just create a pull request with your changes. And make sure your code includes at least a few tests!
-
Median of Arrays
Find the median of two sorted arrays. -
Priority Queue (Java) (Python)
Implement a Priority Queue. -
0-1 Knapsack (Java) (Python)
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. -
Matrix Product (Github)
Given a matrix, find the path from top left to bottom right with the greatest product by moving only down and right. -
Find Duplicates (C++) (Java) (Python)
Given an array of integers where each value1 <= x <= len(array)
, write a function that finds all the duplicates in the array. -
Integer to Roman Numeral (C++) (Java) (Python)
(Php)
Given an integer, write a function to return its roman numeral representation. -
Autocomplete (Github)
Write an autocomplete class that returns all dictionary words with a given prefix. -
Linked List Cycle (Java) (Python)
Given a linked list, determine whether it contains a cycle. -
Build Order (Java) (Python)
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. -
Consecutive Array (Java) (Python)
Given an unsorted array, find the length of the longest sequence of consecutive numbers in the array. -
Zero Matrix (Github)
Given a boolean matrix, update it so that if any cell is true, all the cells in that row and column are true. -
Random Binary Tree (Github)
Implement a binary tree with a method getRandomNode() that returns a random node. -
Two Missing Numbers (C++) (Java) (Python)
Given an array containing all the numbers from 1 to n except two, find the two missing numbers. -
Shortest Path (Github)
Given a directed graph, find the shortest path between two nodes if one exists. -
Square Submatrix (Java) (Python)
Given a 2D array of 1s and 0s, find the largest square subarray of all 1s. -
Random Linked List (Java)
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. -
Big Int Mod (Java) (Python)
Given a list of bytesa
, each representing one byte of a larger integer (ie.{0x12, 0x34, 0x56, 0x78}
represents the integer0x12345678
), and an integerb
, finda % b
. -
Merge K Arrays
Given k sorted arrays, merge them into a single sorted array. -
Dedup Linked List (Java) (Python)
Given an unsorted linked list, write a function to remove all the duplicates. -
Lowest Common Ancestor
Given two nodes in a binary tree, write a function to find the lowest common ancestor. -
String Deletion (Java) (Python)
Given a string and a dictionary HashSet, write a function to determine the minimum number of characters to delete to make a word. -
Sum (Java) (Python)
Given two integers, write a function to sum the numbers without using any arithmetic operators. -
Reverse Stack (C++) (Java) (Python)
Given a stack, reverse the items without creating any additional data structures. -
Swap Variables (C++) (Java) (Python)
Given two integers, write a function that swaps them without using any temporary variables. -
Matrix Search
Given ann x m
array where all rows and columns are in sorted order, write a function to determine whether the array contains an elementx
. -
Clock Angle (C++) (Java) (Python)
Given two integers, an hour and a minute, write a function to calculate the angle between the two hands on a clock representing that time. -
Fibonacci (C++) (Java) (Python)
Given an integer n, write a function to compute the nth Fibonacci number. -
Tree to Doubly Linked List
Given a tree, write a function to convert it into a circular doubly linked list from left to right by only modifying the existing pointers. -
Line Intersection (Java) (Python)
Given two lines on a Cartesian plane, write a function to determine whether or not the lines intersect. -
Longest Consecutive Branch
Given a tree, write a function to find the length of the longest branch of nodes in increasing consecutive order. -
Sort Stacks
Given a stack, sort the elements in the stack using one additional stack. -
Print Reversed List (C++) (Java) (Python)
Given a linked list, write a function that prints the nodes of the list in reverse order. -
Longest Common Substring
Given two strings, write a function that returns the longest common substring. -
Stack from Queues
Implement a LIFO stack with basic functionality (push and pop) using FIFO queues to store the data. -
Balanced Binary Tree
Given a binary tree, write a function to determine whether the tree is balanced. -
Gray Code (C++) (Java) (Python)
Given two integers, write a function to determine whether or not their binary representations differ by a single bit. -
Merge Arrays
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. -
Zero Sum Subarray
Given an array, write a function to find any subarray that sums to zero, if one exists. -
Three Sum
Given a list of integers, write a function that returns all sets of 3 numbers in the list,a
,b
, andc
, so thata + b + c == 0
. -
Rotate Bits (Java) (Python)
Given a number, write a function to rotate the bits (ie circular shift). -
Palindromes
Given a linked list, write a function to determine whether the list is a palindrome. -
Permutations
Write a function that returns all permutations of a given list. -
N Stacks 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.
-
Tree Level Order (Java) (Python)
Given a tree, write a function that prints out the nodes of the tree in level order. -
Split Linked List
Given a linked list, write a function to split the list into two equal halves. -
Kth Most Frequest String
Given a list of strings, write a function to get the kth most frequently occurring string. -
String Compression (Java) (Python)
Given a string, write a function to compress it by shortening every sequence of the same character to that character followed by the number of repetitions. If the compressed string is longer than the original, you should return the original string. -
Anagrams (Java) (Python)
Given two strings, write a function to determine whether they are anagrams. -
Binary Search Tree Verification
Given a binary tree, write a function to test if the tree is a binary search tree. -
Max Stack (Java) (Python)
Implement a LIFO stack that has apush()
,pop()
, andmax()
function, wheremax()
returns the maximum value in the stack. All of these functions should run inO(1)
time. -
Number of Ones in Binary (C++) (Java) (Python)
Given an integer, write a function to compute the number of ones in the binary representation of the number. -
Smallest Change (Java) (Python)
Given an input amount of changex
, write a function to determine the minimum number of coins required to make that amount of change. -
Nth-to-last Element (Java) (Python)
Given a linked list, and an input n, write a function that returns the nth-to-last element of the linked list. -
FizzBuzz (C++) (Java) (Python)
Output numbers from1
tox
. If the number is divisible by3
, replace it with“Fizz”
. If it is divisible by5
, replace it with“Buzz”
. If it is divisible by3
and5
replace it with“FizzBuzz”
.