Coding is an integral part of any programming job interviews, and Java development interviews are no exception. I would even suggest you should never hire anyone without testing their coding skill, coding is also an art, and more often than a good code is an excellent developer as well. If you look at tech giants like Amazon, Facebook, and Google they thoroughly test the coding skill of any developer they hire, notably Amazon who first send online coding exercises to filter Java programmers who can code. This online test usually gives you requirements and ask you to write a program in a limited time, usually 2 to 3 hours. The application should meet the output provided by the exercise itself. These type of tasks are very tough to crack if you don't have excellent coding skill.

Btw, the most crucial question is how do you develop that kind of coding skill in the first place? Well, things always start small, and if you pay attention, there are many Java job interviews where you would have been asked to write small programs.

They are simple, but yet they give a good indication of the coding skill of prospective candidates. They are usually preferred by many companies because it often requires 10 to 20 minutes to write the solution and discuss them.

In this list, I am going to share 50 of such small programs from Java Programming interviews. These programs are from various Data Structure and Algorithm topics like an array, string, linked list, binary tree, etc. If you don't have a good knowledge of Data structure and algorithm, I suggest you to first read a good book on Data Structure and Algorithms like Introduction to Algorithms by Thomas H. Cormen.

But, If you find the book challenging, you can also join a comprehensive online course on Data structure and algorithms like

##

Here is a big list of Java programs for Job Interviews. As I said, it includes questions from problem-solving, linked list, array, string, matrix, bitwise operators and other miscellaneous parts of programming. Once you are gone through these questions, you can handle a good number of questions on real Job interviews.

Write a simple Java program which will print Fibonacci series, e.g. 1 1 2 3 5 8 13 ... . up to a given number. We prepare for cross questions like using iteration over recursion and how to optimize the solution using caching and memoization.

Write a Java program to check if a given number is prime or not. Remember, a prime number is a number which is not divisible by any other number, e.g. 3, 5, 7, 11, 13, 17, etc. Be prepared for cross, e.g. checking till the square root of a number, etc.

You need to write a simple Java program to check if a given String is palindrome or not. A Palindrome is a String which is equal to the reverse of itself, e.g., "Bob" is a palindrome because of the reverse of "Bob" is also "Bob." Though be prepared with both recursive and iterative solution of this problem. The interviewer may ask you to solve without using any library method, e.g. indexOf() or subString() so be prepared for that.

This is generally asked as follow-up or alternative of the previous program. This time you need to check if given Integer is palindrome or not. An integer is called palindrome if it's equal to its reverse, e.g. 1001 is a palindrome, but 1234 is not because the reverse of 1234 is 4321 which is not equal to 1234. You can use divide by 10 to reduce the number and modulus 10 to get the last digit. This trick is used to solve this problem.

A number is called an Armstrong number if it is equal to the cube of its every digit. For example, 153 is an Armstrong number because of 153= 1+ 125+27, which is equal to 1^3+5^3+3^3. You need to write a program to check if the given number is Armstrong number or not.

This is one of the interesting programs from Java Interviews, mostly asked to 2 to 3 years of experienced programmers or higher. The interviewer simply asked you to write code where a resource is accessed by multiple threads. You need to write code in such a way that no deadlock should occur. The trick to solving this problem is acquiring resources in order and release them in reverse order, e.g. first acquire resource R1 and only if you have got R1 to go for R2. This way, you can avoid deadlock.

This is one of the simplest programs you can expect in interviews. It is generally asked to see if you can code or not. Sometimes interviewer may also ask about changing a recursive solution to iterative one or vice-versa.

This problem is similar to the String Palindrome problem we have discussed above. If you can solve that problem, you can solve this as well. You can use indexOf() or substring() to reverse a String or alternatively, convert the problem to reverse an array by operating on character array instead of String. If you want to brush up your data structure skill you can also check Data Structures and Algorithms: Deep Dive Using Java course on Udemy before solving this question.

