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 e.g. 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 been also involved in interviewing C++ developer. One difference which 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 also happens to be a good developer and coder.

I firmly believe that interview teaches you a lot in a very 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 than 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 then you can also take help from a good book our course like

##

For the sake of clarity and focus, I have categorized these data structure and algorithmic questions into various sub-category 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 and slowly progressing to the topic which you want to improve.

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

In this category, you will find questions which 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:

Print duplicate characters from String? (solution)

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

Print first non repeated character from String? (solution)

Reverse a given String using recursion? (solution)

Check if a String contains only digits? (solution)

Find duplicate characters in a String? (solution)

Count a number of vowels and consonants in a given String? (solution)

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

Find all permutations of String? (solution)

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

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

Check if 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

You can only access the array using index and Java also doesn't valid index check and if you try to access an array 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:

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

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

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

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

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

Remove duplicates from given array in Java? (solution)

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

Remove duplicates from an array in place? (solution)

Reverse an array in place in Java? (solution)

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

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

Transpose a Matrix? (solution)

Add or subtract two Matrices? (solution)

Multiply two Matrices in Java? (solution)

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

If you need more advanced questions based upon array then you can see also see

A linked list is another important data structure from interview point of view, here are some of the frequently asked linked list questions from programming interviews:

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

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

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

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

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

Reverse a linked list? (solution)

Reverse a singly linked list without recursion? (solution)

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

Find the sum of two linked list using Stack? (program)

If you need more interview questions based upon linked list then you can also refer to this list of 30 linked list questions.

###

the tree data structure is another popular data structure in programming interviews. It has several variants e.g. a binary tree, binary 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:

Implement a binary search tree? (solution)

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

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

Implement Post-order traversal algorithm? (solution)

Traverse a binary tree in Post order traversal without recursion (solution)

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

Count a number of leaf nodes in a given binary tree? (solution)

In order traversal in given binary tree? (solution)

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

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

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

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

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

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)

###

Stack and Queue are derived data structure 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 which will be added first will also be retrieved first.

The queue allows you to add an element at the tail and retrieve an element 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 which will be 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 you to join a good course on Data Structure and Algorithms e.g. Deep Dive into Data Structure in Java.

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

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

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

3) How do you implement Stack using Queues? (answer)

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

5) Difference between Stack and Queue data structure (answer)

If you need more such coding questions you can take help from books like

Other algorithms questions e.g. collision detection are not so popular but they are very interesting to solve and develop your grasp on creating your algorithms.

Implement the Bubble Sort algorithm? (solution)

Implement Iterative QuickSort Algorithm? (solution)

Implement the Bucket Sort Algorithm? (solution)

Implement the Counting Sort Algorithm? (solution)

Implement the Insertion Sort Algorithm? (solution)

Implement a Merge Sort Algorithm? (solution)

Implement the Radix Sort Algorithm? (solution)

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

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 then I suggest you check out the

Yes, understand binary and playing with bits is an important skill for good programmers. Every major programming language e.g. Java and C++ provide both bitwise and bit shift operators, which you can use to manipulate bits and solve problems.

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

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

Subtract two binary numbers? (solution)

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

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

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

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

Design a Vending Machine? (solution)

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

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

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

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

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

Find the largest prime factor of a given integral number? (solution)

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

Print Floyd's triangle? (solution)

Print Pascal's triangle? (solution)

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

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

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

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

Print a given Pyramid structure? (solution)

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

Reverse given Integer in Java? (solution)

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

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

'

That's all about some

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

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

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

Some

Thanks a lot for reading this article so far. If you like these Data Structure and Algorithm Interview questions then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

All the best for your interview!!

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 also happens to be a good developer and coder.

I firmly believe that interview teaches you a lot in a very 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 than 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 then you can also take help from a good book our course like

**Data Structures and Algorithms: Deep Dive**Using Java for quick reference.##
__Data Structures and Algorithm Interview Questions__

For the sake of clarity and focus, I have categorized these data structure and algorithmic questions into various sub-category 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 and slowly progressing to the topic which 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++ who doesn't use String.They are everywhere. From the C++ perspective, String is nothing but a null-terminated character array, but from Java perspective, String is a full-fledged object backed by character array.

In this category, you will find questions which 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:

Print duplicate characters from String? (solution)

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

Print first non repeated character from String? (solution)

Reverse a given String using recursion? (solution)

Check if a String contains only digits? (solution)

Find duplicate characters in a String? (solution)

Count a number of vowels and consonants in a given String? (solution)

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

Find all permutations of String? (solution)

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

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

Check if 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 toughest algorithm questions.### 2. Array and Matrix Interview Questions

