Data Structure Interview Questions
Data Structure Interview Questions
A list of most frequently asked Data Structure interview questions and answers are
given below.
Linear Data Structure: A data structure is called linear if all of its elements are
arranged in the sequential order. In linear data structures, the elements are stored in a
non-hierarchical way where each item has the successors and predecessors except the
first and last element.
Non-Linear Data Structure: The Non-linear data structure does not form a sequence
i.e. 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.
o Compiler Design,
o Operating System,
o Database Management System,
o Statistical analysis package,
o Numerical Analysis,
o Graphics,
o Artificial Intelligence,
o Simulation
The main difference between file structure and storage structure is based on memory
area that is being accessed.
File structure: It is the representation of the storage structure in the auxiliary memory.
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?
o Expression evaluation
o Backtracking
o Memory Management
o Function calling and return
o Push Operations
o Pop Operations
o Peek Operations
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.
o Increment the variable top so that it can refer to the next memory allocation
o Copy the item to the at the array index value equal to the top
o Repeat step 1 and 2 until stack overflows
Pop:
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.
o 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.
o 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.
Address(a[i][j]) = B. A. + (i * n + j) * size
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));
o node data
o pointer to the next node in sequence (next pointer)
o pointer to the previous node (previous pointer).
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;
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.
o Queues are widely used as waiting lists for a single shared resource like a printer,
disk, CPU.
o 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.
o Queues are used as buffers in most of the applications like MP3 media player,
CD player, etc.
o Queues are used to maintain the playlist in media players to add and remove
the songs from the play-list.
o Queues are used in operating systems for handling interrupts.
o 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.
o 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.
o If (rear + 1)%maxsize = front, the queue is full. In that case, overflow occurs and
therefore, insertion can not be performed in the queue.
o 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.
o 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.
o General Tree
o Forests
o Binary Tree
o Binary Search Tree
o Expression Tree
o Tournament Tree
39) Which data structure suits the most in the tree construction?
Queue data structure
40) Which data structure suits the most in the tree construction?
Queue data structure
SN B Tree B+ Tree
1 Search keys cannot repeatedly be stored. Redundant search keys can be present.
2 Data can be stored in leaf nodes as well as Data can only be stored on the leaf nodes.
internal nodes
3 Searching for some data is a slower process since Searching is comparatively faster as data can
data can be found on internal nodes as well as on only be found on the leaf nodes.
the leaf nodes.
4 Deletion of internal nodes is so complicated and Deletion will never be a complexed process
time-consuming. since element will always be deleted from the
leaf nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make the
search operations more efficient.
o Path: A Path is the sequence of adjacent vertices connected by the edges with
no restrictions.
o 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
o 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.
50) Which data structures are used in BFS and DFS algorithm?
Example:
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.
o Sparse matrix,
o Index generation.