Important Short Questions of DSA
Important Short Questions of DSA
Important Short Questions of DSA
Ans: Data Structure is a way of collecting and organizing data in such a way
that we can perform operations on the data in an effective way. General data
structure types include arrays, linked lists, stacks, Queues etc.
i) Big – Oh(O)
ii) Big – Omega(Ω)
iii) Big – Theta(Θ)
Ans: In divide and conquer approach, the problem in hand, is divided into
smaller sub-problems and then each problem is solved independently. The
solution of all sub-problems is finally merged in order to obtain the solution of
the original problem.
1
Q6. Define an abstract data type (ADT)?
Ans: Abstract data type is a data type, whose behavior is defined by a set of
values and set of operations, independent of any particular implementation.
Stacks and Queues are the examples of Abstract data type.
Ans:
PUSH POP
PUSH function is used to insert POP function is used to remove an
an element at the top of the element from the top of the stack.
stack.
The element is added to the The element is removed from the
stack container and the size of stack container and the size of the
the stack is increased by 1. stack is decreased by 1.
Ans: Queue is used when things don't have to be processed immediately, but
have to be processed in First In First Out order. This property of Queue makes
it useful in following kind of scenario.
Ans:
2
Q10. What are doubly link lists?
Ans: Doubly linked list is a variation of linked list in which navigation is possible
in both ways, either forward or backward easily as compared to single linked
list. In doubly linked list a node consists of three parts:
Node data.
Pointer to the next node.
Pointer to the previous node.
Ans: The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Ans: A binary tree is a tree data structure composed of nodes, each of which
has at most, two children, referred to as left and right nodes. The tree starts off
with a single node known as the root.
Ans: Full Binary Tree: A Binary Tree is a full binary tree if every node has 0 or 2
children. We can also say a full binary tree is a binary tree in which all nodes
except leaf nodes have two children.
Complete Binary Tree: A Binary Tree is a Complete Binary Tree if all the levels
are completely filled except possibly the last level and the last level has all keys
as left as possible.
Perfect Binary Tree: A Binary tree is a Perfect Binary Tree in which all the
internal nodes have two children and all leaf nodes are at the same level.
Ans: Hashing is a technique or process of mapping keys, values into the hash
table by using a hash function. It is done for faster access to elements. The
efficiency of mapping depends on the efficiency of the hash function used.
3
Q15. Which sorting algorithm is best if the list is already sorted? Why?
Ans: Insertion sort algorithm is best if the list is already sorted. Because if the
list is already sorted then insertion sort will give Big O(n) complexity.
Ans: Heap is a special case of complete binary tree data structure where the
root-node key is compared with its children and arranged accordingly. If the
value of the root node is less than or equal to either of its children then it is a
min heap. If the value of the root node is greater than or equal to either of its
children than it is a max heap.
Ans:
4
Q20. What is Stack? Why do we use stacks?
Ans: Stack is an abstract data type and linear data structure that follows the
LIFO (Last – IN – First - Out) principle. It can be defined as a container in which
Insertion and deletion can be done from the one end known as the top of the
stack. Usage of Stack is following.
Ans: Shell sort is a generalized version of the insertion sort algorithm. It first
sorts elements that are far apart from each other and successively reduces the
interval between the elements to be sorted. The interval between the
elements is reduced based on the sequence used.
Ans: In Breadth First search (BFS) traversal, It starts at the tree root or some
arbitrary node of a graph, sometimes referred to as a “search key”, and
explores the neighbor nodes first, before moving to the next level neighbors.
Ans: An AVL tree is a balanced binary search tree in which balance factor of
every node is either -1, 0 or +1. Balance factor of a node is the difference
between the heights of left and right subtrees of that node.
5
Q25. What is minimum spanning tree?
Ans: A minimum spanning tree is a spanning tree in which the sum of the
weight of the edges is as minimum as possible.
Ans: An adjacency list represents a graph as an array of linked lists. The index
of the array represents a vertex and each element in its linked list represents
the other vertices that form an edge with the vertex.
Ans: A Graph is a non-linear data structure consisting of nodes and edges. The
nodes are sometimes also referred to as vertices and the edges are lines or
arcs that connect any two nodes in the graph.
Ans: B-tree is a special type of self-balancing search tree in which each node
can contain more than one key and can have more than two children. It is a
6
generalized form of the binary search tree. It is also known as a height-
balanced m-way tree.
Ans:
Ans: FIFO is the acronym of First IN First Out. It means whatever the data is
entered first, it will be the first to be removed also. Queue data structure
works on this method.
Ans: Merge sort is one of the most efficient sorting algorithms. It works on the
principle of Divide and Conquer. Merge sort repeatedly breaks down a
problem into several sub-problems until each sub-problem consists of a single
element and merging those sub-problems in a manner that results into a
sorted list.
7
Q34. Define leaves node in a tree?
Ans: In a tree data structure, the node which does not have a child is called as
LEAF Node. In simple words, a leaf is a node with no child. In a tree data
structure, the leaf nodes are also called as External Nodes.
Q39. Why to use PREFIX and POSTFIX notations when we have simple INFIX
notation?
Ans: Infix notation is easy to read for humans, whereas pre-/postfix notation is
easier to parse for a machine. The big advantage in pre-/postfix notation is that
there never arise any questions like operator precedence.
8
Q40. There are 8, 15, 13 and 14 nodes in 4 different trees. Which one of them
can form a full binary tree? Why?
Ans: A full binary tree with N leaves contains 2N - 1 nodes. It means that the
number of nodes in a full binary tree is always odd, so you can't create a full
binary tree with 8 and 14 but you can create with 13 and 15.
Ans: Binary Search Tree is a node-based binary tree data structure which has
the following properties:
The left subtree of a node contains only nodes with keys lesser than the
node’s key.
The right subtree of a node contains only nodes with keys greater than
the node’s key.
The left and right subtree each must also be a binary search tree.
Ans: A new item is always inserted at the leaf node. Start searching from the
root till a leaf node is hit, while searching if new value is greater than current
node move to right child else to left child. Once a leaf node is found, the new
node is added as a child of the leaf node.
Ans: A binary search is an algorithm that is best applied to search a list or array
when the elements are already in sorted order.
Ans: The main difference between file structure and storage structure is based
on memory area that is being accessed.
9
Q45. Which data structure is applied when dealing with a recursive function?
Ans: An ordered list is a collection of items where each item holds a relative
position that is based upon some underlying characteristic of the item. The
ordering is typically either ascending or descending and we assume that list
items have a meaningful comparison operation that is already defined.
Ans: Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details or implementation. It
is one of the most essential feature of Object Oriented programming.
Ans: Data structure is important 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 etc.
Q49. What is the minimum number of nodes that a binary tree can have?
Ans:
Q50. What is a cycle in a graph, also define cyclic graph, acyclic, uni-cyclic?
10
Ans: In graph theory, a path that starts from a given vertex and ends at the
same vertex is called a cycle. A cyclic graph is a graph containing at least one
graph cycle. A graph that is not cyclic is said to be acyclic. A cyclic graph
possessing exactly one cycle is called a uni-cyclic graph.
Ans:
Ans: The pointer is a variable which stores the address of another variable i.e.,
direct address of the memory location. The general form of a pointer variable
declaration is.
type *var-name;
Ans: A hash table is a data structure that stores key value pairs. Each value is
assigned a unique key that is generated using a hash function. The name of the
key is used to access its associated value. This makes searching for values in a
hash table very fast, irrespective of the number of items in the hash table.
Ans:
11
Q55. Define acyclic graph with an example?
Ans: Circular Queue is a linear data structure in which the operations are
performed based on FIFO (First In First Out) principle and the last position is
connected back to the first position to make a circle. It is also called ‘Ring
Buffer’. Circular queue is used in Computer controlled Traffic Signal System. It
is also used for CPU scheduling and Memory management.
Q58. Are linked lists considered linear or non-linear data structures? give
reason?
Ans: It actually 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. Linked list
access data in a linear fashion. But they store data in non-linear fashion.
Q59. What are tree traversals? How many traversals of binary tree are
possible?
Ans: Traversal is a process to visit all the nodes of a tree and may print their
values too. There are three ways which we use to traverse a tree,
In-order Traversal
12
Pre-order Traversal
Post-order Traversal
Ans: We should not use sequential search when list or array is already in
sorted order. The sequential search is used whenever the list is not ordered.
Ans: The following two are the most commonly used representations of a
graph.
1. Adjacency Matrix
2. Adjacency List
Ans: A linked list is a linear data structure that includes a series of connected
nodes. Here, each node store the data and the address of the next node.
For Example:
13
IsEmpty: Check if the queue is empty
IsFull: Check if the queue is full
Peek: Get the value of the front of the queue without removing it
Ans: The selection sort algorithm sorts an array by repeatedly finding the
minimum element from unsorted part and putting it at the beginning. The
algorithm maintains two subarrays in a given array.
Ans: Tower of Hanoi is a mathematical puzzle where we have three rods and n
disks. The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
Xn = Xn-1 + Xn-2
Ans: A recursive function is a function that calls itself during its execution. The
process may repeat several times.
Base Case: You must always have at least one case that can be solved
without recursion.
Make Progress: Any recursive call must make progress toward a base
case.
Design Rule: Always assume that the recursive call works.
Compound Interest: Never duplicate work by solving the same instance
of a problem in separate recursive calls.
Ans:
15
Q72. What is meant by a sorting algorithm is stable?
Ans: A sorting algorithm is said to be stable if two objects with equal keys
appear in the same order in sorted output as they appear in the input unsorted
array.
1.
Ans: A Queue is an abstract data type and linear data structure which follows
the First In First Out (FIFO) rule - the item that goes in first is the item that
comes out first.
Ans:
Array Stack
Array contains elements of the same Stack can contain elements of
data type. different data types
Any element can be accessed using Only the topmost element can be read
16
the array index. or removed at a time.
Basic operations include insert, delete, Basic operations are push, pop and
modify, traverse, sort, search. peek.
Ans: linked lists are best suited if the size of the structure and the data in the
structure are constantly changing
Ans: Bubble sort has a worst-case and average-case complexity of О(n2) and
the best-case complexity of bubble sort is only O(n).
Q78. What is index number of the middle element of an array of size 11?
17