You are on page 1of 13

1. What is data structure?

-> A data structure is a particular way of storing and organizing data in a computer so that it
can be used efficiently.
Data management concept includes, data collection or organization of data in proper
structure.

2. Can you explain the 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.

3. Can you tell how linear data structures differ from non-linear data structures?
->
Linear data structure Nonlinear data structure
Elements are stored sequentially Not stored in sequential order
We can traverse either forward or Branches to more than one node
backward
Example : array, stacks, queues, Can’t be traversed in a single run
linked list
Example : tree, graphs

4. What is an array?
-> An array is a data structure consisting of a collection of similar data elements, each
identified by one array index.
The elements in array are stored in consecutive memory locations.
Fixed size.
Data elements are stored in continuous memory locations which may not be available
always.
Adding and removing of elements is tough because of shifting the elements from their
positions.

5. What is a multidimensional array?


-> Multidimensional arrays in simple words as an array of arrays. Data in multidimensional
arrays are stored in tabular form (in row-major order). The most used multidimensional
array is the Two-Dimensional Array.
The total number of elements that can be stored in a multidimensional array can be
calculated by multiplying the size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
Two-Dimensional array: Two – dimensional array is the simplest form of a
multidimensional array.

Better method :
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

Three-Dimensional array: Initialization in a Three-Dimensional array is the same as that of


Two-dimensional arrays.
The difference is as the number of dimensions increases so the number of nested braces
will also increase.
Better method :
int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};
6. What is a linked list?
-> Linked list: Consisting of a group of nodes which together represent a sequence.
Under the simplest form, each node is composed of a datum and a reference (in other
words, a link) to the next node in the sequence.
This structure allows for efficient insertion or removal of elements from any position in the
sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node.
The last node is linked to a terminator (NULL pointer) used to signify the end of the list.
Advantage: Provides quick insert and delete operations
Disadvantage: Slow search operations and requires more memory space.(Why?)
Need to traverse one by one element as they are not having Index.
Example : Chain(Hand to hand)

7. Are linked lists of linear or non-linear type?


-> Linked lists are linear type of data structure.
8. How are linked lists more efficient than arrays?
-> Array : Adding and removing of elements is tough because of shifting the elements from
their positions
Linked lists: This structure allows for efficient insertion or removal of elements from any
position in the sequence.
That why linked lists more efficient than arrays.

9. Explain the scenarios where you can use linked lists and arrays.
-> The scenarios where we use linked list over array:
When we do not know the exact number of elements beforehand.
When we know that there would be large number of add or remove operations.
Less number of random access operations.
Where we use arrays over the linked list:
When we need to index or randomly access elements more frequently.
When we need speed while iterating over the elements in the sequence.
Due to the nature of arrays and linked list, it is safe to say that filled arrays use less memory
than linked lists.

10. What is a doubly-linked list (DLL)? What are its applications.


-> Doubly-linkwd list: Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either
forward and backward easily as compared to Single Linked List.
Application: Dynamic memory allocation : We use linked list of free blocks.
Performing arithmetic operations on long integers.
Representing sparse matrices.

11. What is a stack? What are the applications of stack?


-> Stack: It is an ordered group of homogeneous items of elements.
The last element to be added is the first to be removed (LIFO: Last In, First Out).
Application: Evaluation of Arithmetic Expressions
Backtracking
Reverse a Data
Processing Function Calls

12. What is a queue? What are the applications of queue?


-> Queue: A Queue is an ordered collection of items from which items may be deleted at one
end (called the front of the queue) and into which items may be inserted at the other end
(the rear of the queue).
Application: Widely used as waiting lists for single shared resource like printer, disk, CPU.
Queues are used as buffers on MP3 players and portable CD players.

13. How is a stack different from a queue?


->
Stacks Queues
Stacks are based on the LIFO principle, i.e., Queues are based on the FIFO principle, i.e.,
the element inserted at the last, is the first the element inserted at the first, is the first
element to come out of the list. element to come out of the list.
Insertion and deletion in stacks takes place Insertion and deletion in queues takes place
only from one end of the list called the top. from the opposite ends of the list. The
insertion takes place at the rear of the list
and the deletion takes place from the front of
the list.
Insert operation is called push operation. Insert operation is called enqueue operation.
Delete operation is called pop operation. Delete operation is called dequeue
operation.
In stacks we maintain only one pointer to In queues we maintain two pointers to access
access the list, called the top, which always the list. The front pointer always points to
points to the last element present in the list. the first element inserted in the list and is still
present, and the rear pointer always points
to the last inserted element.
Stack is used in solving problems works on Queue is used in solving problems having
recursion. sequential processing.