Write a program to remove duplicates from an array in Java without using the Java Collection API. The array can be an array of String, Integer or Character, your solution should be independent of the type of array. If you want to practice more array-based questions, then see this list of top 30 array interview questions from Java interviews.

10. Printing patterns (solutions)

11. Print repeated characters of String? (solution)

12. GCD of two numbers (solution)

You need to write a program to calculate the square root of a number without using the Math.sqrt() function of JDK. You need to write your logic and method to calculate the square root. You can though use the popular algorithm, like Newton's method.

14. Reverse array in place (solution)

15. Reverse words of a sentence (solution)

16. Leap year (solution)

17. Binary search (solution)

Write a program to check if two given String is Anagram of each other. Your function should return true if two Strings are Anagram, false otherwise. A string is said to be an anagram if it contains the same characters and same length, but in a different order, e.g. army and Mary are anagrams. You can ignore cases for this problem, but you should clarify that from your interview.

This one of the popular OOAD (object-oriented analysis and design) question from Java Interviews. You will be given 3 hours to design and code a vending machine satisfying some of the business requirements. You also need to write unit tests to prove your code satisfy those requirements. You can see this article for more object-oriented analysis questions.

20. Reverse a number (solution)

21. The first non-repeated character of String (solution)

22. Finding Middle element of linked list in one pass (solution)

23. Pre-order traversal (solution)

24. Pre-order traversal without recursion (solution)

25. In order traversal (solution)

26. In order traversal without recursion (solution)

27. Post-order traversal (solution)

28. Postorder traversal without recursion (solution)

29. Print all leaves of a binary tree (solution)

You need to write a Java program to sort an array of integers using a quick sort algorithm. You cannot use any library method, e.g. JDK or a third party library, which means, you need to first implement the quicksort algorithm and then sort the array.

Write a program to implement the insertion sort algorithm in Java. The program should take an unsorted array and sort it using insertion sort algorithm Also explain the best case and worst case time and space complexity of the Insertion sort algorithm.

Write a program to implement the bubble sort algorithm in Java. You can use basic operators and functions, but sorting functions from Java API is not allowed.

33. Transpose a matrix (solution)

Write a Java program to print all permutations of a given String. For example, if given String is "GOD" then your program should print all 6 permutations of this string, e.g. "GOD," "OGD," "DOG," "GDO," "ODG," and "DGO."

35. Reverse a String in place (solution)

36. Adding two matrices in Java (solution)

37. Matrix multiplication (solution)

38. Removal all white space from String (solution)

Write a program to reverse a singly linked list in Java. You can use iteration and recursion to solve this problem, but you should reverse a linked list in place.

Just write a program in Java to find the length of a singly linked list in one pass, i.e. in just one iteration of a singly linked list.

Write a program to check if given linked list has a loop or not. Sometimes a linked list get corrupt, and two nodes point to the same node, which forms the loop or cycle in the linked list.

42. Find the start of loop in a linked list (solution)

43. Find the middle element of a linked list (solution)

You need to write a program to find the 3rd element from the tail of a singly linked list. You need to solve this problem without iterating twice. If you want more linked list questions, you can see the article about frequently asked linked list interview questions.

It's possible to convert a doubly-linked list to a binary tree, you need to write a Java program which takes a doubly-linked list and returns a binary tree.

You need to given an unsorted linked list, and you need to write a program in Java to sort them in ascending order of the values in each node.

You need to write a Java program to implement quicksort sorting algorithm without recursion. You can use essential JDK classes and programming constructs, but recursion is not allowed.

This program is increasingly getting popular on Java interview because it sorts a given array in linear time. Though there is a lot of prerequisites, e.g. you must know the maximum value present in the array, it is a very interesting problem from interview point of view. You need to write a program to implement a bucket sort algorithm in Java. If you are not familiar with Bucket sort or any other linear sorting algorithm, I suggest you to first read a good on algorithms, e.g. Introduction to Algorithms by Thomas H. Cormen.

