Data structures and algorithms, Schemes and Mind Maps of Computer science

quick interview notes about Data structures and algorithms for complete summary and interview guide

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 10/04/2023

jhgfd-ghj
jhgfd-ghj 🇮🇳

4 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INTERVIEW QUESTIONS AND
ANSWERS DATA STRUCTURE
AND ALGORITHM
1. Can you explain the difference between file structure and storage structure?
File Structure: Representation of data into secondary or auxiliary memory say any device such as hard
disk or pen drives that stores data which remains intact until manually deleted is known as a file
structure representation.
Storage Structure: In this type, data is stored in the main memory i.e RAM, and is deleted once the
function that uses this data gets completely executed.
The difference is that storage structure has data stored in the memory of the computer system, whereas
file structure has the data stored in the auxiliary memory.
2. Can you tell how linear data structures differ from non-linear data structures?
If the elements of a data structure result in a sequence or a linear list then it is called a linear data
structure. Whereas, traversal of nodes happens in a non-linear fashion in non-linear data structures.
Lists, stacks, and queues are examples of linear data structures whereas graphs and trees are the
examples of non-linear data structures.
3. What is an array?
Arrays are the collection of similar types of data stored at contiguous memory locations.
It is the simplest data structure where the data element can be accessed randomly just by using its index
number.
4. What is a multidimensional array?
Multi-dimensional arrays are those data structures that span across more than one dimension.
This indicates that there will be more than one index variable for every point of storage. This type of
data structure is primarily used in cases where data cannot be represented or stored using only one
dimension. Most commonly used multidimensional arrays are 2D arrays.
2D arrays emulates the tabular form structure which provides ease of holding the bulk of data that are
accessed using row and column pointers.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Data structures and algorithms and more Schemes and Mind Maps Computer science in PDF only on Docsity!

INTERVIEW QUESTIONS AND

ANSWERS DATA STRUCTURE

AND ALGORITHM

  1. Can you explain the difference between file structure and storage structure? File Structure: Representation of data into secondary or auxiliary memory say any device such as hard disk or pen drives that stores data which remains intact until manually deleted is known as a file structure representation. Storage Structure: In this type, data is stored in the main memory i.e RAM, and is deleted once the function that uses this data gets completely executed. The difference is that storage structure has data stored in the memory of the computer system, whereas file structure has the data stored in the auxiliary memory.
  2. Can you tell how linear data structures differ from non-linear data structures? If the elements of a data structure result in a sequence or a linear list then it is called a linear data structure. Whereas, traversal of nodes happens in a non-linear fashion in non-linear data structures. Lists, stacks, and queues are examples of linear data structures whereas graphs and trees are the examples of non-linear data structures.
  3. What is an array? Arrays are the collection of similar types of data stored at contiguous memory locations. It is the simplest data structure where the data element can be accessed randomly just by using its index number.
  4. What is a multidimensional array? Multi-dimensional arrays are those data structures that span across more than one dimension. This indicates that there will be more than one index variable for every point of storage. This type of data structure is primarily used in cases where data cannot be represented or stored using only one dimension. Most commonly used multidimensional arrays are 2D arrays. 2D arrays emulates the tabular form structure which provides ease of holding the bulk of data that are accessed using row and column pointers.
  1. What is a linked list? A linked list is a data structure that has sequence of nodes where every node is connected to the next node by means of a reference pointer. The elements are not stored in adjacent memory locations. They are linked using pointers to form a chain. This forms a chain-like link for data storage.

Each node element has two parts: a data field a reference (or pointer) to the next node. The first node in a linked list is called the head and the last node in the list has the pointer to NULL. Null in the reference field indicates that the node is the last node. When the list is empty, the head is a null reference.

  1. Are linked lists of linear or non-linear type? Linked lists can be considered both linear and non-linear data structures. This depends upon the application that they are used for.

