Singly LinkedList

Data structures interview questions Java

One more suggestion I have to make is whenever you get some time, just read the Introduction to Algorithm by Thomas Cormen, if you have not read already. This book is the bible of algorithm and IMHO every programmer should read this book.

Data Structures and Algorithm Interview Questions for Java Programmers

This is a combined list of questions from various data structure e.g. array, linked list, stack or queue. It includes some coding questions as well, which gel with data structures.

Question 1: How to find middle element of linked list in one pass?

One of the most popular question from data structures and algorithm, mostly asked on telephonic interview. Since many programmer know that, in order to find length of linked list we need to first traverse through linked list till we find last node, which is pointing to null, and then in second pass we can find middle element by traversing only half of length. They get confused when interviewer ask him to do same job in one pass. In order to find middle element of linked list in one pass, you need to maintain two-pointer, one increment at each node while other increments after two nodes at a time, by having this arrangement, when first pointer reaches end, second pointer will point to middle element of linked list. See this trick to for more details.

Question 2: How to find if linked list has a loop ?

This question has bit of similarity with earlier algorithm and data structure interview question. I mean we can use two pointer approach to solve this problem. If we maintain two pointers, and we increment one pointer after processing two nodes and other after processing every node, we are likely to find a situation where both the pointers will be pointing to same node. This will only happen if linked list has loop.

Question 3 : How to find 3rd element from end in a linked list in one pass?

This is another frequently asked linked list interview question. This question is exactly similar If we apply same trick of maintaining two pointers and increment other pointer, when first has moved up to 3rd element, than when first pointer reaches to the end of linked list, second pointer will be pointing to the 3rd element from last in a linked list.

Question 4: In an integer array, there is 1 to 100 number, out of one is duplicate, how to find?

This is a rather simple data structures question, especially for this kind of. In this case you can simply add all numbers stored in array, and total sum should be equal to n(n+1)/2. Now just subtract actual sum to expected sum, and that is your duplicate number. Of course there is a brute force way of checking each number against all other numbers, but that will result in performance of O(n^2) which is not good. By the way this trick will not work if array have multiple duplicates or its not numbers forming arithmetic progression. Here is example of one way

Question 6 : How to reverse String in Java ?

This is one of my favorite question. Since String is one of the most important type of programming, you expect lot of question related to String any data structure interview. There are many ways to reverse Sting in Java or any other programming language, and interviewer will force you to solve this problem by using without API i.e. without using reverse method of StringBuffer. In follow-up he may ask to reverse String using recursion as learn reversing String using both loops and recursion in Java.

Question 7: Write a Java program to sort an array using Bubble Sort algorithm?

I have always send couple of questions from searching and sorting in data structure interviews. Bubble sort is one of the simplest sorting algorithm but if you ask anyone to implement on the spot it gives you an opportunity to gauge programming skills of a candidate. Secomplete solution of this datastrucutre interview question.

Question 8: What is the difference between Stack and Queue data structure?

One of the classical data structure interviews question. I guess every one know, No? Any way main difference is that Stack is LIFO(Last In First Out) data structure while Queue is a FIFO(First In First Out) data structure.

Question 9: How do you find duplicates in an array if there is more than one duplicate?

Sometime this is asked as follow-up question of earlier data structure interview question, related to finding duplicates in Array. One way of solving this problem is using structure. You can traverse through array, and store each number as key and number of occurrence as value. At the end of traversal you can find all duplicate numbers, for which occurrence is more than one. In Java if a number already exists ihen calling get(index) will return number otherwise it return null. this property can be used to insert or update numbers in HashMap.

Question 10 : What is difference between Singly Linked List and Doubly Linked List data structure?

This is another classical interview question on data structure, mostly asked on telephonic rounds. Main difference between singly linked list and doubly linked list is ability to traverse. In a single linked list, node only points towards next node, and there is no pointer to previous node, which means you can not traverse back on a singly linked list. On the other hand doubly linked list maintains two pointers, towards next and previous node, which allows you to navigate in both direction in any linked list.

Question 11 : Write Java program to print Fibonacci series ?

This is not a data structures question, but a programming one, which many times appear during data structure interview. Fibonacci series is a mathematical series, where each number is sum of previous two numbers e.g. 1, 1, 2, 3, 5, 8, 13, 21. Interviewer is often interested in two things, a function which returns nth number in Fibonacci series and solving this problem using recursion in Java. Though, its easy question, recursion part often confuses beginners. See this link to

Question 12: Write Java program to check if a number is a palindrome or not?

This is similar to previous question, not directly related to data structures, but quite popular along with other questions. A number is called palindrome, if reverse of number is equal to number itself. Interviewer ask to solve this problem without taking help from Java API or any open source library. Any way it’s simple question, you can use division operator (/) and remainder operator (%) to solve this question. Just remember, division operator can be used to get rid of last digit e.g. 1234/10 will give you 123, and modulus operator can give you last digit e.g. 1234%10 will return 4. By the way, here is

Question 13 : What is binary search tree?

This is a data structure question from Tree data structures. Binary Search Tree has some special properties e.g. left nodes contains items whose value is less than root, right sub tree contains keys with higher node value than root, and there should not be any duplicates in the tree. Apart from definition, interview can ask you to implement binary search tree in Java and questions on tree traversal e.g. IN order, preorder, and post order traversals are quite popular data structure question.

Question 14 : How to reverse linked list using recursion and iteration?

This is another good question on data structures. There are many algorithms to reverse linked list and you can search for them using google. I am thinking of writing another blog post to explain linked list reversal and will share with you later.

Question 15: Write a Java program to implement Stack in Java?

You can implement Stack by using array or linked list. This question expect you to implement standard method provided by stack data structure e.g. push and pop. Both push and pop should be happen at top of stack, which you need to keep track. It’s also good if you can implement utility methods like contains, isEmpty etc. By the way JDK has java.util.Stack class and you can check it’s code to get an idea. You can also chere Josh Bloch has explains how an incorrect implementation of stack can cause memory leak in Java.

Share this article

Related Posts

Latest Posts
Aptitude Exams
Aptitude Exams
With the Test for Foreign Students (TestAS)…
Engineering Aptitude
Engineering Aptitude
In the Engineering aptitude is play a…
Responsive Web design interview questions and answers
Responsive Web…
Dummy text No matter what type of programming…
Facilities Manager interview questions
Facilities Manager…
Emory and Henry students met with Strongwell’s…
Aptitude test for Customer Service
Aptitude test…
Do you want to raise the bar with your…
Featured posts
  • Top 100 Java interview questions
  • Director of sales interview questions
  • Oracle Apps technical interview questions
  • QA technical interview questions and answers
  • veterinary technician interview questions
  • Top 10 toughest interview questions
  • Top 10 hardest interview questions
  • 50 Top interview questions
  • Top 10 most Popular interview questions
Copyright © 2024 l All rights reserved.