14. Explain the process behind storing a variable in memory.


->

15. How to implement a queue using stack?


-> Method 1 (By making enQueue operation costly) : This method makes sure that oldest
entered element is always at the top of stack 1, so that deQueue operation just pops from
stack1. To put the element at top of stack1, stack2 is used.
Method 2 (By making deQueue operation costly) : In this method, in en-queue operation,
the new element is entered at the top of stack1. In de-queue operation, if stack2 is empty
then all the elements are moved to stack2 and final top of stack2 is returned.
Method 2 is definitely better than method 1.
Method 1 moves all the elements twice in enQueue operation, while method 2 (in deQueue
operation) moves the elements once and moves elements only if stack2 empty. So, the
amortized complexity of the dequeue operation becomes ϴ (1).

16. How do you implement stack using queues?


-> Method 1 (By making push operation costly) :
This method makes sure that newly entered element is always at the front of ‘q1’, so that
pop operation just dequeues from ‘q1’. ‘q2’ is used to put every new element at front of
‘q1’.
Method 2 (By making pop operation costly) :
In push operation, the new element is always enqueued to q1. In pop() operation, if q2 is
empty then all the elements except the last, are moved to q2. Finally the last element is
dequeued from q1 and returned.

17. What is a tree data structure?


-> A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such
that each node of the tree stores a value, a list of references to nodes (the “children”).
Recursive Definition: A tree consists of a root, and zero or more subtrees T1, T2, … , Tk such
that there is an edge from the root of the tree to the root of each subtree.

18. What is the maximum number of nodes in a binary tree of height k


-> The maximum number of nodes in a binary tree of height k is 2^(k+1) – 1.

19. What are tree traversals?


-> Tree traversal means traversing or visiting each node of a tree.
Depth First Traversals:
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Breadth-First or Level Order Traversal: 1 2 3 4 5

20. What are the applications of graph data structure?


->
 In Computer science graphs are used to represent the flow of computation.
 In Facebook, users are considered to be the vertices and if they are friends then there
is an edge running between them. Facebook’s Friend suggestion algorithm uses graph
theory. Facebook is an example of undirected graph.
 In World Wide Web, web pages are considered to be the vertices. There is an edge
from a page u to other page v if there is a link of page v on page u. This is an example
of Directed graph. It was the basic idea behind Google Page Ranking Algorithm.

21. What is an AVL Tree?


-> An AVL (Adelson-Velskii and Landis) tree is a height balance tree. These trees are binary
search trees in which the heights of two siblings are not permitted to differ by more than
one.
i.e. Height of the left subtree - height of the right subtree | ≤ 1
Searching time in a binary search tree is O(h), where h is the height of the tree. For efficient
searching, it is necessary that height should be kept to minimum. A full binary search tree
with n nodes will have a height of O(log, n). In practice, it is very difficult to control the
height of a BST. It lies between O(n) to O(log₂ n). An AVL tree is a close approximation of full
binary search tree.

22. What is the difference between tree and graph data structure?
->
Graph Tree
Graph is a non-linear data structure. Tree is a non-linear data structure.
Tree is a non-linear data structure. It is a collection of nodes and edges.
Each node can have any number of edges. General trees consist of the nodes having any
number of child nodes. But in case of binary
trees every node can have at the most two
child nodes.
There is no unique node called root in graph. There is a unique node called root in trees.
A cycle can be formed. There will not be any cycle.
Applications: For finding shortest path in Applications: For game trees, decision trees,
networking graph is used. the tree is used.
23. What is the difference between the Breadth-First Search (BFS) and Depth First Search
(DFS)?
->
BFS DFS
BFS stands for Breadth First Search. DFS stands for Depth First Search.
BFS(Breadth First Search) uses Queue data DFS(Depth First Search) uses Stack data
structure for finding the shortest path. structure.
BFS can be used to find single source In DFS, we might traverse through more
shortest path in an unweighted graph, edges to reach a destination vertex from a
because in BFS, we reach a vertex with source.
minimum number of edges from a source
vertex.
BFS is more suitable for searching vertices DFS is more suitable when there are
which are closer to the given source. solutions away from source.
BFS considers all neighbors first and DFS is more suitable for game or puzzle
therefore not suitable for decision making problems. We make a decision, then explore
trees used in games or puzzles. all paths through this decision. And if this
decision leads to win situation, we stop.
The Time complexity of BFS is O(V + E) when The Time complexity of DFS is also O(V + E)
Adjacency List is used and O(V^2) when when Adjacency List is used and O(V^2)
Adjacency Matrix is used, where V stands for when Adjacency Matrix is used, where V
vertices and E stands for edges. stands for vertices and E stands for edges.
Here, siblings are visited before the children Here, children are visited before the siblings