When linked list is used for access strategies, it is considered as a linear data-structure. When they are used for data storage, it can be considered as a non-linear data structure.

  1. How are linked lists more efficient than arrays? Insertion and Deletion Insertion and deletion process is expensive in an array as the room has to be created for the new elements and existing elements must be shifted. But in a linked list, the same operation is an easier process, as we only update the address present in the next pointer of a node. Dynamic Data Structure Linked list is a dynamic data structure that means there is no need to give an initial size at the time of creation as it can grow and shrink at runtime by allocating and deallocating memory. Whereas, the size of an array is limited as the number of items is statically stored in the main memory. No wastage of memory As the size of a linked list can grow or shrink based on the needs of the program, there is no memory wasted because it is allocated in runtime. In arrays, if we declare an array of size 10 and store only 3 elements in it, then the space for 3 elements is wasted. Hence, chances of memory wastage is more in arrays.

Push, pop, and top (or peek) are the basic operations of a stack.

Following are some of the applications of a stack: Check for balanced parentheses in an expression Evaluation of a postfix expression Problem of Infix to postfix conversion Reverse a string

  1. What is a queue? What are the applications of queue? A queue is a linear data structure that follows the FIFO (First In First Out) approach for accessing elements. Dequeue from the queue, enqueue element to the queue, get front element of queue, and get rear element of queue are basic operations that can be performed.

Some of the applications of queue are: CPU Task scheduling BFS algorithm to find shortest distance between two nodes in a graph. Website request processing Used as buffers in applications like MP3 media player, CD player, etc. Managing an Input stream

  1. How is a stack different from a queue? In a stack, the item that is most recently added is removed first whereas in queue, the item least recently added is removed first.
  2. Explain the process behind storing a variable in memory. A variable is stored in memory based on the amount of memory that is needed. Following are the steps followed to store a variable: The required amount of memory is assigned first. Then, it is stored based on the data structure being used. Using concepts like dynamic allocation ensures high efficiency and that the storage units can be accessed based on requirements in real time.
  1. How to implement a queue using stack? A queue can be implemented using two stacks. Let q be the queue andstack1 and stack2 be the 2 stacks for implementing q. We know that stack supports push, pop, peek operations and using these operations, we need to emulate the operations of queue - enqueue and dequeue. Hence, queue q can be implemented in two methods (Both the methods use auxillary space complexity of O(n)): By making enqueue operation costly: Here, the oldest element is always at the top of stack1 which ensures dequeue operation to occur in O(1) time complexity. To place element at top of stack1, stack2 is used. Pseudocode:

Enqueue: Here time complexity will be O(n) enqueue(q, data): While stack1 is not empty: Push everything from stack1 to stack2. Push data to stack Push everything back to stack1. Dequeue: Here time complexity will be O(1)

deQueue(q): If stack1 is empty then error else Pop an item from stack1 and return it By making dequeue operation costly: Here, for enqueue operation, the new element is pushed at the top of stack1. Here, the enqueue operation time complexity is O(1). In dequeue, if stack2 is empty, all elements from stack1 are moved to stack2 and top of stack2 is the result. Basically, reversing the list by pushing to a stack and returning the first enqueued element. This operation of pushing all elements to new stack takes O(n) complexity. Pseudocode: Enqueue: Time complexity: O(1)

