vprusso / Byte-by-Byte-Solutions

Coding interview questions and solutions

Home Page:http://www.byte-by-byte.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Byte by Byte Solutions

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.

Contributing

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!

Index of Questions

  • 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.

  • Anagrams (Java) (Python)
    Given two strings, write a function to determine whether they are anagrams.

  • Autocomplete (Github)
    Write an autocomplete class that returns all dictionary words with a given prefix.

  • Balanced Binary Tree (Java) (Python)
    Given a binary tree, write a function to determine whether the tree is balanced.

  • Big Int Mod (Java) (Python)
    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.

  • Binary Search Tree Verification
    (Java) (Python)
    Given a binary tree, write a function to test if the tree is a binary search tree.

  • 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.

  • 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.

  • Consecutive Array (Java) (Python)
    Given an unsorted array, find the length of the longest sequence of consecutive numbers in the array.

  • Dedup Linked List (Java) (Python)
    Given an unsorted linked list, write a function to remove all the duplicates.

  • Fibonacci (C++) (Java) (Python)
    Given an integer n, write a function to compute the nth Fibonacci number.

  • Find Duplicates (C++) (Java) (Python)
    Given an array of integers where each value 1 <= x <= len(array), write a function that finds all the duplicates in the array.

  • FizzBuzz (C++) (Java) (Python)
    Output numbers from 1 to x. If the number is divisible by 3, replace it with “Fizz”. If it is divisible by 5, replace it with “Buzz”. If it is divisible by 3 and 5 replace it with “FizzBuzz”.

  • Gray Code (C++) (Java) (Python)
    Given two integers, write a function to determine whether or not their binary representations differ by a single bit.

  • Integer to Roman Numeral (C++) (Java) (Python)
    Given an integer, write a function to return its roman numeral representation.

  • Kth Most Frequest String
    Given a list of strings, write a function to get the kth most frequently occurring string.

  • Line Intersection (Java) (Python)
    Given two lines on a Cartesian plane, write a function to determine whether or not the lines intersect.

  • Linked List Cycle (Java) (Python)
    Given a linked list, determine whether it contains a cycle.

  • Longest Common Substring
    Given two strings, write a function that returns the longest common substring.

  • Longest Consecutive Branch
    Given a tree, write a function to find the length of the longest branch of nodes in increasing consecutive order.

  • Lowest Common Ancestor
    Given two nodes in a binary tree, write a function to find the lowest common ancestor.

  • 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.

  • Matrix Search
    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.

  • Max Stack (Java) (Python)
    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.

  • Median of Arrays
    Find the median of two sorted arrays.

  • 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.

  • Merge K Arrays
    Given k sorted arrays, merge them into a single sorted array.

  • 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.

  • 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.

  • 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.

  • 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.

  • Print Reversed List (C++) (Java) (Python)
    Given a linked list, write a function that prints the nodes of the list in reverse order.

  • Priority Queue (Java) (Python)
    Implement a Priority Queue.

  • Random Binary Tree (Github)
    Implement a binary tree with a method getRandomNode() that returns a random node.

  • 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.

  • Reverse Stack (C++) (Java) (Python)
    Given a stack, reverse the items without creating any additional data structures.

  • Rotate Bits (Java) (Python)
    Given a number, write a function to rotate the bits (ie circular shift).

  • Shortest Path (Github)
    Given a directed graph, find the shortest path between two nodes if one exists.

  • Sort Stacks (Java) (Python)
    Given a stack, sort the elements in the stack using one additional stack.

  • Smallest Change (Java) (Python)
    Given an input amount of change x, write a function to determine the minimum number of coins required to make that amount of change.

  • Split Linked List
    Given a linked list, write a function to split the list into two equal halves.

  • Stack from Queues
    Implement a LIFO stack with basic functionality (push and pop) using FIFO queues to store the data.

  • 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.

  • 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.

  • Square Submatrix (Java) (Python)
    Given a 2D array of 1s and 0s, find the largest square subarray of all 1s.

  • Sum (Java) (Python)
    Given two integers, write a function to sum the numbers without using any arithmetic operators.

  • Swap Variables (C++) (Java) (Python)
    Given two integers, write a function that swaps them without using any temporary variables.

  • Three Sum
    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.

  • 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.

  • Tree Level Order (Java) (Python)
    Given a tree, write a function that prints out the nodes of the tree in level order.

  • Two Missing Numbers (C++) (Java) (Python)
    Given an array containing all the numbers from 1 to n except two, find the two missing numbers.

  • Zero Matrix (Java) (Python)
    Given a boolean matrix, update it so that if any cell is true, all the cells in that row and column are true.

  • Zero Sum Subarray
    Given an array, write a function to find any subarray that sums to zero, if one exists.

About

Coding interview questions and solutions

http://www.byte-by-byte.com


Languages

Language:Java 50.7%Language:Python 32.8%Language:C++ 16.5%