DS Interview Questions: 1. What Is Data Structure?
DS Interview Questions: 1. What Is Data Structure?
6. What is an algorithm?
An algorithm is a step-by-step method of solving a problem or manipulating data. It defines a set of
instructions to be executed in a certain order to get the desired output.
9. List the data structures which are used in RDBMS, Network Data Modal, and Hierarchical Data Model.
o RDBMS uses Array data structure
o Network data model uses Graph
o Hierarchal data model uses Trees
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.
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.
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
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.
8. Explain the scenarios where you can use linked lists and arrays.
Following are the scenarios where we use linked list over array :
o When we do not know the exact number of elements beforehand.
o When we know that there would be large number of add or remove operations.
o Less number of random-access operations.
o When we want to insert items anywhere in the middle of the list, such as when implementing a
priority queue, linked list is more suitable.
Below are the cases where we use arrays over the linked list :
o When we need to index or randomly access elements more frequently.
o When we know the number of elements in the array beforehand in order to allocate the right
amount of memory.
o When we need speed while iterating over the elements in the sequence.
o When memory is a concern: Due to the nature of arrays and linked list, it is safe to say that filled
arrays use less memory than linked lists. Each element in the array indicates just the data whereas
each linked list node represents the data as well as one or more pointers or references to the other
elements in the linked list.
42. Name the ways to determine whether a linked list has a loop.
Using hashing
Using the visited nodes method (with or without modifying the basic linked list data structure)
Floyd’s cycle-finding algorithm
25. What pointer type should be used to implement the heterogeneous linked list in C language?
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.
12) Write the steps involved in the insertion and deletion of an element in the stack.
Push:
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:
o Store the topmost element into another variable
o Decrement the value of the top
o Return the topmost element
15) Which notations are used in Evaluation of Arithmetic Expressions using prefix and postfix forms?
Polish and Reverse Polish notations.
31) What are the scenarios in which an element can be inserted into the circular 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.
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.
45) What are the differences between B tree and B+ tree?
S B Tree B+ Tree
N
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 Searching is comparatively faster as data
since data can be found on internal nodes as can only be found on the leaf nodes.
well as on the leaf nodes.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make the
search operations more efficient.
40) Which data structure suits the most in the tree construction?
Queue data structure
35. What is the difference between tree and graph data structure?
1. Tree and graph are differentiated by the fact that a tree structure must be connected and can never
have loops whereas in the graph there are no restrictions.
2. Tree provides insights on relationship between nodes in a hierarchical manner and graph follows a
network model.
17. What is the requirement for an object to be used as key or value in HashMap?
The key or value object that gets used in hashmap must implement equals() and hashcode() method. The
hash code is used when inserting the key object into the map and equals method is used when trying to
retrieve a value from the map.
19. What is the time complexity of basic operations get() and put() in HashMap class?
The time complexity is O(1) assuming that the hash function used in hash map distributes elements
uniformly among the buckets.
20. Which data structures are used for implementing LRU cache?
LRU cache or Least Recently Used cache allows quick identification of an element that hasn’t been put to
use for the longest time by organizing items in order of use. In order to achieve this, two data structures
are used:
Queue – This is implemented using a doubly-linked list. The maximum size of the queue is
determined by the cache size, i.e by the total number of available frames. The least recently used
pages will be near the front end of the queue whereas the most recently used pages will be
towards the rear end of the queue.
Hashmap – Hashmap stores the page number as the key along with the address of the
corresponding queue node as the value.
24) 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));
27) Write the C program to insert a node in circular singly list at the beginning.
#include<stdio.h>
#include<stdlib.h>
void beg_insert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beg_insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beg_insert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nNode Inserted\n");
}
}
41) Write the recursive C function to count the number of nodes present in a binary tree.
int count (struct node* t)
{
if(t)
{
int l, r;
l = count(t->left);
r=count(t->right);
return (1+l+r);
}
else
{
return 0;
}
}
26. Write a recursive function to calculate the height of a binary tree in Java.
Consider that every node of a tree represents a class called Node as given below:
public class Node{
int data;
Node left;
Node right;
}
Then the height of the binary tree can be found as follows:
int heightOfBinaryTree(Node node)
{
if (node == null)
return 0; // If node is null then height is 0 for that node.
else
{
// compute the height of each subtree
int leftHeight = heightOfBinaryTree(node.left);
int rightHeight = heightOfBinaryTree(node.right);
//use the larger among the left and right height and plus 1 (for the root)
return Math.max(leftHeight, rightHeight) + 1;
}
}
45. Consider the following tree as example for finding the left view:
39. Given an m x n 2D grid map of '1’s which represents land and '0’s that represents water, return the
number of islands (surrounded by water and formed by connecting adjacent lands in 2 directions -
vertically or horizontally). Assume that the boundary cases - which is all four edges of the grid are
surrounded by water.
Constraints are:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] can only be ‘0’ or ‘1’.
Example:
Input: grid = [
[“1” , “1” , “1” , “0” , “0”],
[“1” , “1” , “0” , “0” , “0”],
[“0” , “0” , “1” , “0” , “1”],
[“0” , “0” , “0” , “1” , “1”]
]
Output: 3
Solution:
class InterviewBit {
public int numberOfIslands(char[][] grid) {
if(grid==null || grid.length==0||grid[0].length==0)
return 0;
int m = grid.length;
int n = grid[0].length;
int count=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(grid[i][j]=='1'){
count++;
mergeIslands(grid, i, j);
}
}
}
return count;
}
public void mergeIslands(char[][] grid, int i, int j){
int m=grid.length;
int n=grid[0].length;
if(i<0||i>=m||j<0||j>=n||grid[i][j]!='1')
return;
grid[i][j]='X';
mergeIslands(grid, i-1, j);
mergeIslands(grid, i+1, j);
mergeIslands(grid, i, j-1);
mergeIslands(grid, i, j+1);
}
}
Can doubly-linked be implemented using a single pointer variable in every node?
A doubly linked list can be implemented using a single pointer. See XOR Linked List – A Memory Efficient
Doubly Linked List
Linked List Questions
Linked List Insertion
Linked List Deletion
middle of a given linked list
Nth node from the end of a Linked List
Tree Traversal Questions
Inorder
Preorder and Postoder Traversals
Level order traversal
Height of Binary Tree
Convert a DLL to Binary Tree in-place
See In-place conversion of Sorted DLL to Balanced BST
Convert Binary Tree to DLL in-place
See Convert a given Binary Tree to Doubly Linked List | Set 1, Convert a given Binary Tree to Doubly Linked
List | Set 2
Delete a given node in a singly linked list
Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
Reverse a Linked List
Write a function to reverse a linked list
Detect Loop in a Linked List
Write a C function to detect loop in a linked list.
Which data structure is used for dictionary and spell checker?
Data Structure for Dictionary and Spell Checker?