24. How do you know when to use DFS over BFS?


-> Comparing BFS and DFS, the big advantage of DFS is that it has much lower memory
requirements than BFS, because it’s not necessary to store all of the child pointers at each
level. Depending on the data and what you are looking for, either DFS or BFS could be
advantageous.
example is Facebook; Suggestion on Friends of Friends. We need immediate friends for
suggestion where we can use BFS. May be finding the shortest path or detecting the cycle
(using recursion) we can use DFS.

25. What is topological sorting in a graph?


-> The topological sorting for a directed acyclic graph is the linear ordering of vertices. For
every edge U-V of a directed graph, the vertex u will come before vertex v in the ordering.
As we know that the source vertex will come after the destination vertex, so we need to
use a stack to store previous elements. After completing all nodes, we can simply display
them from the stack.

26. In circular queue, the value of REAR would be?


-> In circular queue, the value of REAR would be rear = maxqueue-1 .

27. Applications of Data Structures and Algorithms.


-> Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in
a certain order to get the desired output. Algorithms are generally created independent of
underlying languages, i.e. an algorithm can be implemented in more than one programming
language.
From the data structure point of view, following are some important categories of
algorithms-
 Search − Algorithm to search an item in a data structure.
 Sort − Algorithm to sort items in a certain order.
 Insert − Algorithm to insert item in a data structure.
 Update − Algorithm to update an existing item in a data structure.
 Delete − Algorithm to delete an existing item from a data structure.
The following computer problems can be solved using Data Structures −
 Fibonacci number series
 Knapsack problem
 Tower of Hanoi
 All pair shortest path by Floyd-Warshall
 Shortest path by Dijkstra
 Project scheduling
28. Name various operations that can be performed in DSA.
->
1. Create:- The create operation results in reserving memory for program elements. This
can be done by declaration statement. Creation of data structure may take place either
during compile-time or run-time. malloc() function of C language is used for creation.
2. Destroy:- Destroy operation destroys memory space allocated for specified data
structure. free() function of C language is used to destroy data structure.
3. Selection:- Selection operation deals with accessing a particular data within a data
structure.
4. Updation:- It updates or modifies the data in the data structure.
5. Searching:- It finds the presence of desired data item in the list of data items, it may also
find the locations of all elements that satisfy certain conditions.
6. Sorting:- Sorting is a process of arranging all data items in a data structure in a particular
order, say for example, either in ascending order or in descending order.
7. Merging:- Merging is a process of combining the data items of two different sorted list
into a single sorted list.
8. Splitting:- Splitting is a process of partitioning single list to multiple list.
9. Traversal:- Traversal is a process of visiting each and every node of a list in systematic
manner.

29. What are Infix, prefix, Postfix notations?


-> Infix Notation :
We write expression in infix notation, e.g. a - b + c, where operators are used in-between
operands. It is easy for us humans to read, write, and speak in infix notation but the same
does not go well with computing devices. An algorithm to process infix notation could be
difficult and costly in terms of time and space consumption.
Prefix Notation :
In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is
also known as Polish Notation.
Postfix Notation :
This notation style is known as Reversed Polish Notation. In this notation style, the operator
is postfixed to the operands i.e., the operator is written after the operands. For example,
ab+. This is equivalent to its infix notation a + b.
30. What are the types of searching used in Data Structures?
->
1. Linear Search
2. Binary Search
3. Jump Search
4. Interpolation Search
5. Exponential Search
6. Sublist Search (Search a linked list in another list)
7. Fibonacci Search
8. The Ubiquitous Binary Search
9. Recursive program to linearly search an element in a given array
10.Recursive function to do substring search
11.Unbounded Binary Search Example (Find the point where a monotonically increasing
function becomes positive first time)

