Top 100 Data Structure and Algorithm Interview Questions for Java Programmers (2024)

Data structure and algorithms are a core part of any Programming job interview. It doesn't matter whether you are a C++ developer, a Java developer, or a Web developer working in JavaScript, Angular, React, or Query. As a computer science graduate, it's expected from a program to have strong knowledge of both basic data structures, like the array, linked list, binary tree, hash table, Stack, Queue, and advanced data structures like the binary heap, trie, self-balanced tree, circular buffer, etc.

I have taken a lot of Java interviews for both junior and senior positions in the past, and I have also been involved in interviewing C++ developers. One difference that I have clearly noticed between a C++ and a Java developer is their understanding and command of Data structure and algorithms.

On average, a C or C++ developer showed a better understanding and application of data structure, and their coding skill was also better than Java developers. 

This is not a coincidence, though. As per my experience, there is a direct correlation between a programmer having a good command of the algorithm and being a good developer and coder.

I firmly believe that the interview teaches you a lot in a short time, and that's why I am sharing some frequently asked Data structure and algorithm questions from various Java interviews.

If you are familiar with them, then try to solve them by hand and if you do not, then learn about them first and then solve them. If you need to refresh your knowledge of data structure and algorithms, you can also take help from a good book, our course like Data Structures and Algorithms: Deep Dive Using Java, for quick reference.





100+ Data Structures and Algorithm Interview Questions with Solution (2024)

For the sake of clarity and focus, I have categorized these data structure and algorithmic questions into various sub-categories, e.g., String questions, array-based questions, linked list questions, binary tree-related questions, searching and sorting based questions, and bit manipulation questions.

This way, you can start with the topic you feel most comfortable with and slowly progress to the topic you want to improve.


1. String Interview Questions

The String is probably the most used data structure. You will see it right from your programming course, and you will use it throughout your professional project. There is hardly an application written in Java and C++ that doesn't use String.

They are everywhere. From the C++ perspective, String is nothing but a null-terminated character array, but from the Java perspective, String is a full-fledged object backed by a character array.

In this category, you will find questions that require String manipulations, e.g., substring, reversing, searching, sorting, slicing and dicing, etc.

Here is a list of some of the frequently asked String Interview Questions from Coding Interviews:

1. Print duplicate characters from String? (solution)

2. Check if two Strings are anagrams of each other? (solution)

3. Print the first non-repeated character from String? (solution)

4. Reverse a given String using recursion? (solution)

5. Check if a String contains only digits? (solution)

6. Find duplicate characters in a String? (solution)

7. Count many vowels and consonants in a given String? (solution)

8. Count the occurrence of a given character in String? (solution)

9. Find all permutations of String? (solution)

10. Reverse words in a given sentence without using any library method? (solution)

11. Check if two String is a rotation of each other? (solution)

12. Check if the given String is Palindrome? (solution)


If you can solve all these String questions without any help, then you are in good shape. For more advanced questions, I suggest you solve problems given on the Algorithm Design Manual by Steven Skiena, a book with the most challenging algorithm questions.

String algorithm interview questions



2. Array and Matrix Interview Questions

Next to String is an array, the second most frequently used data structure. Array stores elements in a contiguous memory location, and in C++, you can also access array elements using pointer arithmetic. Still, in Java, an array is again an object, which provides just the length method.

You can only access the array using the index, and Java also doesn't valid index check, and if you try to access a display with an invalid index, you will get Java. lang.ArrayIndexOutOfBoundsException, so beware of that.

Here is a list of some of the frequently asked Array and Matrix-based Programming questions:

11. Find a missing number in a given integer array of 1 to 100? (solution)

12. Find the duplicate number on a given integer array? (solution)

13. The largest and smallest number in an unsorted integer array? (solution)

14. Find all pairs of integer arrays whose sum is equal to a given number? (solution)

15. Find duplicate numbers in an array if it contains multiple duplicates? (solution)

16. Remove duplicates from the given array in Java? (solution)

17. Sort an integer array in place using the QuickSort algorithm? (solution)

18. Remove duplicates from an array in place? (solution)

19. Reverse an array in place in Java? (solution)

20. Find multiple missing numbers in a given integer array with duplicates? (solution)

21. Perform a binary search in a given array? (solution)

22. Transpose a Matrix? (solution)

23. Add or subtract two Matrices? (solution)

24. Multiply two Matrices in Java? (solution)

25. Calculate the average of all numbers in a given array? (solution)