This is another problem which is similar to the previous one because counting sort is also a linear sorting algorithm. Just remember that bucket sort, and counting sort are different algorithms, so it's also good to state how they are different.

Write a program which accepts two given String and checks if they are the rotation of each. If they then return true otherwise return false. A String is said to be a rotation of other string if they contain same characters and the sequence is rotated across any character, e.g. "dabc" is a rotation of "abcd" but "dbac" is not. If you want to practice more string-based questions, you can also see my list of 20 String-based algorithm questions from Java interviews.

Write a program to implement an LRU cache in Java. An LRU cache means Least Recently Used Cache which removes the least recently used element if the cache is full. You can use LinkedHashMap to implement LRU cache in Java.

Implement the merge sort algorithm in Java. You can write a recursive or iterative solution, whichever you like. You also need to explain the time and space complexity for the best, worst, and average case.

That's all about

These small programs touch several important areas, e.g. popular data structures like an array, linked list, binary tree, binary search tree, string, etc., popular algorithms, e.g. sieve of the Eratosthenes algorithm for generating primes, the Euclidean algorithm for calculating LCM, and GCF, Fibonacci sequence, printing patterns and so on.

These programs also touch base on useful operators like bit-shift and bitwise operators, modulus operators, and others. Overall, you get a good understanding of the Java programming world by practicing these Java programs from coding interviews.

Thanks for reading this article so far, if you like these Java programs, then please share with your friends and colleagues, if you have any question or problem, then please drop a comment.

Btw, the most crucial question is how do you develop that kind of coding skill in the first place? Well, things always start small, and if you pay attention, there are many Java job interviews where you would have been asked to write small programs.

They are simple, but yet they give a good indication of the coding skill of prospective candidates. They are usually preferred by many companies because it often requires 10 to 20 minutes to write the solution and discuss them.

In this list, I am going to share 50 of such small programs from Java Programming interviews. These programs are from various Data Structure and Algorithm topics like an array, string, linked list, binary tree, etc. If you don't have a good knowledge of Data structure and algorithm, I suggest you to first read a good book on Data Structure and Algorithms like Introduction to Algorithms by Thomas H. Cormen.

But, If you find the book challenging, you can also join a comprehensive online course on Data structure and algorithms like

**Data Structures and Algorithms: Deep Dive Using Java**on Udemy to learn better and more comfortable.##
__Top 50 Java Programs from Coding Interviews__

Here is a big list of Java programs for Job Interviews. As I said, it includes questions from problem-solving, linked list, array, string, matrix, bitwise operators and other miscellaneous parts of programming. Once you are gone through these questions, you can handle a good number of questions on real Job interviews.**1. Fibonacci series**(solution)Write a simple Java program which will print Fibonacci series, e.g. 1 1 2 3 5 8 13 ... . up to a given number. We prepare for cross questions like using iteration over recursion and how to optimize the solution using caching and memoization.

**2. A prime number**(solution)Write a Java program to check if a given number is prime or not. Remember, a prime number is a number which is not divisible by any other number, e.g. 3, 5, 7, 11, 13, 17, etc. Be prepared for cross, e.g. checking till the square root of a number, etc.

**3. String Palindrome**(solution)You need to write a simple Java program to check if a given String is palindrome or not. A Palindrome is a String which is equal to the reverse of itself, e.g., "Bob" is a palindrome because of the reverse of "Bob" is also "Bob." Though be prepared with both recursive and iterative solution of this problem. The interviewer may ask you to solve without using any library method, e.g. indexOf() or subString() so be prepared for that.