31. What are dynamic arrays?


-> It is a varying-size list data structure that allows items to be added or removed, it may use a
fixed sized array at the back end.
The first part stores the items of the dynamic array and the second part is reserved for new
allocations.
Dynamic arrays share the advantage of arrays, added to it is the dynamic addition of
elements to the array. Memory can be leaked if it is not handled properly during allocation
and deallocation. It is a disadvantage.

32. What are the different types of linked lists?


-> Singly Linked List : It is the simplest type of linked list in which every node contains some
data and a pointer to the next node of the same data type.
Doubly Linked List : A doubly linked list or a two-way linked list is a more complex type of
linked list which contains a pointer to the next as well as the previous node in sequence,
Therefore, it contains three parts are data, a pointer to the next node, and a pointer to the
previous node.
Circular Linked List : A circular linked list is that in which the last node contains the pointer
to the first node of the list.
Doubly Circular linked list : A Doubly Circular linked list or a circular two-way linked list is a
more complex type of linked-list that contains a pointer to the next as well as the previous
node in the sequence.
Header Linked List : A header linked list is a special type of linked list which contains a
header node at the beginning of the list.

33. What are graphs and their uses?


-> A graph is a non-linear data structure, which consists of vertices(or nodes) connected by
edges(or arcs) where edges may be directed or undirected.
Uses :
In Computer science graphs are used to represent the flow of computation.
In Facebook, users are considered to be the vertices and if they are friends then there is an
edge running between them. Facebook’s Friend suggestion algorithm uses graph theory.
Facebook is an example of undirected graph.

34. What is the LIFO and FIFO principle?


-> FIFO :
In this, the new element is inserted below the existing element, So that the oldest element
can be at the top and taken out first.
LIFO :
In this, the new element is inserted above the existing element, So that the newest element
can be at the top and taken out first.

35. What Data Structures make use of pointers?


-> Pointers that are used in linked list have various applications in data structure.
Data structures that make use of this concept include the Stack, Queue, Linked List and
Binary Tree.

36. What is the meaning of the stack overflow condition?


-> A stack overflow is an undesirable condition in which a particular computer program tries to
use more memory space than the call stack has available. In programming, the call stack is a
buffer that stores requests that need to be handled.
The size of a call stack depends on various factors. It is usually defined at the start of a
program. Its size can depend on the architecture of the computer on which the program
runs, the language in which the program is written, and the total amount of available
memory in the system. When a stack overflow occurs as a result of a program's excessive
demand for memory space, that program (and sometimes the entire computer) may crash.
37. What is a Balanced Tree and why is that important?
->

38. What operations can be performed on stacks?


->
 Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow
condition.
 Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
 Peek or Top: Returns the top element of the stack.
 isEmpty: Returns true if the stack is empty, else false.

39. Explain the Tower of Hanoi Problem.


-> The Tower of Hanoi, is a mathematical problem which consists of three rods and multiple
disks. Initially, all the disks are placed on one rod, one over the other in ascending order of
size similar to a cone-shaped tower.
The objective of this problem is to move the stack of disks from the initial rod to another
rod, following these rules:
A disk cannot be placed on top of a smaller disk
No disk can be placed on top of the smaller disk.
The goal is to move all the disks from the leftmost rod to the rightmost rod. To move N
disks from one rod to another, 2^𝑁−1 steps are required. So, to move 3 disks from starting
the rod to the ending rod, a total of 7 steps are required.
40. What are the time complexities of linear search and binary search?
-> Input data needs to be sorted in Binary Search and not in Linear Search
Linear search does the sequential access whereas Binary search access data randomly.
Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
Linear search performs equality comparisons and Binary search performs ordering
Comparisons.

41. What are recursive algorithms?


-> Recursive algorithm is a method of simplification that divides the problem into sub-
problems of the same nature. The result of one recursion is the input for the next recursion.
The repletion is in the self-similar fashion. The algorithm calls itself with smaller input
values and obtains the results by simply performing the operations on these smaller values.
Generation of factorial, Fibonacci number series are the examples of recursive algorithms.

You might also like