## DSA Interview Questions

1. What is a Data Structure?

**Answer**:- Data structure is a way that specifies how to organize and manipulate the data so that the data can be used efficiently. It also specify the relationship between them. Some example of data structure are - array, linked list, stack etc.

2. What are linear and non linear data Structures?

**Answer**:-

**Linear Data Structure:** If all the elements are arranged in sequential order then it is called linear data strucutre for example Array, Linked List.

**Non Linear Data Structure** If the elements are not arranged in sequential order then it is called non linear data structure for example Tree, Graph.

3. How Linked list is different from array?

**Answer**:- Differences between array and linked list are -

1) Arrays are index based data structure where each element is associated with an index. On the other hand, Linked list relies on references where each node consists of the data and the references to the previous and next element.

2) The size of the arrays is fixed, Linked Lists are Dynamic in size.

3) In array elements are accessed randomly using index while in linked list elements are accessed in sequence.

4) Insertion and Deletion operations are expensive in array whereas it can be easily done in linked list.

5) Extra memory space is required in linked list for storing address of next node.

6) Element location is allocated during compile time in array while in linked list it is done during run time.

4. What are the criteria of algorithm analysis?

**Answer**:- Algorithms are analyzed on the basis of 2 factors - time and space. Time specify how much execution time and space specify how much extra space is required by the algorithm.

5. What are the operations that can be performed on a data-structures?

**Answer**:- Following operations can be performed -

**Insertion :**Adding a new data item.

**Deletion :**Deleting the existing data item.

**Traversal :**Accessing each data item.

**Searching :**Finding a particular data item.

**Sorting :**Arranging the data item in a particular sequence.

6. Explain the difference between Divide & Conquer and Dynamic programming?

**Answer**:- In divide and Conquer approach we divide the problem into minimum possible sub-problem and solve them independently. Divide-and-Conquer is a Top-Down Technique. In Dynamic Programming we diving the problem to a minimum possible sub-problem and solve them combinedly. Dynamic Programming is a Bottom-Up Technique.

7. Which data structure is used to perform recursion?

**Answer**:- Stack is used to perform recursion due its Last in First Property. Recursion makes use of system stack for storing the return addresses of the function calls.

8. What are the operations that can be performed on a stack?

**Answer**:- Various operations that can be performed on a stack are -

**Push Operation : ** Inserting the data item into the stack.

**Pop Operation : ** Deleting the data item from the stack.

**Peek Operation: ** Get the data item which is on the top of the stack, without removing it.

9. Explain Infix, prefix, Postfix notations?

**Answer**:-

In **Infix Notation** operators are written in-between their operands. For example -> A + B * C + D.

In **Prefix Notation** operators are written before their operands. For example -> + + A * B C D.

In ** Postfix Notation** operators are written after their operands. For example -> A B C * + D +.

10. What is a queue in data-structure?

**Answer**:- Queue is a linear data structure where the elements are inserted from one end called REAR and deleted from the other end called as FRONT. It follow First In First Out (FIFO) i.e the first element that is added to the queue is the first one to be removed.

11. How to implement a stack using queue?

**Answer**:- Stack can be implemented using 2 queues. To push an item in the stack, we first move all elements from the first queue to the second queue, then push new item into the first queue, and finally move all elements back to the first queue. This ensures that the new item lies at front of the queue and hence would be the first one to be removed. To pop an item from the stack, we simply return the front item from the first queue.

12. What is Brute Force algorithm?

**Answer**:- Brute force means that you will go through all possible solutions extensively. For example, in a chess game, if you know you can win in two moves, the brute force will go through all possible combination of moves, without taking anything in consideration. So the little pawn in the back that cannot influence the outcome will still be considered.

13. What is the wrost case complexity of merge sort and quick sort?

**Answer**:-

**Merge sort :** n*log(n)

**Quick sort :** n^2

14. What is shell sort?

**Answer**:- Shell sort is a is a generalized version of insertion sort. It is a highly efficient sorting algorithm. In shell sort we break the original list into a number of smaller sublists, each of which is sorted using an insertion sort.

15. Which data structures are used for BFS and DFS of a graph?

**Answer**:- Queue is used for BFS while Stack is used for DFS.

16. What is a binary search tree?

**Answer**:- Binary search tree is a binary tree in which all nodes of left subtree are less than root node and all nodes of right subtree are more than root node.

17. What is an AVL Tree?

**Answer**:- AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodes.

18. What is a spanning tree?

**Answer**:- A spanning tree is a subset of a graph, which has all the vertices covered with minimum possible number of edges. It cannot be disconnected and does not have cycles.

19. What is hashing technique?

**Answer**:- Hashing is a technique where search time is independent of the number of items or elements. In this technique a hash function is used to generate an address from a key. The hash function takes a key as input and returns the hash value of that key which is used as an address index in the array.

20. what is heap data structure?

**Answer**:- Heap is a tree based data structure which satisfy the heap property. Heap can be of 2 type -

**Min Heap : ** In this the value of the root node is less than or equal to either of its children. The same property must be recursively true for all sub-trees in that Binary Tree.

**Max Heap : ** In this the value of the root node is greater than or equal to either of its children. The same property must be recursively true for all sub-trees in that Binary Tree.

**Also check : **

Discussion