Top 53 Java Programs for Coding and Programming Interviews

Hello guys, if you are learning to code and programming, or preparing for a programming job interview and looking for some practice material then you have come to the right place. Earlier, I have shared the best string, array, linked list, and binary tree coding problems and In this article, I am going to share some of the most common Java coding and programming problems for beginners. These are the problems, I have solved myself to learn to program and develop a coding sense and these are the ones that keep coming on Java coding interviews. By going through these coding problems you will not only learn Java but also prepare yourself for Java interviews. 

These computer programs are very simple but will teach you a lot about computer science, problem-solving, data structure, algorithms, programming languages, control structure, and in general how to think of a solution and then translate it into code. 

I call that coding sense and that's probably the most important thing for a programmer. It doesn't come overnight and it slowly develops when you utilize that part of your brain by solving coding problems.

I have listed the problems in no particular order and that's why you can solve them in any order you want. I generally pick a problem that is the easiest one to solve like checking if a number is even or odd, or calculating factorial, or checking if a given number is a palindrome.

Once you solve one problem, you can move to another but try as much as possible yourself before looking at the solution. Yes, I have also linked to the solution wherever possible. For some problems, I have not linked to the solution, which you can find by doing a quick Google search.

While most of the problems don't expect much but you should be familiar with the programming languages you are trying to learn like how to write for loop, and how to create a function, how to use if-else, and other conditional operators, etc. Actually, if you don't know, you will also learn when you try to solve these problems but then you need to check the solution first.

Some problems also require you to be familiar with essential data structures like an array, linked list, binary tree, stack, queue, binary search tree, graph, etc. If you are not familiar with or need to revise those concepts then you can also check out Data Structures and Algorithms: Deep Dive Using Java course on Udemy to learn those fundamental data structures first.




50+ Java Coding Problems from Programming Job Interviews

Without wasting any more of your time, here is my list of 50+ Coding and Programming Problems for Java programmers. I have given a solution in Java programming language because that's my favorite and strongest skill but you are free to solve these coding problems in any programming language of your choice like PythonJavaScriptRuby, or even Golang.

1. For a given array of integers (positive and negative) find the largest sum of a contiguous sequence

2. Algorithm: Implement a queue using 2 stacks (solution)

3. Algorithm: Integer division without the division operator (/) (solution)

4. How to print All permutations of a Given String in Java (solution)

Top 50 Java Programs from Coding and Programming Interviews



5. Algorithm: All combinations of a set (solution)

6. Given an array of n integers(consider +ve and also -ve), find a subarray such that the sum of that array s maximum. (solution)

7. What is Tortoise and hare algorithm? How to implement in Java? (solution)

8. Given two strings .. you need to tell whether they are a permutation of each other. (solution)

9. Given a string,  print the first non-repeating char. (solution)

10. Given two sorted lists, combine them into one sorted list. (solution)

11. Insert a node in a doubly-linked list. (solution)

12. You are given an integer array of size n containing elements in the range 0 to n-1. Now count the frequency of all elements. (solution)

13. Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once.

Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3} Output: 2

14. Given a positive integer n, count the total number of set bits in a binary representation of all numbers from 1 to n.

Examples:

Input: n = 3

Output:  4

Input: n = 6

Output: 9

15. Write a function add() that returns the sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc). (solution)

16. Write a Java program to find the largest or smallest of three integers, without using any of the comparison operators. (solution)

17. How to Print Fibonacci Series for N numbers (solution)

Java Programs from coding interviews with solution



18. Write a Program to check if given number is a Armstrong Number (solution)

19. How to check if a number is Even or Odd (solution)

20. Java Program to find GCD of two numbers (solution)

21. How to Check if a number is a palindrome or not (solution)

22. How to Check if String is palindrome or not  (solution)

23. Write a  Java Program to calculate Simple interest  (solution)

24. Write a Java Program to check a leap Year (solution)

25. How to implement Bubble Sort in Java (solution)

26. How to implement Insertion sort in Java (solution)

27. How to implement QuickSort in Java (solution)

28. Can you write a program to implement Algorithm for Selection Sort (solution)

29. Algorithm for Merge Sort (solution)

30. Algorithm for Heap Sort (solution)

31. Java Program to count vowels and consonants in a String (solution)

32. How to reverse String in Java (solution)

33. Can you Code a Java Program to reverse a number (solution)

34. How to calculate Nth factorial in Java (solution)

35. How to check if a given number is  a Prime number? (solution)
You need to write a program to check if the given number is prime or not. If the given number is prime then return true otherwise return false. A prime number is a number that is not divisible by any number except 1 and itself.

36. Write a program to calculate Area of a triangle in Java (solution)
It's simple as long as you remember the formula for calculating area of triangle. It also depends what is given like height and base of triangle or co-ordinates of triangle so you better clarify with interviewer. 

37. Can you write a program to find middle elements of the linked list (solution)

38. Write a Java program to Count occurrences of characters in String (solution)

39. How to Check if a number is positive or negative in Java? (solution)

40. Can you write a program to swap two numbers without using temp variable (solution)

41. How to check if two Strings are Anagram (solution)

42. Write a Java Program to check if a number is the power of Two or not (solution)

43. How to check if LinkedList contains loops or cycles in Java? (solution)

44. Write a Java Program to find the sum of digits in a number (solution)

45. How to remove duplicates from an array? (solution)
hint - this is a common Java coding problem, you can remove duplicates from an array creating a new array of unique elements. To find unique elements use a set and copy array elements there. set will not allow duplicates so you will have an array of unique elements.

By the way, if you want to learn general techniques to solve coding problems like Sliding Window, fast and slow pointer, Dynamic Programming then I highly recommend you to check out Grokking the Coding Interview: Patterns for Coding Questions course on Educative. It will teach you 15 coding patterns that can be used to solve many of these questions. 

50+  Coding and Programming Problems for Java Programmers


46. Find whether a given number is a power of 4 or not (solution)

47. Compute the minimum or maximum of two integers without branching (solution)

48. Given an unsigned integer, swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43 (00101011). Every even position bit is swapped with the adjacent bit on the right side (even position bits are highlighted in the binary representation of 23), and every odd position bit is swapped with an adjacent on the left side.

49. Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and the remaining two have occurred once). Find those two numbers in the most efficient way. (solution)

50. Write an Efficient C Program to Reverse Bits of a Number? (solution)

51. How to Count the number of set bits in a number (solution)

52. Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should return true -1 and +100 and should return false for -100 and -200. The function should not use any of the arithmetic operators.

53. How to implement LRU cache in Java. (solution)
You need to implement a Least Recently used (LRU) cache in Java where the least recently used item is evicted when the cache becomes full.  hint - you can implement this in Java using LinkedHashMap, it has a method which can notify you.


That's all about 50+ Java coding and programming problems for interviews. As I said, you can use these coding problems not just for learning Java but also for preparing for coding interviews. They will provide you enough practice and you will learn actual coding by solving these problems. 

Btw, coding interviews are not easy and you may need to prepare more questions and revise concepts, that's why I have jotted down some important resources for you below, you can join these courses to take your Java coding interview preparation to next level. 

Other Java and Data Structure and Algorithms Interview Questions

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check this list of Free Data Structure and Algorithms Courses for Programmers.

1 comment:

Feel free to comment, ask questions if you have any doubt.