**4. Integer Palindrome**(solution)This is generally asked as follow-up or alternative of the previous program. This time you need to check if given Integer is palindrome or not. An integer is called palindrome if it's equal to its reverse, e.g. 1001 is a palindrome, but 1234 is not because the reverse of 1234 is 4321 which is not equal to 1234. You can use divide by 10 to reduce the number and modulus 10 to get the last digit. This trick is used to solve this problem.

**5. Armstrong number**(solution)A number is called an Armstrong number if it is equal to the cube of its every digit. For example, 153 is an Armstrong number because of 153= 1+ 125+27, which is equal to 1^3+5^3+3^3. You need to write a program to check if the given number is Armstrong number or not.

**6. Avoiding deadlock in Java**(solution)This is one of the interesting programs from Java Interviews, mostly asked to 2 to 3 years of experienced programmers or higher. The interviewer simply asked you to write code where a resource is accessed by multiple threads. You need to write code in such a way that no deadlock should occur. The trick to solving this problem is acquiring resources in order and release them in reverse order, e.g. first acquire resource R1 and only if you have got R1 to go for R2. This way, you can avoid deadlock.

**7. Factorial**(solution)This is one of the simplest programs you can expect in interviews. It is generally asked to see if you can code or not. Sometimes interviewer may also ask about changing a recursive solution to iterative one or vice-versa.

**8. Reverse a String**(solution)This problem is similar to the String Palindrome problem we have discussed above. If you can solve that problem, you can solve this as well. You can use indexOf() or substring() to reverse a String or alternatively, convert the problem to reverse an array by operating on character array instead of String. If you want to brush up your data structure skill you can also check Data Structures and Algorithms: Deep Dive Using Java course on Udemy before solving this question.

**9. Remove duplicates from an array**(solution)Write a program to remove duplicates from an array in Java without using the Java Collection API. The array can be an array of String, Integer or Character, your solution should be independent of the type of array. If you want to practice more array-based questions, then see this list of top 30 array interview questions from Java interviews.

10. Printing patterns (solutions)

11. Print repeated characters of String? (solution)

12. GCD of two numbers (solution)

**13. The square root of a number**(solution)You need to write a program to calculate the square root of a number without using the Math.sqrt() function of JDK. You need to write your logic and method to calculate the square root. You can though use the popular algorithm, like Newton's method.

14. Reverse array in place (solution)

15. Reverse words of a sentence (solution)

16. Leap year (solution)

17. Binary search (solution)

**18. String Anagram**(solution)Write a program to check if two given String is Anagram of each other. Your function should return true if two Strings are Anagram, false otherwise. A string is said to be an anagram if it contains the same characters and same length, but in a different order, e.g. army and Mary are anagrams. You can ignore cases for this problem, but you should clarify that from your interview.

**19. Design a Vending Machine**(solution)This one of the popular OOAD (object-oriented analysis and design) question from Java Interviews. You will be given 3 hours to design and code a vending machine satisfying some of the business requirements. You also need to write unit tests to prove your code satisfy those requirements. You can see this article for more object-oriented analysis questions.

20. Reverse a number (solution)

21. The first non-repeated character of String (solution)

22. Finding Middle element of linked list in one pass (solution)

23. Pre-order traversal (solution)

24. Pre-order traversal without recursion (solution)

25. In order traversal (solution)

26. In order traversal without recursion (solution)

27. Post-order traversal (solution)

28. Postorder traversal without recursion (solution)

29. Print all leaves of a binary tree (solution)

**30. Sort array using quicksort**(solution)You need to write a Java program to sort an array of integers using a quick sort algorithm. You cannot use any library method, e.g. JDK or a third party library, which means, you need to first implement the quicksort algorithm and then sort the array.

**31. Insertion sort**(solution)Write a program to implement the insertion sort algorithm in Java. The program should take an unsorted array and sort it using insertion sort algorithm Also explain the best case and worst case time and space complexity of the Insertion sort algorithm.

**32. Bubble sort**(solution)Write a program to implement the bubble sort algorithm in Java. You can use basic operators and functions, but sorting functions from Java API is not allowed.

