Algorith Datastructure
Algorith Datastructure
source: http://placement.freshersworld.com/data-structures/33121964113
1.What is data structure?
A data structure is a way of organizing data that considers not only the items stored, but also
their relationship to each other. Advance knowledge about the relationship between data
items allows designing of efficient algorithms for the manipulation of data.
2.Minimum number of queues needed to implement the priority queue?
Two. One queue is used for actual storing of data and another for storing priorities.
3.What are the notations used in Evaluation of Arithmetic Expressions using prefix and postfix
forms?
Polish and Reverse Polish notations.
4.List out few of the Application of tree data-structure?
i)The manipulation of Arithmetic expression
ii)Symbol Table construction
iii)Syntax analysis.
5.What is the type of the algorithm used in solving the 8 Queens problem?
Backtracking
6.In RDBMS, what is the efficient data structure used in the internal storage representation?
B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes searching
easier. This corresponds to the records that shall be stored in leaf nodes.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
7. What is a spanning Tree?
A spanning tree is a tree associated with a network. All the nodes of the graph appear on the
tree once. A minimum spanning tree is a spanning tree organized so that the total edge weight
between nodes is minimized.
8. List out the areas in which data structures are applied extensively?
Compiler Design, Operating System, Database Management System, Statistical
analysis package, Numerical Analysis, Graphics, Artificial Intelligence, Simulation
9. Translate infix expression into its equivalent post fix expression: (A-B)*(D/E)
(A-B)*(D/E) = [AB-]*[DE/] = AB-DE/*
10. What are priority queues?
A priority queue is a collection of elements such that each element has been assigned a
priority.
11. What is a string?
A sequential array of characters is called a string.
12. What is Brute Force algorithm?
Algorithm used to search the contents by comparing each element of array is called Brute
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Force algorithm.
13. What are the limitations of arrays?
i)Arrays are of fixed size.
ii)Data elements are stored in continuous memory locations which may not be available
always.
iii)Adding and removing of elements is problematic because of shifting the locations.
14. How can you overcome the limitations of arrays?
Limitations of arrays can be solved by using the linked list.
15. What is a linked list?
Linked list is a data structure which store same kind of data elements but not in continuous
memory locations and size is not fixed. The linked lists are related logically.
16. What is a node?
The data element of a linked list is called a node.
17. What does node consist of?
Node consists of two fields:data field to store the element and link field to store the address
of the next node.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
23. What is the need for the header?
Header of the linked list is the first element in the list and it stores the number of elements in
the list. It points to the first data element of the list.
24. Define leaf?
In a directed tree any node which has out degree o is called a terminal node or a leaf.
25. What are the applications of binary tree?
Binary tree is used in data processing.
26. What are the different types of traversing?
The different types of traversing are
i)Pre-order traversal-yields prefix from of expression.
ii)In-order traversal-yields infix form of expression.
iii)Post-order traversal-yields postfix from of expression.
27. Define pre-order traversal?
i)Process the root node
ii)Process the left subtree
iii)Process the right subtree
28. Define post-order traversal?
i)Process the left subtree
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
somebody does not know how to use the features of a language.We can know the errors at
the time of compilation.
logical Error-It is due to the poor understanding of the requirement or problem.
Run time Error-The exceptions like divide a number by 0,overflow and underflow comes
under this.
34. What is mean by d-queue?
D-queue stands for double ended queue. It is a abstract data structure that implements a
queue for which elements can be added to front or rear and the elements can be removed
from the rear or front. It is also called head-tail linked list
35. What is AVL tree?
Avl tree is self binary tree in which balancing factor lie between the -1 to 1.It is also known as
self balancing tree.
36. what is binary tree?
Binary tree is a tree which has maximum no. of childrens either 0 or 1 or 2. i.e., there is at the
most 2 branches in every node.
37. What is the difference between a stack and a Queue?
Stack – Represents the collection of elements in Last In First Out order. Operations includes
testing null stack, finding the top element in the stack, removal of top most element and
adding elements on the top of the stack.
Queue - Represents the collection of elements in First In First Out order.Operations include
testing null queue, finding the next element, removal of elements and inserting the elements
from the queue.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Insertion of elements is at the end of the queue.Deletion of elements is from the beginning
of the queue
38. What actions are performed when a function is called?
i)arguments are passed
ii)local variables are allocated and initialized
iii)transferring control to the function
39. What is precision?
Precision refers the accuracy of the decimal portion of a value. Precision is the number of
digits allowed after the decimal point.
40. What do you mean by overflow and underflow?
When new data is to be inserted into the data structure but there is no available space i.e.free
storage list is empty this situation is called overflow.When we want to delete data from a data
structure that is empty this situation is called underflow.
Data Structure Advanced updated on Sep 2019
1. Is it possible to find a loop in a Linked list ?
a. Possilbe at O(n)
b. Not possible
c. Possible at O(n^2) only
d. Depends on the position of loop
Solution: a. Possible at O(n)
Have two pointers say P1 and P2 pointing to the first node of the list.
Start a loop and Increment P1 once and P2 twice in each iteration. At any point of time if
P1==P2 then there is a loop in that linked list. If P2 reaches NULL (end of linked list) then no
loop exists.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
2. Two linked lists L1 and L2 intersects at a particular node N1 and from there all other nodes
till the end are common. The length of the lists are not same. What are the possibilities to
find N1?.
a. Solution exist for certain cases only
b. No linear solution exist
c. Linear solution is possible
d Only Non-linear solution exist.
Solution: c. Linear solution is possible
Have two pointers say P1 pointing to the first node of L1 and P2 to that of L2. Traverse through
both the lists. If P1 reaches L1’s last node, point it to the first node of L2 and continue
traversing. Do the same thing for P2 when it reaches L2’s last node. (By doing this, we are
balancing the difference in the length between the linked lists. The shorter one will get over
soon and by redirecting to longer list’s head, it will traverse the extra nodes also.) Finally they
will Meet at the Intersection node.
3. void PrintTree (Tree T)
{
if (T != NULL)
{
PrintTree (T-> Left);
PrintElement (T-> Element);
PrintTree (T->Right);
}
}
The above method ‘PrintTree’ results in which of the following traversal
a Inorder
b. Preorder
c. Postorder
d. None of the above
Solution: a. Inorder
Inorder:
void PrintTree (Tree T)
{
if (T != NULL)
{
PrintTree (T-> Left);
PrintElement (T-> Element);
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
PrintTree (T->Right);
}
}
For preorder use this order
PrintElement (T-> Element);
PrintTree (T-> Left);
PrintTree (T->Right);
For postorder use this order
PrintTree (T-> Left);
PrintTree (T->Right);
PrintElement (T-> Element);
4. Given a Binary Search Tree (BST), print its values in ascending order.
a. Perform Depth first traversal
b. Perform Breadth first traversal
c. Perform Postorder traversal
d. Perform Inorder traversal
Solution: d. Perform Inorder traversal
It is the properfy of BST and Inorder traversal.
5. Is it possible to implement a queue using Linked List ?. Enqueue & Dequeue should be O(1).
a. Not possible to implement.
b Only Enqueue is possible at O(1).
c. Only Dequeue is possible at O(1).
d. Both Enqueue and Dequeue is possible at O(1)
Solution: d. Both Enqueue and Dequeue is possible at O(1)
Have two pointers H pointing to the Head and T pointing to the Tail of the linked list. Perform
enqueue at T and perform dequeue at H. Update the pointers after each operations
accordingly.
6. Given a Tree, is it possible to find the greatest and least among leaves in linear time?.
a. Solution depends on the tree structure
b.Linear solution exist
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
a. Solution need 2 extra spaces
b. Linear solution is possible without using any extra space
c No linear solution exist
d Solution need 1 extra space
Solution:b. Linear solution is possible without using any extra space
Perform an inorder traversal. Once you find K1 print it and continue traversal now, print all
other traversed elements until you reach K2.
Note: If K1 == K2 stop once you find K1.
10. How many stacks are required to implement a Queue.
a. One
b. Two
c. Three
d. Two + one extra space.
Solution:b Two
Have two stacks S1 and S2.
For Enqueue, perform push on S1.
For Dequeue, if S2 is empty pop all the elements from S1 and push it to S2. The last element
you popped from S1 is an element to be dequeued. If S2 is not empty, then pop the top
element in it.
DATA STRUCTURES updated on Sep 2019
DATA STRUCTURES:
1. Is it possible to find a loop in a Linked list ?
a. Possilbe at O(n)
b. Not possible
c. Possible at O(n^2) only
d. Depends on the position of loop
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
}
}
The above method PrintTree results in which of the following traversal
a Inorder
b. Preorder
c. Postorder
d. None of the above
Solution: a. Inorder
Inorder:
void PrintTree (Tree T)
{
if (T != NULL)
{
PrintTree (T-> Left);
PrintElement (T-> Element);
PrintTree (T->Right);
}
}
For preorder use this order
PrintElement (T-> Element);
PrintTree (T-> Left);
PrintTree (T->Right);
For postorder use this order
PrintTree (T-> Left);
PrintTree (T->Right);
PrintElement (T-> Element);
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
4. Given a Binary Search Tree (BST), print its values in ascending order.
a. Perform Depth first traversal
b. Perform Breadth first traversal
c. Perform Postorder traversal
d. Perform Inorder traversal
Solution: d. Perform Inorder traversal
It is the properfy of BST and Inorder traversal.
5. Is it possible to implement a queue using Linked List ?. Enqueue & Dequeue should be O(1).
a. Not possible to implement.
b Only Enqueue is possible at O(1).
c. Only Dequeue is possible at O(1).
d. Both Enqueue and Dequeue is possible at O(1)
Solution: d. Both Enqueue and Dequeue is possible at O(1)
Have two pointers H pointing to the Head and T pointing to the Tail of the linked list. Perform
enqueue at T and perform dequeue at H. Update the pointers after each operations
accordingly.
6. Given a Tree, is it possible to find the greatest and least among leaves in linear time?.
a. Solution depends on the tree structure
b.Linear solution exist
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
d. 2 stacks can be implemented if the given condition is applied only for 1 stack.
Solution:c. 2 stacks can be implemented for the given condition
Start 1st stack from left (1st position of an array) and 2nd from right (last position say n).
Move 1st stack towards right( i.e 1,2,3 ...n) and 2nd towards left (i.e n,n-1,n-2...1).
9. Given two keys K1 & K2, write an algorithm to print all the elements between them with
K1<=K2 in a BST.
a. Solution need 2 extra spaces
b. Linear solution is possible without using any extra space
c No linear solution exist
d Solution need 1 extra space
Solution:b. Linear solution is possible without using any extra space
Perform an inorder traversal. Once you find K1 print it and continue traversal now, print all
other traversed elements until you reach K2.
Note: If K1 == K2 stop once you find K1.
10. How many stacks are required to implement a Queue.
a. One
b. Two
c. Three
d. Two + one extra space.
Solution:b Two
Have two stacks S1 and S2.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
memory to store that data is not available contiguously. In this case we cannot use array.
Hence, we go for a linked list. Since each node is connected using link, it is not necessary that
memory has to be contiguous.
Also, some of the major differences between a Linked List and an array are given below. For
more, click here.
Arrays Linked List
Array elements can be accessed randomly using the array index. Random accessing is not
possible in linked lists. The elements will have to be accessed sequentially.
Data elements are stored in contiguous locations in memory. New elements can be
stored anywhere and a reference is created for the new element using pointers.
4) Write the syntax in C to create a node in the singly linked list.
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
5) What is the use of a doubly-linked list when compared to that of a singly linked list?
data structures interview questions
In a singly linked list, we have only forward links. Hence, we cannot traverse the linked list in
a backward manner. In order to overcome this, we go for a doubly linked list. In a doubly
linked list, each node has three fields such as previous, data and next field and has two links
such as a forward and backward link. The previous field of the first node and the address field
of the last node is NULL. The previous field of the second node has the address of the first
node and so on.
Also, accessing of elements can be done more efficiently incase of a doubly linked list.
6) What is the difference between an Array and Stack?
Stack Data Structure:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Size of the stack keeps on changing as we insert and delete the element
Stack can store elements of different data type
Array Data Structure:
Size of the array is fixed at the time of declaration itself
Array stores elements of similar data type
7) What are the minimum number of Queues needed to implement the priority queue?
Two queues are needed. One queue is used to store the data elements, and another is used
for storing priorities. Check out the implementation of a Priority Queue.
8) What are the different types of traversal techniques in a tree?
There are three main traversals of a tree such as In-order, Pre-order, Post-order.
Algorithm of In-order traversal:
Traverse the left sub-tree
Visit the root
Traverse the right sub-tree
Algorithm of Pre-order traversal:
Visit the root
Traverse the left sub-tree
Traverse the right sub-tree
Algorithm of Post-order traversal:
Traverse the left sub-tree
Traverse the right sub-tree
Visit the root
9) Why it is said that searching a node in a binary search tree is efficient than that of a simple
binary tree?
When searching any node in binary search tree, the value of the target node is compared with
the parent node and accordingly either left sub tree or right sub tree is searched. So, one has
to compare only particular branches. Thus searching becomes efficient.
10) What are the applications of Graph DS?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Graphs are used in circuit networks where points of connection are drawn as vertices and
component wires become the edges of the graph, in transport networks where stations are
drawn as vertices and routes become the edges of the graph, in maps that draw
cities/states/regions as vertices and adjacency relations as edges, in program flow analysis
where procedures or modules are treated as vertices and calls to these procedures are drawn
as edges of the graph.
11) Can we apply Binary search algorithm to a sorted Linked list?
No, we cannot apply the binary search algorithm to a sorted linked list because finding the
index of the middle element is difficult.
12) When can you tell that a Memory Leak will occur?
A memory leak occurs when a program does not free a block of memory allocated
dynamically.
13) How will you check if a given Binary Tree is a Binary Search Tree or not?
To know that you need to check the inorder traversal of a binary tree. If it is sorted, then the
binary tree is BST. Click here to know how to perform inorder traversal.
14) Which data structure is ideal to perform Recursion operation and why?
Stack is the most ideal for recursion operation. This is mainly because of its LIFO (Last In First
Out) property, it remembers the elements & their positions, so it exactly knows which one to
return when a function is called.
15) What are some of the most important applications of a Stack?
Some of the important applications are given below. Check them out to know the detailed
code & explanation.
Balanced parenthesis checker
Redundant braces
Infix to postfix using a stack
Infix to prefix using a stack
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
16) Convert the below given expression to its equivalent Prefix And Postfix notations.
((A + B) * C – (D – E) ^ (F + G))
Prefix Notation: ^ – * +ABC – DE + FG
postfix Notation: AB + C * DE – – FG + ^
Source: https://www.faceprep.in/data-structures-interview-questions/
Source: https://java2blog.com/data-structure-and-algorithm-interview-questions-in-
java/#Question_102_What_is_Memoization
I have been posting data structure and algorithms interview questions on various topics such
as Array, Queue, Stack, Binary tree, LinkedList, String, Number, ArrayList, etc. So I am
consolidating a list of programs to create an index post. I will keep adding links to this post
whenever I add new programs. These are frequently asked Data Structure and algorithm
interview questions.
Table of Contents
Stack
Question 1: Implement a stack using array.
Question 2: Implement a stack using Linked List :
Question 3: Implement a stack using two queues .
Question 4 : Sort an stack using another stack
Queue
Question 5: Implement Queue using Array in java.
Question 6: Implement a stack using two queues .
Linked List
Question 7 : Implement singly linked list in java.
Question 8: How to reverse linked list in java.
Question 9: How to find middle element of linked list.
Question 10 : How to find nth element from end of linked list .
Question 11 : How to detect a loop in linked list. If linked list has loop,
find the start node for the loop.
Question 12: How to check if linked list is palindrome or not?
Question 13 : Find intersection of two linked lists?
Question 14 : How to reverse a linked list in pairs?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
to bottom right of MxN matrix. You can either move down or right.
Question 94 : Edit Distance Problem in java
Question 95: Coin change problem in java
Question 96 : Minimum number of jumps to reach last index
Miscellaneous
Question 97 : What is an algorithm and how to calculate complexity
of algorithms.
Question 98 : Implement trie data structure in java.
Question 99 : Count Factorial Trailing Zeroes in java.
Question 100 : Largest Rectangular Area in a Histogram.
Question 101 : Check for balanced parentheses in an expression in
java.
Question 102 : What is Memoization.
Join Our News Letter – Stay Updated
If you want to practice and improve data structure and algorithm programs, this post will be
very helpful to you. I will recommend you to try it yourself first and then check the solution.
Stack
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 1: Implement a stack using array.
You need to implement Stack using array. You need to write push and pop methods to
demonstrate Stack behavior(Last In First Out).
Solution : Java Program to implement stack using array.
Question 2: Implement a stack using Linked List :
You need to implement Stack using Linked List. You need to write push and pop methods to
demonstrate Stack behavior(Last In First Out).
Solution : Java Program to implement stack using Linked List
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 5: Implement Queue using Array in java.
You need to use array to implement queue.
Solution : Implement Queue using Array in java
Question 6: Implement a stack using two queues .
You need to use Linked list to implement queue.
Solution : Java Program to implement queue using linked list
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Linked List
Question 7 : Implement singly linked list in java.
You need to implement singly linked list data structures.You need to write simple program to
demonstrate insert , delete operations.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Doubly Linked List in java
Array
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 16 : Write java Program to Find Smallest and Largest Element in an Array.
You are given an integer array containing 1 to n but one of the number from 1 to n in the array
is missing. You need to provide an optimum solution to find the missing number. Number can
not be repeated in the arry.
For example:
1
2 int[] arr1={7,5,6,1,4,2};
3 Missing numner : 3
4 int[] arr2={5,3,1,2};
5 Missing numner : 4
6
Solution : Java Program to Find Smallest and Largest Element in an Array
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 int arr[]={16,19,21,25,3,5,8,10};
3 Minimum element in the array : 3
4
If you note that array is sorted and rotated. You need to i an element in above array in o(log
n) time complexity.
Solution : Find minimum element in a sorted and rotated array
Question 20: Find second largest number in an array
You are given an sorted and rotated array as below:
For example:
1
2 int[] arr1={7,5,6,1,4,2};
3 Second largest element in the array : 6
4
Solution : java program to find second largest number in an array.
Question 21 : Find the number occurring odd number of times in an array
You are given a array of integer. All numbers occur even number of times except one. You
need to find the number which occurs odd number of time. You need to solve it with o(n)
time complexity and o(1) space complexity.
For example:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 int array[] = new int[]{20, 40, 50, 40, 50, 20, 30, 30, 50, 20, 40, 40, 20};
3 Number which occurs odd number of times is : 50
4
Solution : java program to find number occurring odd number of times in an array.
Question 22 : Find minimum number of platforms required for railway station
You are given arrival and departure time of trains reaching to a particular station. You need
to find minimum number of platforms required to accommodate the trains at any point of
time.
For example:
1
2 arrival[] = {1:00, 1:40, 1:50, 2:00, 2:15, 4:00}
3 departure[] = {1:10, 3:00, 2:20, 2:30, 3:15, 6:00}
4 No. of platforms required in above scenario = 4
5
Please note that arrival time is in chronological order.
Solution : Find minimum number of platforms required for railway station.
Question 23 : Find a Pair Whose Sum is Closest to zero in Array
Given array of +ve and -ve integers ,we need to find a pair whose sum is closed to Zero in
Array.
For example:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 array[]={1,3,-5,7,8,20,-40,6};
3 The pair whose sum is closest to zero : -5 and 6
4
Solution : Find a Pair Whose Sum is Closest to zero in Array in java.
Question 24 : Given a sorted array and a number x, find the pair in array whose sum is
closest to x
Given a sorted array, we need to find a pair whose sum is closed to number X in Array.
For example:
1
2 array[]={-40,-5,1,3,6,7,8,20};
3 The pair whose sum is closest to 5 : 1 and 3
4
Solution : Find a Pair Whose Sum is Closest to X in Array in java.
Question 25 : Find all pairs of elements from an array whose sum is equal to given number
Given a array,we need to find all pairs whose sum is equal to number X.
For example:
1
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
2 Input :
3 int[] arr = {0,0,0,1,1,1,1};
4 output : 4
5 int[] arr = {0,0,1};
6 output : 1
7
Solution : Count 1’s in sorted Binary Array.
Question 36 : Find first repeating element in an array of integers.
Find the first repeating element in array of integers.
For example :
1
2 Input :
3 Input: array[] = {10, 7, 8, 1, 8, 7, 6}
4 Output: 7 [7 is the first element actually repeats]
5
Solution : Find first repeating element in an array of integers.
Question 37 : Check if Array Elements are Consecutive.
Given an array, we need to check if array contains consecutive elements.
For example :
1
2 Input: array[] = {5, 3, 4, 1, 2}
3 Output: true
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
4 As array contains consecutive elements from 1 to 5Input: array[] = {47, 43, 45, 44, 46}
5 Output: true
6 As array contains consecutive elements from 43 to 47Input: array[] = {6, 7, 5, 6}
7 Output: false
8 As array does not contain consecutive elements.
9
Solution : Check if Array Elements are Consecutive.
Question 38 : Permutations of array in java.
Given array of distinct integers, print all permutations of the array.
For example :
1
2 array : [10, 20, 30]
3
4 Permuations are :
5
6 [10, 20, 30]
7 [10, 30, 20]
8 [20, 10, 30]
9 [20, 30, 10]
10 [30, 10, 20]
11 [30, 20, 10]
12
Solution : Permutations of array in java.
Question 39 : Rotate an array by K positions.
For example :
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 N=6 and k=2
3 If Arr[] = {1, 2, 3, 4, 5, 6} and k=2
4 then rotated array will be {5, 6, 1, 2, 3, 4}
5
Solution : Rotate an array by K positions.
Question 40 : Stock Buy Sell to Maximize Profit.
Given an array of integers representing stock price on single day, find max profit that can be
earned by 1 transaction.
So you need to find pair (buyDay,sellDay) where buyDay < = sellDay and it should maximise
the profit.
For example :
1
2 int arr[]={14, 12, 70, 15, 99, 65, 21, 90};
3 Max profit can be gain by buying at 1th day(0 based indexing) and sell at 4th day.
4 Max profit = 99-12 =87
5
Solution : Stock Buy Sell to Maximize Profit.
Question 41 : Find maximum difference between two elements such that larger element
appears after the smaller number.
Given array of integers, find Maximum difference between two elements such that larger
element appears after the smaller number
For example :
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 int arr[]={14, 12, 70, 15, 95, 65, 22, 30};
3 Max Difference =95-12 = 83
4
Solution : Maximum difference between two elements such that larger element appears
after the smaller number.
Question 42 : Search in a row wise and column wise sorted matrix.
Given row wise and column wise sorted matrix ,we need to search element with minimum
time complexity.
Solution : Search in a row wise and column wise sorted matrix.
Question 43 : Largest sum contiguous subarray.
Largest sum contiguous subarray is the task of finding the contiguous subarray within a one-
dimensional array of numbers which has the largest sum.
For example :
1
2 for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4,
3
Solution : Largest sum contiguous subarray.
Question 44 : Find the Contiguous Subarray with Sum to a Given Value in an array.
Given an array of positive integer and given value X, find Contiguous sub array whose sum is
equal to X.
For example :
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 arr[]={14, 12, 70, 15, 99, 65, 21, 90};
3 X =97.
4 Sum found between index 1 to 3
5 Elements are 12, 17 and 15
6
Solution : Find the Contiguous Subarray with Sum to a Given Value in an array.
Question 45 : Longest Common Prefix in an array of Strings in java.
Given an array of positive integer and given value X, find Contiguous sub array whose sum is
equal to X.
For example :
1
2 String[] strArr={"java2blog","javaworld","javabean","javatemp"};
3 So Longest common prefix in above String array will be “java” as all above string starts with “java”.
4
Solution : Longest Common Prefix in an array of Strings in java.
Question 46 : Find all subsets of set (power set) in java.
Given a set of distinct integers, arr, return all possible subsets (the power set).
For example :
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1
2 Input: nums = [1,2,3]
3 Output:
4 [
5 [3],
6 [1],
7 [2],
8 [1,2,3],
9 [1,3],
10 [2,3],
11 [1,2],
12 []
13 ]
14
Solution : Find all subsets of set in java.
String
Question 47 : How to reverse a String in java? Can you write a program without using any
java inbuilt methods?
Solution: There are many ways to do it, some of them are:
▪ Using for loop
▪ Using recursion
▪ Using StringBuffer
Please refer to the solution at reverse a String in java
Question 48 : Write a java program to check if two Strings are anagram in java?
Solution: Two string are anagrams if they have same characters but in different order. For
example: Angel and Angle are anagrams
There are few ways to check if Strings are anagrams. Some of them are:
1 Using String methods
2 Using array.sort
Check solution at check if two Strings are anagram in java.
Question 49 : Write a program to check if String has all unique characters in java?
Solution: Here are some ways to check if String contains all unique characters
▪ By using HashSet
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
remaining String recursively. Please find complete solution at how to find all permutations
of String in java.
Binary Tree
Question 56 : How can you traverse binary tree?
There are three ways to traverse binary tree.
▪ PreOrder
▪ InOrder
▪ PostOrder.
Question 57 : Write an algorithm to do level order traversal of binary tree?
You need to write java program to do level order traversal of binary tree. You can use queue
data structure to do level order traversal.
Solution : Level order traversal of binary tree.
Question 58 : Write an algorithm to do spiral order traversal of binary tree?
You need to write java program to do spiral level order traversal of binary tree
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Sprial order or zigzag traversal of binary tree.
Question 59 : How can you print leaf nodes of binary tree?
You need to write java program to print all leaf nodes of binary tree.
Leaf nodes for above binary tree will be 5 , 30 , 55 ,70
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Print all paths from root to leaf in binary tree.
Question 62 : How to find level of node in binary tree
Given a node, you need to find level of a node. For example : Level of node will 3 for node 70
used in Question 14.
Solution: Find level of node in binary tree.
Question 63 : How to find maximum element in binary tree.
You need to write a java program to find maximum element in binary tree.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution: Program to find LCA in binary tree.
Question 65 : How to do boundary traversal of binary tree.
Write a java program to do boundary traversal of binary tree as shown in below image.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Boundary traversal of binary tree.
Question 66 : How to print vertical sum of binary tree?
You need to find sum of nodes which lies in same column.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : How to print vertical sum of binary tree.
Question 67 : Count subtrees with Sum equal to target in binary tree?
Given a Binary tree and an integer. You need to find the number of subtrees having the sum
of all of its nodes equal to given Integer, that is, Target sum.
Solution : Count subtrees with Sum equal to target in binary tree.
Binary Search tree
Question 68 : What is binary search tree?
Binary search tree is a special type of binary tree which have following properties.
▪ Nodes which are smaller than root will be in left subtree.
▪ Nodes which are greater than root will be right subtree.
▪ It should not have duplicate nodes
▪ Both left and right subtree also should be binary search tree.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 69 : Can you write algorithm to insert a node in binary search tree.
Solution : Insert node in binary search tree
Question 70 : Can you write algorithm to delete a node in binary search tree.
Solution : Delete node in binary search tree
Question 71 : How can you find minimum and maximum elements in binary search tree?
Solution : Leftmost and rightmost nodes of binary search tree are minimum and maximum
nodes respectively
Minimum and maximum elements in binary search tree.
Question 72 : How to find lowest common ancestor(LCA) in binary search tree.
You need to write a program to find LCA in binary search tree.
Solution: Program to find LCA in binary search tree.
Question 73 : Find inorder successor in a Binary search Tree
You need to write a program to find inorder successor in a Binary search tree.
Solution: Inorder Successor in a Binary Search Tree
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 74 : Convert sorted array to balanced BST
Solution: Convert sorted sorted array to balanced BST
Question 75 : Convert sorted Linked List to balanced BST
Solution: Convert sorted Linked List to balanced BST
Question 76 : Check if a binary tree is binary search tree or not in java
Solution: Check if a binary tree is binary search tree or not in java
Sorting
Question 77 : Write an algorithm to implement bubble sort?
Solution : Bubble sort in java
Question 78 : Write an algorithm to implement insertion sort sort?
Solution : Insertion sort in java
Question 79 : Write an algorithm to implement selection sort sort?
Solution : Selection sort in java
Question 80 : Can you write algorithm for merge sort and also do you know complexity of
merge sort?
Solution : Merge sort in java
Question 81 : Do you know how to implement Heap sort?
Solution : implement Heap sort in java
Question 82 : Implement quick sort in java?
Solution : implement Quick sort in java
Question 83 : Implement shell sort in java?
Solution : implement Shell sort in java
Question 84 : Implement Counting sort in java?
Solution : implement Counting sort in java
Question 85 : What is binary search? Can you write an algorithm to find an element in
sorted array using binary search?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Bellman ford algorithm in java
Question 90 : Explain Kruskal’s algorithm for finding minimum spanning tree
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Kruskal’s algorithm
Dynamic Programming
Question 91 : Given two String, find longest common substring.
Solution: Longest common substring in java.
Question 92 : Given two Strings A and B. Find the length of the Longest Common
Subsequence (LCS) of the given Strings.
Solution: Longest common subsequence in java
Question 93 : Given a matrix, we need to count all paths from top left to bottom right
of MxN matrix. You can either move down or right.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution: Count all paths in matrix
Question 94 : Edit Distance Problem in java
Given two strings string1 and string2, String1 is to be converted into String2 with the given
operations available in the minimum number of steps. Using any one of the given operations
contributes to the increment of steps by one.
Allowed Operations are :
(i) Remove : This operation allows the Removal any one character from String.
(ii) Insert : This operation allows the Insertion of one character at any spot in the String.
(iii) Replace : This operation allows the replacement of any one character in the string with
any other character.
Solution: Edit distance problem in java.
Question 95: Coin change problem in java
Given an Amount to be paid and the currencies to pay with. There is infinite supply of every
currency using combination of which, the given amount is to be paid. Print the number of
ways by which the amount can be paid.
Solution: Coin change problem in java
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Solution : Implement trie data structure in java.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Data structure refers to the way data is organized and manipulated. It seeks to find ways to
make data access more efficient. When dealing with the data structure, we not only focus
on one piece of data but the different set of data and how they can relate to one another in
an organized manner.
The key difference between both the data structure is the memory area that is being
accessed. When dealing with the structure that resides the main memory of the computer
system, this is referred to as storage structure. When dealing with an auxiliary structure, we
refer to it as file structures.
A binary search is an algorithm that is best applied to search a list when the elements are
already in order or sorted. The list is searched starting in the middle, such that if that middle
value is not the target search key, it will check to see if it will continue the search on the
lower half of the list or the higher half. The split and search will then continue in the same
manner.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
A linked list is a sequence of nodes in which each node is connected to the node following it.
This forms a chain-like link for data storage.
To reference all the elements in a one -dimension array, you need to use an indexed loop,
So that, the counter runs from 0 to the array size minus one. In this manner, You can
reference all the elements in sequence by using the loop counter as the array subscript.
Data structures are essential in almost every aspect where data is involved. In general,
algorithms that involve efficient data structure is applied in the following areas: numerical
analysis, operating system, A.I., compiler design, database management, graphics, and
statistical analysis, to name a few.
7) What is LIFO?
LIFO is a short form of Last In First Out. It refers how data is accessed, stored and retrieved.
Using this scheme, data that was stored last should be the one to be extracted first. This
also means that in order to gain access to the first data, all the other data that was stored
before this first data must first be retrieved and extracted.
8 ) What is a queue?
A queue is a data structure that can simulate a list or stream of data. In this structure, new
elements are inserted at one end, and existing elements are removed from the other end.
A binary tree is one type of data structure that has two nodes, a left node, and a right node.
In programming, binary trees are an extension of the linked list structures.
10) Which data structures are applied when dealing with a recursive function?
Recursion, is a function that calls itself based on a terminating condition, makes use of the
stack. Using LIFO, a call to a recursive function saves the return address so that it knows
how to return to the calling function after the call terminates.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
A stack is a data structure in which only the top element can be accessed. As data is stored
in the stack, each data is pushed downward, leaving the most recently added data on top.
A binary search tree stores data in such a way that they can be retrieved very efficiently. The
left subtree contains nodes whose keys are less than the node’s key value, while the right
subtree contains nodes whose keys are greater than or equal to the node’s key value.
Moreover, both subtrees are also binary search trees.
Multidimensional arrays make use of multiple indexes to store data. It is useful when storing
data that cannot be represented using single dimensional indexing, such as data
representation in a board game, tables with data stored in more than one column.
It depends on where you intend to apply linked lists. If you based it on storage, a linked list
is considered non-linear. On the other hand, if you based it on access strategies, then a
linked list is considered linear.
Apart from being able to store simple structured data types, dynamic memory allocation can
combine separately allocated structured blocks to form composite structures that expand
and contract as needed.
FIFO stands for First-in, First-out, and is used to represent how data is accessed in a queue.
Data has been inserted into the queue list the longest is the one that is removed first.
An ordered list is a list in which each node’s position in the list is determined by the value of
its key component, so that the key values form an increasing sequence, as the list is
traversed.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Merge sort, is a divide-and-conquer approach for sorting the data. In a sequence of data,
adjacent ones are merged and sorted to create bigger sorted lists. These sorted lists are
then merged again to form an even bigger sorted list, which continues until you have one
single sorted list.
Null is a value, whereas Void is a data type identifier. A variable that is given a Null value
indicates an empty value. The void is used to identify pointers as having no initial size.
A linked list is an ideal data structure because it can be modified easily. This means that
editing a linked list works regardless of how many elements are in the list.
Pushing and popping applies to the way data is stored and retrieved in a stack. A push
denotes data being added to it, meaning data is being “pushed” into the stack. On the other
hand, a pop denotes data retrieval, and in particular, refers to the topmost data being
accessed.
A linear search refers to the way a target key is being searched in a sequential data
structure. In this method, each element in the list is checked and compared against the
target key. The process is repeated until found or if the end of the file has been reached.
The amount of memory to be allocated or reserved would depend on the data type of the
variable being declared. For example, if a variable is declared to be of integer type, then 32
bits of memory storage will be reserved for that variable.
The heap is more flexible than the stack. That’s because memory space for the heap can be
dynamically allocated and de-allocated as needed. However, the memory of the heap can at
times be slower when compared to that stack.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
A postfix expression is an expression in which each operator follows its operands. The
advantage of this form is that there is no need to group sub-expressions in parentheses or
to consider operator precedence.
Data abstraction is a powerful tool for breaking down complex data problems into
manageable chunks. This is applied by initially specifying the data objects involved and the
operations to be performed on these data objects without being overly concerned with how
the data objects will be represented and stored in memory.
Assuming that the data to be inserted is a unique value (that is, not an existing entry in the
tree), check first if the tree is empty. If it’s empty, just insert the new item in the root node.
If it’s not empty, refer to the new item’s key. If it’s smaller than the root’s key, insert it into
the root’s left subtree, otherwise, insert it into the root’s right subtree.
The selection sort is a fairly intuitive sorting algorithm, though not necessarily efficient. In
this process, the smallest element is first located and switched with the element at subscript
zero, thereby placing the smallest element in the first position.
The smallest element remaining in the subarray is then located next to subscripts 1 through
n-1 and switched with the element at subscript 1, thereby placing the second smallest
element in the second position. The steps are repeated in the same manner till the last
element.
In the case of signed numbers, the first bit is used to indicate whether positive or negative,
which leaves you with one bit short. With unsigned numbers, you have all bits available for
that number. The effect is best seen in the number range (an unsigned 8-bit number has a
range 0-255, while the 8-bit signed number has a range -128 to +127.
30) What is the minimum number of nodes that a binary tree can have?
A binary tree can have a minimum of zero nodes, which occurs when the nodes have NULL
values. Furthermore, a binary tree can also have 1 or 2 nodes.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Dynamic data structures are structures that expand and contract as a program runs. It
provides a flexible means of manipulating data because it can adjust according to the size of
the data.
Pointers that are used in linked list have various applications in the data structure. Data
structures that make use of this concept include the Stack, Queue, Linked List and Binary
Tree.
Most declarations do, with the exemption of pointers. Pointer declaration does not allocate
memory for data, but for the address of the pointer variable. Actual memory allocation for
the data comes during run-time.
When dealing with arrays, data is stored and retrieved using an index that refers to the
element number in the data sequence. This means that data can be accessed in any order.
In programming, an array is declared as a variable having a number of indexed elements.
35) What is the minimum number of queues needed when implementing a priority
queue?
The minimum number of queues needed in this case is two. One queue is intended for
sorting priorities while the other queue is used for actual storage of data.
There are many types of sorting algorithms: quick sort, bubble sort, balloon sort, radix sort,
merge sort, etc. Not one can be considered the fastest because each algorithm is designed
for a particular data structure and data set. It would depend on the data set that you would
want to sort.
Stack follows a LIFO pattern. It means that data access follows a sequence wherein the last
data to be stored when the first one to be extracted. Arrays, on the other hand, does not
follow a particular order and instead can be accessed by referring to the indexed element
within the array.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
A bubble sort is one sorting technique that can be applied to data structures such as an
array. It works by comparing adjacent elements and exchanges their values if they are out of
order. This method lets the smaller values “bubble” to the top of the list, while the larger
value sinks to the bottom.
A linked list typically has two parts: the head and the tail. Between the head and tail lie the
actual nodes. All these nodes are linked sequentially.
Selection sort works by picking the smallest number from the list and placing it at the front.
This process is repeated for the second position towards the end of the list. It is the simplest
sort algorithm.
A graph is one type of data structure that contains a set of ordered pairs. These ordered
pairs are also referred to as edges or arcs and are used to connect nodes where data can be
stored and retrieved.
The linear data structure is a structure wherein data elements are adjacent to each other.
Examples of linear data structure include arrays, linked lists, stacks, and queues. On the
other hand, a non-linear data structure is a structure wherein each data element can
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
connect to more than two adjacent data elements. Examples of nonlinear data structure
include trees and graphs.
An AVL tree is a type of binary search tree that is always in a state of partially balanced. The
balance is measured as a difference between the heights of the subtrees from the root. This
self-balancing tree was known to be the first data structure to be designed as such.
Doubly linked lists are a special type of linked list wherein traversal across the data elements
can be done in both directions. This is made possible by having two links in every node, one
that links to the next node and another one that connects to the previous node.
Huffman’s algorithm is used for creating extended binary trees that have minimum
weighted path lengths from the given weights. It makes use of a table that contains the
frequency of occurrence for each data element.
Fibonacci search is a search algorithm that applies to a sorted array. It makes use of a
divide-and-conquer approach that can significantly reduce the time needed in order to
reach the target element.
To find the target key in a linked list, you have to apply sequential search. Each node is
traversed and compared with the target key, and if it is different, then it follows the link to
the next node. This traversal continues until either the target key is found or if the last node
is reached.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Source: https://javarevisited.blogspot.com/2013/03/top-15-data-structures-algorithm-
interview-questions-answers-java-programming.html
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 a
telephonic interview. Since many programmers know that, in order to find the length of a
linked list we need to first traverse through the linked list till we find the last node, which is
pointing to null, and then in second pass we can find a middle element by traversing only
half of length.
They get confused when the interviewer asks him to do the same job in one pass i.e.
without traversing the linked list again.
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 the first pointer reaches the end, the second pointer will point to a
middle element of the linked list. See this trick to find middle element of linked list in a
single pass for more details.
Question 2: How to find if a linked list has a loop?
This question has a bit of similarity with the 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 the same node.
This will only happen if a linked list has a loop or cycle. You can check my article linked list
with cycles for more details.
Question 3: How to find the third element from the end in a linked list in one pass?
This is another frequently asked linked list interview question. This question is exactly
similar to find middle element of linked list in a single pass.
If we apply the same trick of maintaining two pointers and increment another pointer, when
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
first has moved up to the 3rd element, then when the first pointer reaches to the end of the
linked list, the second pointer will be pointing to the 3rd element from last in a linked list.
Sometimes, interviewers can also generalize this problem and ask him to find the kth
element from the tail, end or last. Just use the same logic, replace 3 with k and you can
solve the problem.
If you want to learn more about linked list, you can also check out Algorithms and Data
Structures - Part 1 and 2 courses on Pluralsight.
Btw, you would need a Pluralsight membership to access this course, which costs around
$29 monthly or $299 annually. I have one and I also suggest all developers have that plan
because Pluralsight is like NetFlix for Software developers.
It has more than 5000+ good quality courses on all latest topics. Since we programmers
have to learn new things every day, an investment of $299 USD is not bad.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Btw, it also offers a 10-day free trial without any obligation which allows you to watch 200
hours of content. You can watch this course for free by signing for that trial.
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 an array, and the 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 the performance of O(n^2) which is not good.
By the way, this trick will not work if an array has multiple duplicates or its not numbers
forming an arithmetic progression. Here is an example of one way to find the duplicate
number in the array.
Question 6: How to reverse String in Java?
This is one of my favorite questions. Since String is one of the most important types of
programming, you expect a 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 the
interviewer will force you to solve this problem by using without API i.e. without using
reverse() method of StringBuffer.
In the follow-up, he may ask to reverse String using recursion as well. See 3 ways to reverse
String in Java to learn to reverse 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 sent a couple of questions from searching and sorting in data structure
interviews. Bubble sort is one of the simplest sorting algorithms but if you ask anyone to
implement on the spot it gives you an opportunity to gauge the programming skills of a
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
candidate. See How to sort an array using Bubble Sort in Java for complete solution of this
data structure interview question.
Question 8: What is the difference between Stack and Queue data structure?
One of the classical data structure interviews question. I guess everyone knows, No?
Anyway, the main difference is that Stack is LIFO (Last In First Out) data structure while
Queue is a FIFO (First In First Out) data structure. You can further see my article stack vs
queue in Java for more details.
Question 9: How do you find duplicates in an array if there is more than one duplicate?
(solution)
Sometimes this is asked a follow-up question of earlier data structure interview question,
related to finding duplicates in Array. One way of solving this problem is using a Hashtable
or HashMap data structure.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
You can traverse through the 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 in HashMap then calling get(index) will return number
otherwise it returns null. this property can be used to insert or update numbers in
HashMap.
Question 10: What is the difference between the Singly Linked List and Doubly Linked List
data structure?
This is another classical interview question on the data structure, mostly asked on
telephonic rounds. The main difference between the singly linked list and the doubly linked
list is the ability to traverse.
In a singly linked list, a node only points towards the next node, and there is no pointer to
the previous node, which means you can not traverse back on a singly linked list.
On the other hand, the doubly linked list maintains two pointers, towards the next and
previous node, which allows you to navigate in both directions in any linked list.
If you want to learn more about essential data structure, you can also check out JavaScript
Algorithms and Data Structures Masterclass By Colt Steele.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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 the sum of the previous two numbers e.g. 1,1, 2, 3, 5, 8, 13, 21.
An interviewer is often interested in two things, a function which returns an 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 find the
nth Fibonacci number in Java.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 12: Write Java program to check if a number is a palindrome or not? (solution)
This is similar to the previous question, not directly related to data structures, but quite
popular along with other questions. A number is called palindrome if the reverse of number
is equal to the number itself.
An interviewer asks to solve this problem without taking help from Java API or any open
source library. Anyway, it’s a simple question, you can use the division operator (/) and
remainder operator (%) to solve this question.
Just remember, division operator can be used to get rid of the 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 a Java program check if the number is palindrome or not.
Question 13: What is a binary search tree? (solution)
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 subtree
contains keys with higher node value than root, and there should not be any duplicates in
the tree.
Apart from the definition, an interview can ask you to implement a binary search tree in
Java and questions on tree traversal e.g. in order, preorder, and postorder traversals are
quite popular data structure question.
Question 14: How to reverse a linked list using recursion and iteration? (solution)
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.
As one of my readers mentioned, I have already written about it and linked into the solution
link, you can check out my solution but I strongly suggest you try it yourself before looking
at the solution.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question 15: Write a Java program to implement Stack in Java? (solution)
You can implement Stack by using an array or linked list. This question expects you to
implement standard method provided by stack data structure e.g. push() and pop(). Both
push() and pop() should be happening at top of the 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 a java.util.Stack class and you can check it’s code to get an idea. You can
also check Effective Java book, where Josh Bloch has explains how an incorrect
implementation of the stack can cause a memory leak in Java.
I also suggest looking on data structure and algorithm questions on Cracking the Coding
Interview book, as this book contains some good questions with proper explanation. That
will certainly help you to do better on programming job interviews.
Source: https://www.java67.com/2018/06/data-structure-and-algorithm-interview-
questions-programmers.html
1. What is a data-structure?
Answer:
A data structure is a way of defining, storing & retrieving data in a structural & systematic
way. A data structure may contain a different type of data item. Different kinds of data
structures are suited to different kinds of applications, and some are highly specialized to
specific tasks.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer:
Data structure availability may vary by programming languages. Commonly available data
structures are the list, arrays, stack, queues, graph, tree etc.
3. What is an algorithm?
Answer:
in certain order to get the desired output. A computer program can be viewed as
Let us move to the next Data Structures And Algorithms Interview Questions
4. Differentiate file structure from the storage structure.
Answer:
Actually, the key difference is the memory area that is being accessed. When dealing with
the structure that resides the main memory of the computer system, this is referred to as
storage structure. When dealing with an auxiliary structure, we refer to it as file structures.
5. What is a linked-list?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer:
A linked-list is a list of data-items connected with links i.e. pointers or references. Most
modern high-level programming language does not provide the feature of directly accessing
a memory location, therefore, linked-list is not supported in them or available in the form of
inbuilt functions. In computer science, a linked list is a linear collection of data elements, in
which linear order is not given by their physical placement in memory. Instead, each
element points to the next. It is a data structure consisting of a group of nodes that together
represent a sequence.
6. What is the stack?
Answer:
In data-structure, a stack is an Abstract Data Type (ADT) used to store and retrieve values in
the Last In First Out method. The stack is the memory set aside as scratch space for a thread
of execution.
7. Why do we use stacks?
Answer:
A stack structure restricts dramatically how elements are inserted, retrieved, and removed:
The most recently inserted element in the stack is the only one that can be retrieved or
removed. Stacks follow the LIFO method and the addition and retrieval of a data item take
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
only Ο(n) time. Stacks are used where we need to access data in the reverse order of their
arrival. Stacks are used commonly in recursive function calls, expression parsing, depth-first
Part 2 – Data Structures And Algorithms Interview Questions (Advanced)
Let us now have a look at the advanced Data Structures And Algorithms Interview
Questions.
8. What operations can be performed on stacks?
Answer:
9. What is a queue in data-structure?
Answer:
The queue is an abstract data structure, somewhat similar to stack. In contrast to stack, a
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
queue is opened at both ends. One end is always used to insert data (enqueue) and the
other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e.,
10. What is linear searching?
Answer:
Linear search tries to find an item in a sequentially arranged data type. These sequentially
arranged data items known as array or list, are accessible in incrementing memory location.
Linear search compares expected data item with each of the data items in list or array. The
average case time complexity of the linear search is Ο(n) and worst-case complexity is
Let us move to the next Data Structures And Algorithms Interview Questions
11. What is a binary search?
Answer:
A binary search works only on sorted lists or arrays. This search selects the middle which
splits the entire list into two parts. First, the middle is compared.
This search first compares the target value to the mid of the list. If it is not found, then it
takes a decision on the weather. In computer science, binary search, also known as half-
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
interval search, logarithmic search, or binary chop, is a search algorithm that finds the
12. What is a graph?
Answer:
A graph is a pictorial representation of a set of objects where some pairs of objects are
vertices, and the links that connect the vertices are called edges. A graph data structure
consists of a finite (and possibly mutable) set of vertices or nodes or points, together with a
set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for
a directed graph.
13. What is a recursive function?
Answer:
A recursive function is one which calls itself, directly or calls a function that in turn calls it.
Every recursive function follows the recursive properties − base criteria where functions
stop calling itself and progressive approach where the functions try to meet the base criteria
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
14. What is a tower of Hanoi?
Answer:
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more
than one ring. All rings are of different sizes and stacked upon each other where the large
disk is always below the small disk. The aim is to move the tower of a disk from one peg to
another, without breaking its properties. The objective of this game is to move the disks one
by one from the first peg to the last peg. And there is only ONE condition, we cannot place a
15. Give some examples of greedy algorithms.
Answer:
The below-given problems find their solution using a greedy algorithm approach −
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
•Knapsack Problem
Answer: A data structure offers a convenient way of organizing as well as manipulating the
data. Simply put, it allows the data to be used in an effective manner. There is a galore of data
structures and each of them is suitable for a distinct set of applications.
For instance, compiler implementations use hash tables for looking up identifiers. Similarly,
B-trees are suitable for the implementation of databases. Data structures are virtually applied
to all areas relying on data. Some of the most important ones are:
• Artificial intelligence
• Compiler design
• Database management
• Graphics
• Numerical analysis
• Operating system
• Statistical analysis
Question: How does a linear data structure differ from a non-linear data structure?
Answer: If the elements of a data structure form a sequence or a linear list then it is called a
linear data structure. On the other hand, non-linear data structures are those in which the
traversal of nodes is done in a non-linear way.
Arrays, linked lists, stacks, and queues are examples of linear data structures, while graphs
and trees are those of non-linear data structures.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question: Please enumerate the various operations that can be performed on a data
structure.
Answer: Following are the various operations that can be performed on a data structure:
Question: Can you tell which data structures are used for BFS and DFS of a graph?
Answer: BFS (Breadth First Search) of a graph uses a queue. Although DFS (Depth First Search)
of a graph makes use of a stack, it can also be implemented using recursion that uses function
call stack.
Question: Please explain stack and also mention some of its important applications.
Answer: Stack is a linear data structure that follows either LIFO (Last In First Out) or FILO (First
In Last Out) approach for accessing elements. Push, pop, and peek are the basic operations
of a stack.
Answer: A queue is a form of linear structure that follows the FIFO (First In First Out) approach
for accessing elements. Dequeue, enqueue, front, and rear are basic operations on a queue.
Like a stack, a queue can be implemented using arrays and linked lists.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
In a stack, the item that is most recently added is removed first. Contrary to this, the item
least recently added is removed first in case of a queue.
Question: What do you understand by a binary search? What is the best scenario of using
it?
Answer: A binary search is an algorithm that starts with searching in the middle element. If
the middle element is not the target element then it further checks whether to continue
searching the lower half or the higher half. The process continues until the target element is
found.
The binary search works best when applied to a list with sorted or ordered elements.
Question: Could you explain how to reference all the elements in a one-dimension array?
A: We can reference all the elements in a one-dimension array using an indexed loop. The
counter runs from 0 to the maximum array size, say n, minus one. All elements of the one-
dimension array are referenced in sequence by using the loop counter as the array subscript.
Answer: Both FIFO and LIFO are approaches to accessing, storing, and retrieving elements
from a data structure. LIFO stands for Last In First Out. In this approach, recently stored data
is the one to be extracted first.
FIFO is a contraction for First In First Out. Following this approach, the data that is stored the
least recently will be extracted first.
Question: Do you know how does dynamic memory allocation help in managing data?
Answer: Dynamic memory allocation helps in storing simple structured data types. Moreover,
it can combine separately allocated structured blocks for forming composite structures that
contract and expand as required.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer: While NULL is a value, VOID is a data type identifier. A variable assigned with a NULL
value represents an empty value. The VOID is used for identifying pointers having no initial
size.
Answer: Both PUSH and POP operations pertain to a stack. Data is added to the stack using
the PUSH operation, while it is retrieved using the POP operation.
Question: Could you explain how does variable declaration affect memory allocation?
Answer: The total amount of memory to be allocated or reserved in the case of a variable
declaration depends on the data type used. For instance, declaring an integer type variable
reserves 4 bytes of memory space while declaring a double variable reserve 8 bytes of the
available memory.
Answer: Data abstraction helps in dividing complex data problems into smaller, easy-to-
manage parts. It starts with specifying all the involved data objects and the various operations
to be performed on the same without stressing too much on the way data is stored.
Question: How will you insert a new item in a binary search tree?
Answer: As a binary search tree doesn’t allow for duplicates, the new item to be inserted
must be unique. Assuming it is, we will proceed with checking whether the tree is empty or
not. If it is empty, then the new item will be inserted in the root node.
However, if the tree is non-empty then we will refer to the key of the new item. When it is
smaller than the root item’s key, the new item will be added to the left subtree. If the new
item’s key is bigger than the root item’s key, then the new item is inserted into the right
subtree.
Question: Could you explain how does the selection sort work on an array?
Answer: The selection sort begins with finding the smallest element. It is switched with the
element present at subscript 0. Next, the smallest element in the remaining subarray is
located and switched with the element residing in the subscript 1.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
The aforementioned process is repeated until the biggest element is placed at the subscript
n-1, where n represents the size of the given array.
Question: Do you know how the memory is affected by signed and unsigned numbers?
Answer: For signed numbers, the first bit is reserved for indicating whether the number is
positive or negative. Hence, it has one bit less for storing the value. Unlike signed numbers,
unsigned numbers have all the bits available for storing the number.
The effect of the aforementioned can be seen in the value range available to signed and
unsigned numbers. While an unsigned 8-bit number can have a range of 0 to 255, an 8-bit
signed number has a range varying from -128 to 127.
Answer: Except for pointers, all declaration statements result in a fixed memory reservation.
Instead of allocating memory for storing data, a pointer declaration results into allocating
memory for storing the address of the pointer variable.
For pointers, actual memory allocation for the data happens during the runtime.
Answer: A stack follows the LIFO approach. This means that data manipulation follows a
specific sequence where the latest data element is the one to be retrieved first.
Unlike a stack, an array doesn’t follow any particular sequence for adding or retrieving data.
Adding or retrieving an element in an array is done by referring to the array index.
Answer: An AVL tree is a type of BST (Binary Search Tree), which is always in a partially-
balanced state. The measure of the balance is given by the difference of the heights of the
subtrees from the root node of the AVL tree.
Question: Please explain how does an Array differ from a Linked List?
Answer: Following are the various differences between an array and a linked list:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Additional Memory – For each element belonging to a linked list, extra memory
•
space is required for storing the pointer. Arrays have no such requirement
• Cache – In comparison to linked lists, arrays have better cache locality, which can
significantly enhance the performance in various scenarios
• Insertion and Deletion – It is easy to add or delete elements in a linked list. Inserting
and deleting elements for an array is comparatively expensive
• Random Access – Linked lists do not allow random access, while arrays do
• Size – While the size of an array is fixed, the size of a linked list is dynamic
Answer:
• Infix Notation – Operators are written between the operands. This is the standard
way of writing expressions. For example, A * (B + C) / D
• Postfix Notation/Reverse Polish Notation – Operators are written after the
operands, hence the name. For instance, A B C + * D /
• Prefix Notation/Polish Notation – Operators are written before the operands. / * A
+ B C D is the prefix notation equivalent of the aforementioned postfix notation
example
Question: Please explain the Linked List and its various types.
Answer: In a linked list, each element is a distinct object. Like arrays, linked lists are a linear
type of data structures. In addition to data, every element of a linked list comprises a
reference to the next element. Various types of linked lists are:
• Singly Linked List – Each node stores the address or reference of the next node in
the linked list, leave for the last node that stores NULL
• Doubly Linked List – Each node keeps two references. One point to the next node
and the other points to the previous node
• Circular Linked List – In this type of linked list, all nodes are connected to form a
circle. Hence, there is no NULL at the end. A circular linked list can either be a single
circular linked list or a double circular linked list
Question: How will you implement a stack using queue and vice-versa?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer: It is possible to implement a stack using two queues. Further, there are two options;
either to make the push operation costly or the pop operation costly.
A queue can also be implemented with two stacks. Moreover, there are two options; either
to make the enQueue operation costly or the deQueue operation costly.
Question: Which data structures are used for implementing LRU cache?
Answer: By organizing items in order of use, a Least Recently Used or LRU cache allows quick
identification of an item that hasn’t been put to use for the longest time. Two data structures
are used for implementing an LRU cache:
• Queue – Implemented using a doubly linked list. The maximum size of the queue is
determined by the total number of frames available i.e. the cache size. While the
most recently used pages will be near the rear end of the queue, the least recently
pages will be near the queue’s front end
• Hashmap – Having page number as the key along with the address of the
corresponding queue node as the value
Question: Could you give a brief explanation of the various approaches for developing
algorithms?
• Divide and Conquer – Involves dividing the entire problem into a number of
subproblems and then solving each of them independently
• Dynamic Programming – Identical to the divide and conquer approach with the
exception that all sub-problems are solved together
• Greedy Approach – Finds a solution by choosing the next best option
Question: Please enumerate some examples of greedy and divide and conquer algorithms.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
•Knapsack Problem
• Kruskal’s Minimal Spanning Tree
• Prim’s Minimal Spanning Tree
• Travelling Salesman
Following are some notable instances of the divide and conquer approach:
•Binary Search
• Closest Pair (or Points)
• Merge Sort
• Quick Sort
• Strassen’s Matrix Multiplication
Answer: Both insertion and selection approaches maintain two sub-lists, sorted and unsorted.
Each takes one element from the unsorted sub-list and place it into the sorted sub-list. The
distinction between the two sorting processes lies in the treatment of the current element.
Insertion sort takes the current element and places it in the sorted sublist at the appropriate
location. Selection sort, on the other hand, searches for the minimum value in the unsorted
sub-list and replaces the same with the present element.
Answer: The shell sort can be understood as a variant of the insertion sort. The approach
divides the entire list into smaller sub-lists based on some gap variable. Each sub-list is then
sorted using insertion sort.
Answer: The process for visiting all the nodes of a tree is called tree traversal. It always starts
from the root node and there are three ways of doing it:
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Question: Please explain a spanning tree. What is the maximum number of spanning trees
a graph can have?
Answer: A spanning tree is a subset of a graph that has all the vertices but with the minimum
possible number of edges. Neither a spanning tree can be disconnected and nor does it have
cycles.
The maximum number of spanning trees that a graph can have depended on how connected
the graph is. A complete undirected graph with n number of nodes can have a maximum of
nn-1 number of spanning trees.
Answer: Kruskal’s algorithm treats a graph as a forest and each node in it as an individual
tree. A tree connects to another tree only if it:
Answer: A Heap data structure is a special balanced binary tree in which the root node key is
compared with its children and accordingly arranged. A Heap data structure can be of two
types:
• Min-Heap – The parent node has a key value less than its children
• Max-Heap – The parent node has a key value greater than its children
Answer: The ability to allow a function or module to call itself is called recursion. Either a
function f calls itself directly or calls another function ‘g’ that in turn calls the function ‘f. The
function f is known as the recursive function and it follows the recursive properties:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer: The Tower of Hanoi is a mathematical puzzle that comprises of three tower (or pegs)
and more than one ring. Each ring is of varying size and stacked upon one another such that
the larger one is beneath the smaller one.
The goal of the Tower of Hanoi problem is to move the tower of the disk from one peg to
another without breaking the properties.
Question: How do the BFS (Breadth First Search) and DFS (Depth First Search) algorithms
work?
Answer: The BFS algorithm traverses a graph in the breadthwards motion. It uses a queue to
remember the next vertex for starting a search when a dead end occurs in any iteration.
A DFS algorithm traverses a graph in the depthward motion. It uses a stack for remembering
the next vertex to start a search when coming across a dead end in an iteration.
Answer: The technique of converting a range of key values into a range of indexes of an array
is known as hashing. It is possible to create associative data storage using hash tables where
data indices can be found by providing the corresponding key values.
Question: Please explain an MST (Minimum Spanning Tree). Also, explain how does Prim’s
algorithm find a minimum spanning tree.
Answer: An MST or Minimum Spanning Tree is a spanning tree in a weighted graph that has
the minimum weight of all the possible spanning trees. Each node is treated as a single tree
by Prim’s algorithm while adding new nodes to the spanning tree from the available graph.
Answer: The interpolation search technique is an enhanced variant of binary search. It works
on the probing position of the required value.
Question: How will you check whether the given Binary Tree is BST or not?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Answer: Simply do inorder traversal of the given binary tree while keeping track of the
previous key value. If the current key value is greater, then continue, otherwise return false.
The binary tree is BST if the inorder traversal of the binary tree is sorted.
The data structure, algorithms, string, array are a core part of any Programming Language
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.
There are a lot of computer science graduates and programmers applying for programming,
coding, and software development roles at startups like Uber and Netflix; big organizations
like Amazon, Microsoft, and Google; and service-based companies like Infosys or
Luxsoft, but many of them have no idea of what kind of programming interview questions
to expect when you're applying for a job with these companies.
In this article, I'll share some frequently asked programming interview questions from
different interviews for programmers at different levels of experience, from people who
have just graduated from college to programmers with one to two years of experience.
As a computer science graduate, its expected from a programmer 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 very short time and that's why I am
sharing some frequently asked Data structure and algorithm questions from various Java
interviews.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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, Algorithm and Programming Language 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 a 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:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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 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:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
If you need more advanced questions based upon array then you can see also seeThe
Coding Interview Bootcamp: Algorithms + Data Structures, a boot camp 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?
• 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.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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.
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.
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. 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.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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 the least, we will see some bit 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.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
That's all about some data structure and algorithm interview questions for programmers.
Remember, its one of the most important topic for all levels of programmers, but its 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
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
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 on 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.
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.
Source: HOB
These are questions that are reported to have been asked during various job inter- views.
We may not have covered everything that was asked, but you should try to answer them
anyway and look up what you do not know.1
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
which, given a linked list, returns true if the list is circular and false if it is not?
6. Given a singly linked list, how would you find a node that is n nodes away from the
last node? Think of iterative and recursive solutions. You can make only a single pass
through the list.
7. Given two sorted linked lists, write code that merges them into a single sorted linked
list without copying the lists into a new list; i.e., just merge the two into one.
8. How can you simulate queue behavior using two stacks?
CSci 235 Software Design and Analysis II Prof. Stewart Weiss Data Structures Job Interview
Questions
8. Write a method of a binary (search) tree class that returns the sum of all of the
numbers stored in the nodes.
Write another method that returns the sum of the numbers stored in the leaf-
nodes.
Write another method that returns the sum of the numbers stored at even num-
bered levels (assume that the root is at level 0, which is even).
9. Given the binary tree below show the inorder, preorder and postorder traversals of
the tree.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
DSME
FGVRH
10. Write a method of a binary search tree class that converts the tree to its mirror
image (i.e., swaps left and right child for each node). Is the resulting tree a binary
search tree?
11. Write a method of a binary tree class that determines if a given tree is a binary
search tree or not.
12. Write a method of a binary tree
13. String printAllPathsFromRoot ()
that returns a multi-line String containing one per line all paths from root to the
leaves (there should be as many lines as there are leaves in the tree).
14. Write a method of a binary (search) tree that returns the largest depth of any node.
15. Given a sorted array (increasing order) of integers, write an algorithm that cre- ates a
binary search tree of minimal height.
16. Write an algorithm that when given a link to a node in a binary search tree returns
the successor value of that node in the tree (the ”successor” being the value that
would follow the value in the given node in an inorder traversal of the tree). Do not
use the inorder traversal.
CSci 235 Software Design and Analysis II Prof. Stewart Weiss Data Structures Job Interview
Questions
(a) What is the complexity (average or worst case) of inserting a new element into:
i. an unsorted singly linked list (at the end) ii. an unsorted doubly linked list (at the
end)
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
iii. a stack (assume array based) iv. a queue (assume array based)
(b) What is the complexity (average or worst case) of finding a specific element in:
17. What is the relationship between a queue and a priority queue? ( we did not cover
this)
18. What data structure can ”simulate” recursion?
19. How would you design a stack that, in addition to traditional push() and pop()
operations, also provides a max() function? Your push(), pop() and max() func- tions
should be O(1).
What is data
structure?
The logical and mathematical model of a particular organization of
data is called data structure. There are two types of data structure
§ Linear
§ Nonlinear
2. What is a linked list?
§ A linked list is a linear collection of data elements, called nodes, where the
linear order is given by pointers.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
§ Each node has two parts first part contain the information of the element
second part contains the address of the next node in the list.
3. What is a queue?
§ A queue is an ordered collection of items from which items may be deleted at
one end (front end) and items inserted at the other end (rear end).
§ It obeys FIFO rule there is no limit to the number of elements a queue contains.
4. What is a spanning Tree?
§ A spanning tree is a tree associated with a network.
§ All the nodes of the graph appear on the tree once.
§ A minimum spanning tree is a spanning tree organized so that the total edge
weight between nodes is minimized.
5. What is precision?
§ Precision refers the accuracy of the decimal portion of a value.
§ Precision is the number of digits allowed after the decimal point.
6. What are the goals of Data Structure?
§ It must rich enough in structure to reflect the actual relationship of data in real
world.
§ The structure should be simple enough for efficient processing of data.
7. What is the difference between a Stack and an Array?
Stack
§ Stack is a dynamic object whose size is constantly changing as items are pushed
and popped .
§ Stack may contain different data types.
§ Stack is declared as a structure containing an array to hold the element of the
stack, and an integer to indicate the current stack top within the array.
§ Stack is a ordered collection of items.
Array
§ Array is an ordered collection of items.
§ Array is a static object.
§ It contains same data types.
§ Array can be home of a stack i.e. array can be declared large enough for
maximum size of the stack.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
each item or element is connected with two or more other items in a non-linear
arrangement. The data elements are not arranged in the sequential structure.
3) List the area of applications of Data Structure.
Data structures are applied extensively in the following areas of computer science:
Compiler Design,
Operating System,
Database Management System,
Statistical analysis package,
Numerical Analysis,
Graphics,
Artificial Intelligence,
Simulation
4) What is the difference between file structure and storage structure?
Difference between file structure and storage structure:
The main difference between file structure and storage structure is based on memory area
that is being accessed.
Storage structure: It is the representation of the data structure in the computer memory.
File structure: It is the representation of the storage structure in the auxiliary memory.
5) List the data structures which are used in RDBMS, Network Data Modal, and Hierarchical
Data Model.
▪ RDBMS uses Array data structure
▪ Network data model uses Graph
▪ Hierarchal data model uses Trees
6) Which data structure is used to perform recursion?
Stack data structure is used in recursion due to its last in first out nature. Operating system
maintains the stack in order to save the iteration variables at each function call
7) What is a Stack?
Stack is an ordered list in which, insertion and deletion can be performed only at one end
that is called the top. It is a recursive data structure having pointer to its top element. The
stack is sometimes called as Last-In-First-Out (LIFO) list i.e. the element which is inserted
first in the stack will be deleted last from the stack.
8) List the area of applications where stack data structure can be used?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
3 Expression evaluation
4 Backtracking
5 Memory Management
6 Function calling and return
9) What are the operations that can be performed on a stack?
▪ Push Operations
▪ Pop Operations
▪ Peek Operations
10) Write the stack overflow condition.
Overflow occurs when top = Maxsize -1
11) What is the difference between PUSH and POP?
PUSH and POP operations specify how data is stored and retrieved in a stack.
PUSH: PUSH specifies that data is being "inserted" into the stack.
POP: POP specifies data retrieval. It means that data is being deleted from the stack.
12) Write the steps involved in the insertion and deletion of an element in the stack.
Push:
4 Increment the variable top so that it can refer to the next memory allocation
5 Copy the item to the at the array index value equal to the top
6 Repeat step 1 and 2 until stack overflows
Pop:
4 Store the topmost element into the an another variable
5 Decrement the value of the top
6 Return the topmost element
13) What is a postfix expression?
An expression in which operators follow the operands is known as postfix expression. The
main benefit of this form is that there is no need to group sub-expressions in parentheses or
to consider operator precedence.
The expression "a + b" will be represented as "ab+" in postfix notation.
14)Write the postfix form of the expression: (A + B) * (C - D)
AB+CD-*
)15) Which notations are used in Evaluation of Arithmetic Expressions using prefix and
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
postfix forms?
Polish and Reverse Polish notations.
16)What is an array?
Arrays are defined as the collection of similar types of data items stored at contiguous
memory locations. It is the simplest data structure in which each data element can be
randomly accessed by using its index number.
17) How to reference all the elements in a one-dimension array?
It can be done by using an indexed loop such that the counter runs from 0 to the array size
minus one. In this manner, you can reference all the elements in sequence by using the loop
counter as the array subscript.
18) What is a multidimensional array?
The multidimensional array can be defined as the array of arrays in which, the data is stored
in tabular form consists of rows and columns. 2D arrays are created to implement a
relational database lookalike data structure. It provides ease of holding the bulk of data at
once which can be passed to any number of functions wherever required.
19) How are the elements of a 2D array are stored in the memory?
There are two techniques by using which, the elements of a 2D array can be stored in the
memory.
▪ Row-Major Order: In row-major ordering, all the rows of the 2D array are stored into the
memory contiguously. First, the 1st row of the array is stored into the memory
completely, then the 2nd row of the array is stored into the memory completely and
so on till the last row.
▪ Column-Major Order: In column-major ordering, all the columns of the 2D array are
stored into the memory contiguously. first, the 1st column of the array is stored into
the memory completely, then the 2nd row of the array is stored into the memory
completely and so on till the last column of the array.
20) Calculate the address of a random element present in a 2D array, given base address as
BA.
Row-Major Order: If array is declared as a[m][n] where m is the number of rows while n is
the number of columns, then address of an element a[i][j] of the array stored in row major
order is calculated as,
Address(a[i][j]) = B. A. + (i * n + j) * size
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Column-Major Order: If array is declared as a[m][n] where m is the number of rows while n
is the number of columns, then address of an element a[i][j] of the array stored in column
major order is calculated as
Address(a[i][j]) = ((j*m)+i)*Size + BA.
21) Define Linked List Data structure.
Linked List is the collection of randomly stored data objects called nodes. In Linked List, each
node is linked to its adjacent node through a pointer. A node contains two fields, i.e. Data
Field and Link Field.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
22) Are linked lists considered linear or non-linear data structures?
A linked list is considered both linear and non-linear data structure depending upon the
situation.
▪ On the basis of data storage, it is considered as a non-linear data structure.
▪ On the basis of the access strategy, it is considered as a linear data-structure.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
23) What are the advantages of Linked List over an array?
▪ The size of a linked list can be incremented at runtime which is impossible in the case of
the array.
▪ The List is not required to be contiguously present in the main memory, if the contiguous
space is not available, the nodes can be stored anywhere in the memory connected
through the links.
▪ The List is dynamically stored in the main memory and grows as per the program demand
while the array is statically stored in the main memory, size of which must be
declared at compile time.
▪ The number of elements in the linked list are limited to the available memory space while
the number of elements in the array is limited to the size of an array.
24) Write the syntax in C to create a node in the singly linked list.
1 struct node
2 {
3 int data;
4 struct node *next;
5 };
6 struct node *head, *ptr;
7 ptr = (struct node *)malloc(sizeof(struct node));
25) If you are using C language to implement the heterogeneous linked list, what pointer
type should be used?
The heterogeneous linked list contains different data types, so it is not possible to use
ordinary pointers for this. For this purpose, you have to use a generic pointer type like void
pointer because the void pointer is capable of storing a pointer to any type.
26) What is doubly linked list?
The doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence. In a doubly linked list, a node consists
of three parts:
◦ node data
◦ pointer to the next node in sequence (next pointer)
◦ pointer to the previous node (previous pointer).
27) Write the C program to insert a node in circular singly list at the beginning.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
1 #include<stdio.h>
2 #include<stdlib.h>
3 void beg_insert(int);
4 struct node
5 {
6 int data;
7 struct node *next;
8 };
9 struct node *head;
10 void main ()
11 {
12 int choice,item;
13 do
14 {
15 printf("\nEnter the item which you want to insert?\n");
16 scanf("%d",&item);
17 beg_insert(item);
18 printf("\nPress 0 to insert more ?\n");
19 scanf("%d",&choice);
20 }while(choice == 0);
21 }
22 void beg_insert(int item)
23 {
24
25 struct node *ptr = (struct node *)malloc(sizeof(struct node));
26 struct node *temp;
27 if(ptr == NULL)
28 {
29 printf("\nOVERFLOW");
30 }
31 else
32 {
33 ptr -> data = item;
34 if(head == NULL)
35 {
36 head = ptr;
37 ptr -> next = head;
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
38 }
39 else
40 {
41 temp = head;
42 while(temp->next != head)
43 temp = temp->next;
44 ptr->next = head;
45 temp -> next = ptr;
46 head = ptr;
47 }
48 printf("\nNode Inserted\n");
49 }
50
51 }
52
28) Define the queue data structure.
A queue can be defined as an ordered list which enables insert operations to be performed
at one end called REAR and delete operations to be performed at another end called
FRONT.
29) List some applications of queue data structure.
The Applications of the queue is given as follows:
◦ Queues are widely used as waiting lists for a single shared resource like a printer, disk,
CPU.
◦ Queues are used in the asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO, sockets.
◦ Queues are used as buffers in most of the applications like MP3 media player, CD player,
etc.
◦ Queues are used to maintain the playlist in media players to add and remove the songs
from the play-list.
◦ Queues are used in operating systems for handling interrupts.
30) What are the drawbacks of array implementation of Queue?
◦ Memory Wastage: The space of the array, which is used to store queue elements, can
never be reused to store the elements of that queue because the elements can only
be inserted at front end and the value of front might be so high so that, all the space
before that, can never be filled.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
◦ Array Size: There might be situations in which, we may need to extend the queue to
insert more elements if we use an array to implement queue, It will almost be
impossible to extend the array size, therefore deciding the correct array size is
always a problem in array implementation of queue.
31) What are the scenarios in which an element can be inserted into the circular queue?
◦ If (rear + 1)%maxsize = front, the queue is full. In that case, overflow occurs and therefore,
insertion can not be performed in the queue.
◦ If rear != max - 1, the rear will be incremented to the mod(maxsize) and the new value will
be inserted at the rear end of the queue.
◦ If front != 0 and rear = max - 1, it means that queue is not full therefore, set the value of
rear to 0 and insert the new element there.
32) What is a dequeue?
Dequeue (also known as double-ended queue) can be defined as an ordered set of elements
in which the insertion and deletion can be performed at both the ends, i.e. front and rear.
33) What is the minimum number of queues that can be used to implement a priority
queue?
Two queues are needed. One queue is used to store the data elements, and another is used
for storing priorities.
34) Define the tree data structure.
The Tree is a recursive data structure containing the set of one or more data nodes where
one node is designated as the root of the tree while the remaining nodes are called as the
children of the root. The nodes other than the root node are partitioned into the nonempty
sets where each one of them is to be called sub-tree.
35) List the types of tree.
There are six types of tree given as follows.
◦ General Tree
◦ Forests
◦ Binary Tree
◦ Binary Search Tree
◦ Expression Tree
◦ Tournament Tree
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
11 {
12 return 0;
13 }
14 }
42) Write a recursive C function to calculate the height of a binary tree.
1 int countHeight(struct node* t)
2 {
3 int l,r;
4 if(!t)
5 return 0;
6 if((!(t->left)) && (!(t->right)))
7 return 0;
8 l=countHeight(t->left);
9 r=countHeight(t->right);
10 return (1+((l>r)?l:r));
11 }
43) How can AVL Tree be useful in all the operations as compared to Binary search tree?
AVL tree controls the height of the binary search tree by not letting it be skewed. The time
taken for all operations in a binary search tree of height h is O(h). However, it can be
extended to O(n) if the BST becomes skewed (i.e. worst case). By limiting this height to log
n, AVL tree imposes an upper bound on each operation to be O(log n) where n is the
number of nodes.
44) State the properties of B Tree.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.
◦ Every node in a B-Tree contains at most m children.
◦ Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
◦ The root nodes must have at least 2 nodes.
◦ All leaf nodes must be at the same level.
45) What are the differences between B tree and B+ tree?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
SN B Tree B+ Tree
2 Data can be stored in leaf nodes as well as internal nodes Data can only be stored o
3 Searching for some data is a slower process since data can be found Searching is comparative
on internal nodes as well as on the leaf nodes. on the leaf nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked tog
more efficient.
46) List some applications of Tree-data structure?
Applications of Tree- data structure:
◦ The manipulation of Arithmetic expression,
◦ Symbol Table construction,
◦ Syntax analysis
◦ Hierarchal data model
47) Define the graph data structure?
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices
and E(G) represents the set of edges which are used to connect these vertices. A graph can
be seen as a cyclic tree, where the vertices (Nodes) maintain any complex relationship
among them instead of having parent-child relations.
48) Differentiate among cycle, path, and circuit?
◦ Path: A Path is the sequence of adjacent vertices connected by the edges with no
restrictions.
◦ Cycle: A Cycle can be defined as the closed path where the initial vertex is identical to the
end vertex. Any vertex in the path can not be visited twice
◦ Circuit: A Circuit can be defined as the closed path where the intial vertex is identical to
the end vertex. Any vertex may be repeated.
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
49) Mention the data structures which are used in graph implementation.
For the graph implementation, following data structures are used.
◦ In sequential representation, Adjacency matrix is used.
◦ In Linked representation, Adjacency list is used.
50) Which data structures are used in BFS and DFS algorithm?
◦ In BFS algorithm, Queue data structure is used.
◦ In DFS algorithm, Stack data structure is used.
51) What are the applications of Graph data structure?
The graph has the following applications:
◦ Graphs are used in circuit networks where points of connection are drawn as vertices and
component wires become the edges of the graph.
◦ Graphs are used in transport networks where stations are drawn as vertices and routes
become the edges of the graph.
◦ Graphs are used in maps that draw cities/states/regions as vertices and adjacency
relations as edges.
◦ Graphs are used in program flow analysis where procedures or modules are treated as
vertices and calls to these procedures are drawn as edges of the graph.
54) In what scenario, Binary Search can be used?
Binary Search algorithm is used to search an already sorted list. The algorithm follows divide
and conqer approach
Example:
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
52) What are the advantages of Binary search over linear search?
There are relatively less number of comparisons in binary search than that in linear search.
In average case, linear search takes O(n) time to search a list of n elements while Binary
search takes O(log n) time to search a list of n elements.
53) What are the advantages of Selecetion Sort?
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials
Best website for interview preparation: www.prodevelopertutorial.com
Buy AJ's guide for data structure and algorithm book to clear interview. 90+ Chapters
https://rebrand.ly/buy_top_DS_algo_book_to_clear_interview
Join telegram channel for placement materials
https://t.me/ajPlacementMaterials