Suppose you need more advanced questions based upon array. In that case, you can also see The Coding Interview Bootcamp: Algorithms + Data Structures,  a bootcamp style course on algorithms, especially designed for interview preparation to get a job on technical giants like Google, Microsoft, Apple, Facebook, etc.

Data Structure and Algorithm Interview Questions Java




3. Linked List Interview Questions

A linked list is another vital data structure from an interview point of view; here are some of the frequently asked related list questions from programming interviews:

Here is a list of some of the standard linked list data structure questions from interviews:

26. Find the middle element of a singly linked list in one pass? (solution)

27. Find the 3rd node from the end in a singly linked list? (solution)

28. Check if a given linked list contains a cycle? How to find the starting node of the cycle? (solution)

29. Find the length of a singly linked list? (solution)

30. Reverse a linked list? (solution)

31. Reverse a singly linked list without recursion? (solution)

32. Remove duplicate nodes in an unsorted linked list? (solution)

33. Find the sum of two linked lists using Stack? (program)

34. Remove duplicate elements from a sorted linked list? (solution)


If you need more interview questions based on the linked list, you can also refer to this list of 30 related questions.




4. Binary Tree Interview Questions

the tree data structure is another famous data structure in programming interviews. It has several variants, e.g., a binary tree, search tree, and even binary heaps. It's almost guaranteed to see a couple of binary tree questions in programming job interviews.

Here is a list of some of the popular binary tree interview questions from programming job interviews:

35. Implement a binary search tree?  (solution)

36. Pre-order traversal in a given binary tree? (solution)

37. Traverse a given binary tree in Pre-order without recursion (solution)

I38. mplement a Post-order traversal algorithm? (solution)

39. Traverse a binary tree in Postorder traversal without recursion (solution)

40. Print all leaves of a binary search tree? (solution)

41. Count many leaf nodes in a given binary tree? (solution)

42. In order traversal in given binary tree? (solution)

43. Print all nodes of given binary tree using inorder traversal without recursion (solution)

44. Check if a given binary tree is a binary search tree?  (solution)

45. Check if a binary tree is balanced or not? (solution)

46. Given a binary search tree, how do you check whether there are two nodes in it whose sum equals a given value? (solution)

47. Convert a binary search tree to a sorted doubly-linked list. you are only allowed to change the target of pointers but cannot create any new nodes. (solution)

48. Given a binary search tree and a value k, How do you find a node in the binary search tree whose value is closest to k. (solution)


Data Structure and Algorithm Interview Questions



5. Stack and Queue Interview Questions

Stack and Queue are derived data structures, i.e., they are implemented either using an array or linked list, but they have unique features.

A queue is also known as FIFO data structure, which means First In First Out, i.e., the element added first will also be retrieved first.

The Queue allows you to add an element at the tail and retrieve a piece from the head, thus giving FIFO ordering.

On the other hand, Stack is a LIFO data structure, Last In First out, i.e., the element added first will be the last one to go.

This property is often used to convert a recursive algorithm into an iterative one. To learn more about Stack and Queue, I just want you to join an excellent course on Data Structure and Algorithms, like Deep Dive into Data Structure in Java.


For now, let's see some coding problems based on Java's Stack and Queue data structure.

49.  How do you implement a Queue using two Stacks? (answer)

50. Write a Java program to implement Stack using an array and linked list? (answer)

51.  How do you implement Stack using Queues? (answer)

52. Given a binary tree, return the postorder traversal of its nodes' values, using Stack? (answer)

53.  Difference between Stack and Queue data structure (answer)


If you need more such coding questions, you can take help from books like Cracking Code Interview, which presents 189+ Programming questions and solutions. An excellent book to prepare for programming job interviews in a short time.

100+ Data Structure and Algorithm Interview Questions Answers for Programmers



6. Search and Sort Algorithmic Interview Questions

Search and Sort based questions are the most popular algorithmic questions on any programming job interview. The interviewer often asks to implement various sorting algorithms, like the Bubble sort, Quicksort, merge sort, and asking to perform a binary search, etc.

Other algorithms questions, like collision detection, are not so popular, but they are exciting to solve and develop your grasp on creating your algorithms.

54. Implement the Bubble Sort algorithm? (solution)

55. Implement Iterative QuickSort Algorithm? (solution)

56. Implement the Bucket Sort Algorithm? (solution)

57. Implement the Counting Sort Algorithm? (solution)

58. Implement the Insertion Sort Algorithm? (solution)