In pop operation, all the elements from q1 except the last remaining element, are pushed to q2 if it is empty. That last element remaining of q1 is dequeued and returned. Pseudocode: Push element to stack s : Here push takes O(1) time complexity. push(s,data): Enqueue data to q Pop element from stack s: Takes O(n) time complexity. pop(s): Step1: Dequeue every elements except the last element from q1 and enqueue to q2. Step2: Dequeue the last item of q1, the dequeued item is stored in result variable. Step3: Swap the names of q1 and q2 (for getting updated data after dequeue) Step4: Return the result.

  1. What is hashmap in data structure? Hashmap is a data structure that uses implementation of hash table data structure which allows access of data in constant time (O(1)) complexity if you have the key.
  2. 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.
  3. How does HashMap handle collisions in Java? The java.util.HashMap class in Java uses the approach of chaining to handle collisions. In chaining, if the new values with same key are attempted to be pushed, then these values are stored in a linked list stored in bucket of the key as a chain along with the existing value. In the worst case scenario, it can happen that all key might have the same hashcode, which will result in the hash table turning into a linked list. In this case, searching a value will take O(n) complexity as opposed to O(1) time due to the nature of the linked list. Hence, care has to be taken while selecting hashing algorithm.
  4. 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.
  5. 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.

  1. What is a priority queue? A priority queue is an abstract data type that is like a normal queue but has priority assigned to elements. Elements with higher priority are processed before the elements with a lower priority. In order to implement this, a minimum of two queues are required - one for the data and the other to store the priority.
  2. Can we store a duplicate key in HashMap? No, duplicate keys cannot be inserted in HashMap. If you try to insert any entry with an existing key, then the old value would be overridden with the new value. Doing this will not change the size of HashMap. This is why the keySet() method returns all keys as a SET in Java since it doesn't allow duplicates.
  3. What is a tree data structure? Tree is a recursive, non-linear data structure consisting of the set of one or more data nodes where one node is designated as the root and the remaining nodes are called as the children of the root. Tree organizes data into hierarchial manner. The most commonly used tree data structure is a binary tree and its variants. Some of the applications of trees are: Filesystems —files inside folders that are inturn inside other folders. Comments on social media — comments, replies to comments, replies to replies etc form a tree representation. Family trees — parents, grandparents, children, and grandchildren etc that represents the family hierarchy.
  1. Write Java code to count number of nodes in a binary tree. int countNodes(Node root) { int count = 1; //Root itself should be counted if (root ==null) return 0; else { count += countNodes(root.left); count += countNodes(root.right); return count; } }
  2. What are tree traversals? Tree traversal is a process of visiting all the nodes of a tree. Since root (head) is the first node and all nodes are connected via edges (or links) we always start with that node. There are three ways which we use to traverse a tree − Inorder Traversal: Algorithm: Step 1. Traverse the left subtree, i.e., call Inorder(root.left) Step 2. Visit the root. Step 3. Traverse the right subtree, i.e., call Inorder(root.right) Inorder traversal in Java: // Print inorder traversal of given tree. void printInorderTraversal(Node root) { if (root == null) return;

//first traverse to the left subtree printInorderTraversal(root.left);

//then print the data of node System.out.print(root.data + " ");

//then traverse to the right subtree printInorderTraversal(root.right); } Uses: In binary search trees (BST), inorder traversal gives nodes in ascending order. Preorder Traversal: Algorithm: Step 1. Visit the root. Step 2. Traverse the left subtree, i.e., call Preorder(root.left) Step 3. Traverse the right subtree, i.e., call Preorder(root.right) Preorder traversal in Java: // Print preorder traversal of given tree. void printPreorderTraversal(Node root) { if (root == null) return; //first print the data of node System.out.print(root.data + " ");

//then traverse to the left subtree printPreorderTraversal(root.left);

//then traverse to the right subtree printPreorderTraversal(root.right);

Consider the following tree as an example, then:

Inorder Traversal => Left, Root, Right : [4, 2, 5, 1, 3] Preorder Traversal => Root, Left, Right : [1, 2, 4, 5, 3] Postorder Traversal => Left, Right, Root : [4, 5, 2, 3, 1]

  1. What is a Binary Search Tree? A binary search tree (BST) is a variant of binary tree data structure that stores data in a very efficient manner such that the values of the nodes in the left sub-tree are less than the value of the root node, and the values of the nodes on the right of the root node are correspondingly higher than the root. Also, individually the left and right sub-trees are their own binary search trees at all instances of time.
  2. What is an AVL Tree? AVL trees are height balancing BST. AVL tree checks the height of left and right sub-trees and assures that the difference is not more than 1. This difference is called Balance Factor and is calculates as. BalanceFactor = height(left subtree) − height(right subtree)
  3. Print Left view of any binary trees. The main idea to solve this problem is to traverse the tree in pre order manner and pass the level information along with it. If the level is visited for the first time, then we store the information of the current node and the current level in the hashmap. Basically, we are getting the left view by noting the first node of every level. At the end of traversal, we can get the solution by just traversing the map. Consider the following tree as example for finding the left view:

Left view of a binary tree in Java: import java.util.HashMap;

//to store a Binary Tree node class Node { int data;

Node left = null, right = null;

Node(int data) { this.data = data; } } public class InterviewBit { // traverse nodes in pre-order way public static void leftViewUtil(Node root, int level, HashMap<Integer, Integer> map) { if (root == null) { return; }

// if you are visiting the level for the first time // insert the current node and level info to the map if (!map.containsKey(level)) { map.put(level, root.data); }

leftViewUtil(root.left, level + 1, map); leftViewUtil(root.right, level + 1, map); }