33. Transpose a matrix (solution)

**34. Print all permutations of String**(solution)Write a Java program to print all permutations of a given String. For example, if given String is "GOD" then your program should print all 6 permutations of this string, e.g. "GOD," "OGD," "DOG," "GDO," "ODG," and "DGO."

35. Reverse a String in place (solution)

36. Adding two matrices in Java (solution)

37. Matrix multiplication (solution)

38. Removal all white space from String (solution)

**39. Reverse a linked list**(solution)Write a program to reverse a singly linked list in Java. You can use iteration and recursion to solve this problem, but you should reverse a linked list in place.

**40. Find the length of the linked list**(solution)Just write a program in Java to find the length of a singly linked list in one pass, i.e. in just one iteration of a singly linked list.

**41. Check if a linked list has a loop**(solution)Write a program to check if given linked list has a loop or not. Sometimes a linked list get corrupt, and two nodes point to the same node, which forms the loop or cycle in the linked list.

42. Find the start of loop in a linked list (solution)

43. Find the middle element of a linked list (solution)

**44. Find the 3rd element from the tail linked list (**solution)You need to write a program to find the 3rd element from the tail of a singly linked list. You need to solve this problem without iterating twice. If you want more linked list questions, you can see the article about frequently asked linked list interview questions.

**44. Convert a linked list to a binary tree**(solution)It's possible to convert a doubly-linked list to a binary tree, you need to write a Java program which takes a doubly-linked list and returns a binary tree.

**45. Sort a linked list**(solution)You need to given an unsorted linked list, and you need to write a program in Java to sort them in ascending order of the values in each node.

**46. Iterative Quicksort**(solution)You need to write a Java program to implement quicksort sorting algorithm without recursion. You can use essential JDK classes and programming constructs, but recursion is not allowed.

**46. Bucket sort**(solution)This program is increasingly getting popular on Java interview because it sorts a given array in linear time. Though there is a lot of prerequisites, e.g. you must know the maximum value present in the array, it is a very interesting problem from interview point of view. You need to write a program to implement a bucket sort algorithm in Java. If you are not familiar with Bucket sort or any other linear sorting algorithm, I suggest you to first read a good on algorithms, e.g. Introduction to Algorithms by Thomas H. Cormen.

**47. Counting sort**(solution)This is another problem which is similar to the previous one because counting sort is also a linear sorting algorithm. Just remember that bucket sort, and counting sort are different algorithms, so it's also good to state how they are different.

**48. Check if two string rotation of each othe**rWrite a program which accepts two given String and checks if they are the rotation of each. If they then return true otherwise return false. A String is said to be a rotation of other string if they contain same characters and the sequence is rotated across any character, e.g. "dabc" is a rotation of "abcd" but "dbac" is not. If you want to practice more string-based questions, you can also see my list of 20 String-based algorithm questions from Java interviews.

**49. LRU cache in Java**(solution)Write a program to implement an LRU cache in Java. An LRU cache means Least Recently Used Cache which removes the least recently used element if the cache is full. You can use LinkedHashMap to implement LRU cache in Java.

**50. Merge sort**Implement the merge sort algorithm in Java. You can write a recursive or iterative solution, whichever you like. You also need to explain the time and space complexity for the best, worst, and average case.

That's all about

**top 50 programs from Java interviews**. You can practice these Java programs even if you are not preparing for any Job interview. They not only help you to do well on your programming job interviews but also on learning how to code and developing your programming skill and coding sense.These small programs touch several important areas, e.g. popular data structures like an array, linked list, binary tree, binary search tree, string, etc., popular algorithms, e.g. sieve of the Eratosthenes algorithm for generating primes, the Euclidean algorithm for calculating LCM, and GCF, Fibonacci sequence, printing patterns and so on.

