DSA Interview (1)
DSA Interview (1)
DSA Interview (1)
Data structures can be categorized as linear (like arrays, linked lists, stack and queue ) or non-linear (like
trees, graphs).
Linear data structures : the elements are arranged in sequence one after the other. It can be traversed on a
single run. That is, if we start from the first element, we can traverse all the elements sequentially in a single
pass.
Non linear data structures : elements in non-linear data structures are not in any sequence. Instead they are
arranged in a hierarchical manner where one element will be connected to one or more elements. It requires
multiple runs. That is, if we start from the first element it might not be possible to traverse all the elements
in a single pass.
Arrays use an index-based data structure which helps to identify each of the elements in an array easily using
the index.
An array can also handle complex data structures (Graph) by storing data in a two-dimensional array.
An array is also used to implement other data structures like Stacks, Queues, Heaps, Hash tables
Singly linked lists : A singly linked list is a unidirectional linked list. So, you can only traverse it in one
direction
Doubly linked lists : A doubly linked list is a bi-directional linked list. So, you can traverse it in both directions.
Unlike singly linked lists, its nodes contain one extra pointer called the previous pointer. This pointer points
to the previous node.
Circular linked lists : A circular Linked list is a unidirectional linked list. So, you can traverse it in only one
direction. But this type of linked list has its last node pointing to the head node.
Circular doubly linked lists : A circular doubly linked list is a mixture of a doubly linked list and a circular
linked list. Like the doubly linked list, it has an extra pointer called the previous pointer, and similar to the
circular linked list, its last node points at the head node. This type of linked list is the bi-directional list. So,
you can traverse it in both directions.
The music players also use the same technique to switch between music.
The common operations on a stack are push (insert an element), pop (remove the top element), and peek
(view the top element).
A queue can be defined as an ordered list which enables insert operations to be performed at one end
called REAR (enqueue) and delete operations to be performed at another end called FRONT (dequeue).
Q-14 What are the applications of Queues?
Operating System uses queues for job scheduling
It helps in serving requests on a single shared resource, like a printer, CPU task scheduling, etc.
Simple Queue or Linear Queue : Linear Queue an insertion takes place from one end while the deletion
occurs from another end.
The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three
elements are deleted from the Queue, we cannot insert more elements even though the space is available in
a Linear Queue.
Circular Queue : It is similar to the linear Queue except that the last element of the queue is connected to
the first element.
The drawback that occurs in a linear queue is overcome by using the circular queue. The main advantage of
using the circular queue is better memory utilization.
Priority Queue : It is a special type of queue in which the elements are arranged based on the priority.
Double Ended Queue (or Deque) : Double Ended Queue, insertion and deletion can be done from both ends
of the queue either from the front or rear.
the node contains three fields. The second field stores the data; the first field stores the address of the left
child, and the third field stores the address of the right child. (The above structure can only be defined for
the binary trees because the binary tree can have utmost two children, and generic trees can have more
than two children. )
There are different types of Tree-like Binary Tree, Binary Search Tree, AVL Tree, B-Tree, etc.
Different tree data structures allow quicker and easier access to the data as it is a non-linear data structure.
Q-18 What are the applications of tree?
Domain Name Server(DNS) also uses tree structures, DOM in Html
The tree data structure is also used to store the data in routing tables in the routers.
commonly used in decision-making algorithms in artificial intelligence and machine learning algorithms
the node contains three fields. The second field stores the data; the first field stores the address of the left
child, and the third field stores the address of the right child.
Full/ proper/ strict Binary tree : The tree can only be considered as the full binary tree if each node must
contain either 0 or 2 children.
Complete Binary tree : The complete binary tree is a tree in which all the nodes are completely filled except
the last level and nodes are filled left to right.
Perfect Binary tree : A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.
Degenerate Binary tree : The degenerate binary tree is a tree in which all the internal nodes have only one
children. right-skewed tree as all the nodes have a right child only. left-skewed tree as all the nodes have a
left child only.
Balanced Binary tree : The balanced binary tree is a tree in which both the left and right trees differ by
atmost 1. For example, AVL and Red-Black trees are balanced binary tree.
As compared to array and linked lists, insertion and deletion operations are faster in BST.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. L L rotation, R R
rotation, L R rotation, R L rotation
Mostly it is used when you have fewer insertion and deletion operations but more searching operations.
Application of AVL Tree- Data Analysis and Data Mining and the applications which involve more searches.
Q-22 What is the main reason why AVL trees are useful?
AVL tree always tries to maintain it’s height to O(log(n)) by doing rotations.
The AVL Tree has a bad cost in inserting and removing data because it has to self-sort itself each and every
time. It has a wonderful searching capacity with O(log(n)) running time guaranteed.
Mostly it is used when you have fewer insertion and deletion operations but more searching operations.
The Red-Black tree is used because the AVL tree requires many rotations when the tree is large, whereas the
Red-Black tree requires a maximum of two rotations to balance the tree.
BFS stands for Breadth First Search. DFS stands for Depth First Search.
Queue data structure is used for the BFS traversal. Stack data structure is used for the BFS traversal.
BFS does not use the backtracking concept. DFS uses backtracking to traverse all the unvisited nodes.
BFS traversal is optimal for those vertices which are DFS traversal is optimal for those graphs in which
to be searched closer to the source vertex. solutions are away from the source vertex.
BFS is employed in network broadcasting, social DFS finds its utility in maze solving, topological sorting,
network analysis, and shortest path finding. and detecting cycles in graphs.
The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two
nodes in the graph.
There are two ways to store Graphs into the computer's memory: Sequential representation (or, Adjacency
matrix representation), Linked list representation (or, Adjacency list representation)
The World Wide Web huge collection of documents pointing to each other via hyperlinks.
Social Networks (facebook) store on their servers an internal graph representation of the human social
network.
Etc..
Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at
the root of the tree. (It either follows max heap or min heap property)
In a max-heap, each parent node is greater than or equal to its child nodes. In a min-heap, each parent node
is less than or equal to its child nodes.
Heapify : It is the process to rearrange the elements to maintain the property of heap data structure.