A collection of Java interview practice problems and solutions, along with JUnit test files.
- Find the Smallest Number
Given an array, find the smallest number in the array. - Find Missing Numbers
In an array, 1-100 numbers are stored. One number is missing. How do you find it? - Find Duplicate Numbers
In an array of 1-100, exactly one number is duplicate. How do you find it? - Compare Arrays
Given two arrays, find which number(s) is not present in the second array. - Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. - Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. - Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. - Pascal Triangle
Given an integerx
, generate the firstx
number of rows of Pascal’s triangle. - Powerset
Given an array or set of objects, return a set of all possible subsets.
- Remove Character
Remove all instances of a specific character from the given String. - String Palindrome Check
Given a String, check if the String is a valid palindrome. - Multiply String
Given two numbers represented as strings, return multiplication of the numbers as a string. Do not use BigInteger. - Merge and Order String Characters
Given an array of strings, return a single string containing all the characters in alphabetical order.
- Array Implementation
Includes the following methods: push(), pop(), peek(), size(), isEmpty() and isFull(). - Balanced Parentheses
Given a String, determine if the parentheses have pairs and are in order.
- Reverse Linked List
Reverse a singly linked list.
- Binary Tree
Includes methods for inserting, removing, traversing (in order, pre-order, post-order)
and searching (find by value, smallest, nth smallest element).
- Quick Sort - average O(n log n); worst O(n^2)
- Merge Sort - guaranteed O(n log n)
- Insertion Sort - O(n^2)
- Bubble Sort - O(n^2)
- Selection Sort - O(n^2)
- Binary Search - O(log n)
- Linear Search - O(n)
- Is Prime
Given an integer, determine whether the integer is a prime number. - Fibonacci Sequence
Given an integerx
, printx
number of elements in the sequence.
Given an integer, return the value of the element in that position of the sequence. - Integer Palindrome Check
Given an integer, determine whether the integer is a valid palindrome. - Prime Sum
Given an even number (greater than 2), return two prime numbers whose sum will be equal to given number. - Find Factors
Return a list of all factors of a given integer.
Know a better solution for one of these problems?
Have an alternate or more efficient solution that you think is worth including?
See a test case that wasn't accounted for?
See a way to improve an existing solution?
Contributions and code improvement suggestions are welcome! Simply submit a pull-request or create an issue.
Before suggesting improvements or new solutions, please check the test folder for JUnit tests that corrospond to the file you are editing, and make sure your code passes those test. The test folder structure follows the same structure as the src folder.