These programs also touch base on useful operators like bit-shift and bitwise operators, modulus operators, and others. Overall, you get a good understanding of the Java programming world by practicing these Java programs from coding interviews.

**Recommended Resources for Coding Interviews**- Data Structures and Algorithms: Deep Dive Using Java
- Algorithms and Data Structures - Part 1 and 2
- Grokking the System Design Interview
- Cracking the Coding Interview - 189 Questions and Solutions
- Grokking the Coding Interview: Patterns for Coding Questions
- The Coding Interview Bootcamp: Algorithms + Data Structures
- 50+ Data Structure and Algorithms Interview Questions
- 20+ Basic Algorithms Questions from Interviews
- 10 Data Structure and Algorithms Courses to Crack Coding Interview

Thanks for reading this article so far, if you like these Java programs, then please share with your friends and colleagues, if you have any question or problem, then please drop a comment.

**P. S. -**Are you ready for the Interview?**Take TripleByte's quiz**and go directly to the final round of interviews with top tech companies like Coursera, Adobe, Dropbox, Grammarly, Uber, Quora, Evernote, Twitch, and many more.
## 29 comments :

Nice collection of interview question

Good Programmas

Good collection

Do you know any other books recommended for coding interviews?

I used:

"Cracking the Coding Interview" by Gayle Laakmann McDowell

"TOP 30 Java Interview Coding Tasks" by Matthew Urban

"Elements of Programming Interviews in Java" by Adnan Aziz, Tsung-Hsien Lee, Amit Prakash

"Coding Interview Questions" by Narasimha Karumanchi

nice collection of questions. Thank you.

Really helpful ! Thanks !

String palindrome is found with error.unable to view it

Hello @Unknown, can you give more details? you mean you can't see the solution?

Ah got it, the link is incorrect, I'll correct it.

perfect ...thank you,Guys!

Thanks! for help,guys.

Very nice..

But I need a differential and integral problems in java

If any one think about it reply me...

We'll discuss about itðŸ˜ŠðŸ˜ŠðŸ˜Š

Nice thank you

Nice

ðŸ™„ðŸ™„ðŸ™„ good

It's very much benificial for us

My problem with this type of question is that most of them do not reflect the types of problems you encounter on the job. A good java programmer knows how to perform task needed by the job. A DB centric application will manipulate many data results efficiently. That might mean knowing lists, iterations, or using lombok. A realtime application will be more focused on things like concurrency. I've used Java professionally since it was in beta nad have never had to shift bits.

Hello Bill, interview and real work is always different. It's part of the culture we have. I think asking these kind of question is to check if people have good fundamental knowledge or not. This is important for engineering a solution rather than just coding. For example, if you know about paging, swap memory, virtual memory you will be better equipped to solve memory related problems of Java application. Just my 2 cent.

Can u plzz help me in solving the below pattern-:

55555

54444

54333

54322

54321

@Unknown, I can give you an hint, use two for loop and a combination of system.out.println() and system.print() to print this pattern

@Unknown

public static void main(String[] s)

{

int i=55555;

int r=1111;

System.out.println(i);

for(int j=0;j<4;j++)

{

i=i-r;

System.out.println(i);

r=r/10;

}

@Unknown

public static void main(String []args){

String num = "";

for(int i = 5; i>=1; i--){

num += i;

//System.out.print(i);

for(int j = 1; j<=i; j++){

System.out.print(i);

}

if(i != 1){

System.out.print("\n"+num);

}

}

}

what are the modifiers applicable in java main method

5432 is the answer of program

Very helpful this kind of coding to pass in coding test

Num value is 5

*** *

** **

* ***

Please slove this program

Reverse String .....

public class Reve

{

public static void main(String [] args)

{

String s="hello java";

String s1=" ";

for(int i=s.length()-1;i>=0;i--)

{

s1=s1+s.CharAt();

}

System.out.println("Original String : "+s);

System.out.println("Reverse String : "+s1);

}

}

## Post a Comment