TLDR;
Okay ji, this video is all about data structures and common interview questions around them. It covers arrays, linked lists, stacks, queues, trees, and graphs. Key takeaways include understanding the difference between linear and non-linear data structures, the advantages of linked lists over arrays, LIFO and FIFO principles in stacks and queues, tree traversal methods, and the characteristics of binary search trees and B-trees.
- Data structures are ways to store and organise data.
- Linear data structures include arrays, queues, stacks, and linked lists.
- Non-linear data structures include trees and graphs.
Introduction [0:00]
Namaste! Nishant here is gonna talk about data structure interview questions and answers for all you freshers out there. So, let's dive right in, yeah?
What do you mean by Data Structure? [0:22]
Data structure, basically, is how you store and organise data in your computer's memory so you can get to it and change it easily. Data structures are mainly of two types: linear and non-linear. Linear ones arrange data in a sequence, like a line, where each element is connected to the ones before and after it. Examples include arrays, queues, stacks, and linked lists. Non-linear data structures don't follow a sequence, like trees and graphs.
What is an array? [1:22]
An array is a collection of data items, all of the same type. It's a linear data structure where these items are stored one after the other in memory. Arrays are static, meaning their size is fixed. For example, you can have an array of size 5 that stores integers like 2, 3, 7, 8, and 6. Each item has an index, which is its location in the array. To get the number 8, you need to specify index 3.
What is a Multidimensional Array? [2:14]
A multi-dimensional array is like an array of arrays. 2D arrays and 3D arrays fall under this category. A 2D array stores data in a table format with rows and columns. You can represent it as arrayName[number of rows][number of columns]. A 3D array is a collection of 2D arrays, or multiple tables, represented as arrayName[number of blocks][number of rows][number of columns], where the number of blocks tells you how many 2D arrays there are.
What is a linked List? [3:23]
A linked list is a linear data structure made up of nodes. Each node has a data field to store the data and an address field (a pointer) to store the address of the next node, forming a chain. The first node is called the head, and the last node, the tail, points to null. Linked lists are dynamic, so they can grow or shrink as needed.
What are the advantages of Linked list over Arrays? [4:15]
Linked lists have a few advantages over arrays. They are dynamic, unlike arrays which are static, so they can change size as needed, avoiding memory wastage. Also, inserting or deleting elements is easier in a linked list. For example, if you want to insert 3 into a sorted array like 2, 4, 6, 7, you have to move all elements to the right of 2. In a linked list, you just need to update the pointers.
What is a doubly linked list? What are its advantages over singly linked list? [5:13]
A doubly linked list is like a linked list, but each node has an extra pointer that points to the previous node. This means you can traverse the list in both directions, making it more efficient than a singly linked list. Deletion is also faster because you don't need to traverse the list to find the previous node.
What are the applications of a linked list? [6:13]
Linked lists are used in many places. They're used to implement stacks and queues, maintain directory names in operating systems, and in image viewer software to navigate through images using "next" and "previous" buttons. Music players also use them.
What is a stack? What are the applications of a stack? [6:48]
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last element inserted is the first one removed. Insertion and deletion happen only at the top of the stack. Stacks can be implemented using arrays or linked lists. Common applications include reversing strings and storing visited URLs. When reversing a string, the letters are stored in a stack, and due to LIFO, they come out in reverse order. For visited URLs, when you press the back button, the URLs are fetched from the stack one by one.
What are the basic operations performed on a stack? [8:05]
The basic operations on a stack are:
- Push: Adds an element to the top of the stack.
- Pop: Removes an element from the top of the stack.
- isEmpty: Checks if the stack is empty.
- isFull: Checks if the stack is full.
- Peek: Returns the value of the topmost element without removing it.
What is Queue? [8:53]
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The first element inserted is the first one removed. Insertion happens at the rear end, and deletion happens at the front end.
What are the basic operationa [9:19]
The basic operations of a queue are:
- Enqueue: Inserts an element to the end of the queue.
- Dequeue: Removes an element from the beginning of the queue.
- isEmpty: Checks if the queue is empty.
- Front: Returns the front item from the queue.
- Rear: Returns the last item from the queue.
What is a Tree data structure? [10:02]
A tree is a non-linear, hierarchical data structure made up of nodes connected by edges. Nodes contain data items. Trees provide quicker and easier access to data compared to linear data structures.
Define the basic terminology in tree data structure? [10:32]
Some basic tree terminologies include:
- Edge: A connection between two nodes.
- Parent Node: The predecessor of a node.
- Child Node: The successor of a node.
- Root Node: The topmost node of a tree, without any parent.
- Leaf Node: A node without any child nodes.
- Sibling Node: Child nodes with the same parent node.
- Degree of a Node: The number of sub-trees attached to a node.
- Height of a Node: The number of edges from the node to its most distant leaf node.
- Height of a Tree: The height of the root node.
- Depth of a Node: The number of edges from the node to the root node.
What are Binary Trees? [12:26]
Binary trees are trees where each node has at most two child nodes: a left child and a right child.
Inorder traversal [14:01]
In an inorder traversal, you first visit all the nodes in the left sub-tree, then the root node, and then all the nodes in the right sub-tree.
Preorder traversal [14:15]
In a pre-order traversal, you first visit the root node, then all the nodes in the left sub-tree, and then all the nodes in the right sub-tree.
2. Breadth First Traversal (BFS) [14:45]
Breadth-first traversal visits trees by levels instead of subtrees. First, you visit the root node, then all the child nodes of the root node from left to right, and then go down all the levels in the same manner until you reach the leaf nodes.
What is a Binary search tree? [15:13]
A binary search tree is an ordered or sorted binary tree. It allows you to maintain a sorted list of numbers to quickly search for a number. The value of the left child node is always less than the value of its parent's node, and the value of the right child node is always more than that of its parent node.
What are B-trees? [16:30]
A B-tree is a self-balancing search tree where each node can have more than one key and more than two children. Because multiple keys are stored in a single node, the height of the tree is less compared to other trees, allowing for faster disk access. In B-trees, every node must be at least half-filled before creating a new node. The root node can have a minimum of two children, and all leaf nodes must be at the same level. The creation process is bottom-up.
What is a graph data structure? [17:33]
A graph is a non-linear data structure consisting of vertices and edges that connect the vertices. The set of edges describes the relationship between the vertices.
What is the difference between directed graph and undirected graph? [17:55]
In a directed graph, the edges that connect the vertices have a direction, representing the direction in which the graph can be traversed. In an undirected graph, the edges don't have any direction and can be traversed bidirectionally.