// to print left view of binary tree public static void leftView(Node root) { // create an empty HashMap to store first node of each level

  1. What are the applications of graph data structure? Graphs are used in wide varieties of applications. Some of them are as follows:

Social network graphs to determine the flow of information in social networking websites like facebook, linkedin etc. Neural networks graphs where nodes represent neurons and edge represent the synapses between them Transport grids where stations are the nodes and routes are the edges of the graph. Power or water utility graphs where vertices are connection points and edge the wires or pipes connecting them. Shortest distance between two end points algorithms.

  1. How do you represent a graph? We can represent a graph in 2 ways:

Adjacency matrix: Used for sequential data representation

Adjacency list: Used to represent linked data

  1. What is the difference between tree and graph data structure? 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. Tree provides insights on relationship between nodes in a hierarchical manner and graph follows a network model.
  2. What is the difference between the Breadth First Search (BFS) and Depth First Search (DFS)? BFS and DFS both are the traversing methods for a graph. Graph traversal is nothing but the process of visiting all the nodes of the graph. The main difference between BFS and DFS is that BFS traverses level by level whereas DFS follows first a path from the starting to the end node, then another path from the start to end, and so on until all nodes are visited. Furthermore, BFS uses queue data structure for storing the nodes whereas DFS uses the stack for traversal of the nodes for implementation.

DFS yields deeper solutions that are not optimal, but it works well when the solution is dense whereas the solutions of BFS are optimal. You can learn more about BFS here: Breadth First Search and DFS here: Depth First Search.

  1. How do you know when to use DFS over BFS? The usage of DFS heavily depends on the structure of the search tree/graph and the number and location of solutions needed. Following are the best cases where we can use DFS: If it is known that the solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth first search (DFS) might take an extremely long time, but BFS could be faster. If the tree is very wide, a BFS might need too much memory, so it might be completely impractical. We go for DFS in such cases. If solutions are frequent but located deep in the tree we opt for DFS.
  2. What is topological sorting in a graph? Topological sorting is a linear ordering of vertices such that for every directed edge ij, vertex i comes before j in the ordering. Topological sorting is only possible for Directed Acyclic Graph (DAG). Applications: jobs scheduling from the given dependencies among jobs. ordering of formula cell evaluation in spreadsheets ordering of compilation tasks to be performed in make files, data serialization resolving symbol dependencies in linkers. Topological Sort Code in Java: // V - total vertices // visited - boolean array to keep track of visited nodes // graph - adjacency list. // Main Topological Sort Function. void topologicalSort() { Stack stack = new Stack();

topologicalSortUtil(i, visited, stack); }

// Push current vertex to stack that saves result stack.push(new Integer(v)); }

  1. 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);