59. Implement a Merge Sort Algorithm? (solution)

60. Implement the Radix Sort Algorithm? (solution)

61. Implement Sieve of Eratosthenes Algorithm to find Prime numbers? (solution)

62. Find GCD of two numbers using Euclid's Algorithm? (solution)

If you want to learn more about other algorithms, apart from search and sort, e.g., advanced String algorithms, I suggest you check out the Data Structure and Algorithm MasterClass Part 1 - 2 on Pluralsight.


Data Structure and Algorithm Interview Questions Programmers



7. Bit Manipulation Interview Questions

Last but not least, we will see some bits of manipulation-based questions. There is a famous saying that there are two kinds of programmers, one who knows binary and others who don't.

Yes, understanding binary and playing with bits is an essential skill for good programmers. Like Java and C++, every primary programming language provides both bitwise and bit shift operators, which you can use to manipulate bits and solve problems.

63. Check if a number is the power of two? (solution)

64. Check if a number is even or odd without using a modulo operator? (solution)

65. Subtract two binary numbers? (solution)

66. Find the number of 1s (the Set bit) in a given Bit Sequence? (solution)



8. Problem-Solving Coding Questions

So far, we have seen most of the programming questions based upon data structure and algorithms. Still, sometimes you will also find questions from Software design, Dynamic Programming, and other logical and tricky questions.

Here is a collection of some of those questions for your practice:

67. Swap two numbers without using the third variable? (solution)

68. Check if two rectangles overlap with each other? (solution)

69. Design a Vending Machine? (solution)

70. Implement an LRU Cache in your favorite programming language? (solution)

71. Check if a given number is a Palindrome? (solution)

72. Check if a given number is an Armstrong number? (solution)

73. Find all prime factors of a given number? (solution)

74. Check if a given number is positive or negative in Java? (solution)

75. Find the most significant prime factor of a given integral number? (solution)

76. Print all prime numbers up to a given number? (solution)

77. Print Floyd's triangle? (solution)

78. Print Pascal's triangle? (solution)

79. Calculate the square root of a given number? (solution)

80. Check if the given number is a prime number? (solution)

81. Add two numbers without using the plus operator in Java? (solution)

82. Check if a given number is even/odd without using an Arithmetic operator? (solution)

83. Print a given Pyramid structure? (solution)

84. Find the highest repeating world from a given file in Java? (solution)

85. Reverse a given Integer in Java? (solution)

86. Convert a decimal number to binary in Java? (solution)

87. Check if a given year is a leap year in Java? (solution)
'

That's all about some data structure and algorithm interview questions for programmers. Remember, it's one of the most important topics for all levels of programmers. Still, it's even more critical for freshers, computer science graduates, and junior programmers with 1 to 2 years of experience.

As you get more experienced, you start seeing fewer number data structure and algorithm questions, like a Java developer with 3 to 4 years of experience will see a lot less DS and also questions than freshers and a more senior Java developer, like someone with 5 to 6 years of experience will see even less.

Nonetheless, it's an important topic, and the programmer should not overlook it. I have found good companies like Google, Microsoft, Amazon; they use Data structure and algorithm questions all the time.

There are more, like interview questions based upon Dynamic Programming and backtracking on the algorithmic front, which I have not shared here, but I'll add it sometime later. If you come across any good data structure and algorithm questions, don't feel shy to share with us.


Some Useful Resources for Coding Interviews:

Thanks a lot for reading this article so far. If you like these frequently asked data structures and Algorithm  Interview questions, please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

All the best for your interview!!

P. S. - And If you are serious about a Coding Interview, you can also take Master the Coding Interview: Big Tech (FAANG) Interviews course on Udemy to prepare for the top tech companies like Coursera, Adobe, Dropbox, Grammarly, Uber, Quora, Evernote, Twitch, etc.

6 comments:

  1. Another list of good questions. Try it.
    https://medium.com/@rathor.rajeev/algorithm-and-data-structure-interview-question-1187cd5da6d8

    ReplyDelete
  2. These questions are really helpful. Thanks for sharing

    ReplyDelete
    Replies
    1. Thanks, I am thinking to add more Dynamic Programing questions and increase the list to 150 questions, what do you guys think? which topics should I add?

      Delete
    2. Yes Please. It would be great help

      Delete
    3. Great, I am working on Dynamic programming coding problem solution nowadays, I have also shared a list of common Dynamic programming problems here, you can take a look.

      Delete
  3. thanks for the article

    ReplyDelete

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