Next to String is array, the second most frequently used data structure. Array stores element in a contiguous memory location and in C++ you can access array elements using pointer arithmetic as well, but in Java array is again an object, which provides just length method.You can only access the array using index and Java also doesn't valid index check and if you try to access an array 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:

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

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

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

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

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

Remove duplicates from given array in Java? (solution)

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

Remove duplicates from an array in place? (solution)

Reverse an array in place in Java? (solution)

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

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

Transpose a Matrix? (solution)

Add or subtract two Matrices? (solution)

Multiply two Matrices in Java? (solution)

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

If you need more advanced questions based upon array then you can see 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.**3. Linked List Interview Questions**A linked list is another important data structure from interview point of view, here are some of the frequently asked linked list questions from programming interviews:

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

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

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

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

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

Reverse a linked list? (solution)

Reverse a singly linked list without recursion? (solution)

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

Find the sum of two linked list using Stack? (program)

If you need more interview questions based upon linked list then you can also refer to this list of 30 linked list questions.

###
**4. Binary Tree Interview Questions**

the tree data structure is another popular data structure in programming interviews. It has several variants e.g. a binary tree, binary 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:

Implement a binary search tree? (solution)

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

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

Implement Post-order traversal algorithm? (solution)

Traverse a binary tree in Post order traversal without recursion (solution)

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

Count a number of leaf nodes in a given binary tree? (solution)

In order traversal in given binary tree? (solution)

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

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

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

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

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)

###
**5. Stack and Queue Interview Questions**

Stack and Queue are derived data structure 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 which will be added first will also be retrieved first.

The queue allows you to add an element at the tail and retrieve an element 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 which will be 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 you to join a good course on Data Structure and Algorithms e.g. Deep Dive into Data Structure in Java.

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

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

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

3) How do you implement Stack using Queues? (answer)

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

5) 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 solution. A good book to prepare for programming job interviews in a short time.### 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 e.g. Bubble sort, Quick sort, merge sort and asking to implement binary search, etc.Other algorithms questions e.g. collision detection are not so popular but they are very interesting to solve and develop your grasp on creating your algorithms.

Implement the Bubble Sort algorithm? (solution)

Implement Iterative QuickSort Algorithm? (solution)

Implement the Bucket Sort Algorithm? (solution)

Implement the Counting Sort Algorithm? (solution)

Implement the Insertion Sort Algorithm? (solution)

Implement a Merge Sort Algorithm? (solution)

Implement the Radix Sort Algorithm? (solution)

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

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 then I suggest you check out the

**Data Structure and Algorithm MasterClass Part 1 - 2**on Pluarlsight.### 7. Bit Manipulation Interview Questions

Last but not least, we will see some bit of manipulation based questions. There is a popular saying that, there are two kinds of programmers, one who knows binary and other's who don't.Yes, understand binary and playing with bits is an important skill for good programmers. Every major programming language e.g. Java and C++ provide both bitwise and bit shift operators, which you can use to manipulate bits and solve problems.

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

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

Subtract two binary numbers? (solution)

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 but sometime 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:

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

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

Design a Vending Machine? (solution)

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

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

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

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

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

Find the largest prime factor of a given integral number? (solution)

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

Print Floyd's triangle? (solution)

Print Pascal's triangle? (solution)

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

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

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

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

Print a given Pyramid structure? (solution)

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

Reverse given Integer in Java? (solution)

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

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, but it's even more important for freshers, computer science graduates and junior programmers with 1 to 2 years of experience.As you get more experienced, you started seeing less number of data structure and algorithm questions e.g. a Java developer with 3 to 4 years of experience will see the lot less DS and also questions then freshers and a more senior Java developer e.g. someone with 5 to 6 years of experience will see even less.

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

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

Some

**Useful Resources**for Coding Interviews:- Data Structures and Algorithms: Deep Dive Using Java
- 10 Books to Prepare Technical Programming/Coding Job Interviews
- 10 Courses to Prepare for Programming Job Interviews
- 10 Algorithm Books Every Programmer Should Read
- Top 5 Data Structure and Algorithm Books for Java Developers
- Top 5 Free Data Structure and Algorithm Courses
- 20+ String Algorithms Interview Questions
- Review these Java Interview Questions for Programmers
- 10 Algorithms Courses Junior Developer should join in 2019

Thanks a lot for reading this article so far. If you like these Data Structure and Algorithm Interview questions then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

All the best for your interview!!

Another list of good questions. Try it.

ReplyDeletehttps://medium.com/@rathor.rajeev/algorithm-and-data-structure-interview-question-1187cd5da6d8