Data Structures
Know the applications of Heaps
-Heap sort (sort array in O(nLogn) time) -Priority queue (supports insert, delete, extractmax, decreaseKey operations in O(LogN) time) -Graph algorithms (Dijkstra's, Prim's)
Know the advantages of AVL trees
-It is always height balanced -Height never goes beyond LogN, where N is the number of nodes -Better for searching compared to binary search tree -It has self balancing capabilities
Know the properties of a Binary Search Tree (BST)
-The left subtree of a node contains only nodes with keys less 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 must also be binary search trees -There must be no duplicate nodes
Know the properties of a binary heap
-it's a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible) -suitable to be stored in an array -is either Min Heap or Max Heap
Know how to handle collisions in hashing
-separate chaining -open addressing
Know how insertion operations for BSTs work
-start from the root -compare the desired value with root value -if less than root, recursively call left subtree -else, recursively call right subtree -After reaching the end, just insert that node at left (if less than current) else right
Know algorithm for evaluating postfix expressions
1) Create a stack to store operands (or values). 2) Scan the given expression and do the following for every scanned element. .....a) If the element is a number, push it into the stack .....b) If the element is an operator, pop operands for the operator from the stack. Evaluate the operator and push the result back to the stack 3) When the expression is ended, the number in the stack is the final answer
Know the most commonly used representations of a graph
1. Adjacency Matrix 2. Adjacency List The choice of graph representation is situation-specific. It totally depends on the type of operations to be performed and ease of use.
Know the parts of a binary tree node
1. Data 2. Pointer to left child 3. Pointer to right child
Know the two basic operations that can be performed to rebalance a BST without violating the BST property rule (keys(left) < key(root) < keys(right)
1. Left Rotation 2. Right Rotation (see image)
Know the two types of heaps
1. Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it's children. The same property must be recursively true for all sub-trees in that Binary Tree. 2. Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it's children. The same property must be recursively true for all sub-trees in that Binary Tree.
Know how to get a postfix expression from an infix expression https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/
1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, 1) If the precedence and associativity of the scanned operator is greater than the precedence and associativity of the operator in the stack(or the stack is empty or the stack contains a '(' ), push it. 2) '^' operator is right associative and other operators like '+','-','*' and '/' are left associative. Check especially for a condition when both top of the operator stack and scanned operator are '^'. In this condition the precedence of scanned operator is higher due to it's right associativity. So it will be pushed in the operator stack. In all the other cases when the top of operator stack is same as scanned operator we will pop the operator from the stack because of left associativity due to which the scanned operator has less precedence. 3) Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) 4) If the scanned character is an '(', push it to the stack. 5) If the scanned character is an ')', pop the stack and output it until a '(' is encountered, and discard both the parenthesis. 6) Repeat steps 2-6 until infix expression is scanned. 7) Print the output 8) Pop and output from the stack until it is not empty.
Know the formula to determine the number of different binary trees that can be created from n nodes
2n! / ((n + 1)! n!)
Know what a complete binary tree is
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
Know what a full binary tree is
A Binary Tree is a full binary tree if every node has 0 or 2 children.
Know how to determine the number of levels a binary tree with L leaves has
A Binary Tree with L leaves has at least | Log2L |+ 1 levels
Know what a perfect binary tree is
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.
Know what the definition of a graph is
A Graph is a non-linear data structure consisting of a finite set 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.
Know what a heap is
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
Know how a binary heap is typically represented
A binary heap is typically represented as an array. The root element will be at Arr[0]. Image shows indexes of other nodes for the ith node, i.e., Arr[i]
Know what a balanced binary tree is
A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For Example, the AVL tree maintains O(Log n) height by making sure that the difference between the heights of the left and right subtrees is at most 1.
Know what a doubly linked is and its characteristics
A doubly linked list contains a previous pointer, next pointer, and data Pros: -Can be traversed forward and backward -quickly insert a new node before a given node Cons: -requires extra space for previous pointer -all operations require an extra previous pointer to be maintained
Know the four ways a node can be added to a doubly linked list
A node can be added in four ways: 1) At the front of the DLL 2) After a given node. 3) At the end of the DLL 4) Before a given node.
Know the three ways a node can be added to a singly linked list
A node can be added in three ways 1) At the front of the linked list 2) After a given node. 3) At the end of the linked list.
Know how to get the number of nodes for a skewed binary tree, given the height
A perfect binary tree of height h has h+1 nodes
Know what a binary tree is
A tree whose elements have at most 2 children (a left child and a right child) https://www.geeksforgeeks.org/binary-tree-data-structure/
Know what a weighted graph is
A weight graph is a graph whose edges have a "weight" or "cost". The weight of an edge can represent distance, time, or anything that models the "connection" between the pair of nodes it connects.
Know what AVL trees are mostly used for
AVL tree are mostly used where search is more frequent compared to insert and delete operations
Know what an AVL tree is
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.
Know what an adjacency matrix is
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Know what the Bellman-Ford Algorithm is and its pros/cons
Algorithm to find the shortest path in a graph Given a graph and a source vertex src in graph, find shortest paths from src to all vertices in the given graph. The graph may contain negative weight edges. Pros: -Work for Graphs with negative weights -Simpler than Dijkstra -Suited well for distributed systems. Cons: -Time complexity is O(VE), which is more than Dijkstra. -Does NOT work with undirected graphs with negative edges (it will declare as negative cycle)
Know what an adjacency list is
An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex.
Know what a hash table is
An array that stores pointers to records corresponding to a hash function value equal to the index for the entry
Know the balancing factor range of AVL trees
Balancing Factor ranges -1, 0, and +1 When balancing factor goes beyond the range require rotations to be performed
Know what hash table chaining is
Each cell of a hash table points to a linked list of records that have same hash function value. Chaining is simple, but requires additional memory outside the table.
Know the two parts of a single linked list
Each node in a list consists of at least two parts: 1) data (we can store integer, strings or any type of data). 2) Pointer (Or Reference) to the next node (connects one node to another)
Know what Dijkstra's algorithm is
Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph. Generate a SPT (shortest path tree) with a given source as a root. We maintain two sets, one set contains vertices included in the shortest-path tree, other set includes vertices not yet included in the shortest-path tree. At every step of the algorithm, we find a vertex that is in the other set (set of not yet included) and has a minimum distance from the source.
Know what hashing is
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.
Know how to delete a node from a singly linked list (given a position)
If the node to be deleted is the root, simply delete it. To delete a middle node, we must have a pointer to the node previous to the node to be deleted. So if positions are not zero, we run a loop position-1 times and get a pointer to the previous node.
Know how to get the height of a binary tree
If there are n nodes in binary tree, maximum height of the binary tree is n-1 and minimum height is floor(log2n).
Know how to determine the number of leaf nodes a BST has, if every node has 0 or 2 children
In Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one more than nodes with two children
Know how to get the minimum possible height or minimum number of levels of a binary tree with N nodes
In a Binary Tree with N nodes, minimum possible height or the minimum number of levels is Log2(N+1) If root is considered 0, then formula becomes: | Log2(N+1) | - 1
Know the advantage of adjacency list representation over adjacency matrix representation of a graph
In adjacency list representation, space is saved for sparse graphs. DFS and BSF can be done in O(V + E) time for adjacency list representation. These operations take O(V^2) time in adjacency matrix representation. Here is V and E are number of vertices and edges respectively. Adding a vertex in adjacency list representation is easier than adjacency matrix representation.
Know what hash table open addressing is
In open addressing, all elements are stored in the hash table itself. Each table entry contains either a record or NIL. When searching for an element, we examine the table slots one by one until the desired element is found or it is clear that the element is not in the table.
Know what a hash function is
In simple terms, a hash function maps a big number or string to a small integer that can be used as index in hash table. A good hash function should have following properties: 1) Efficiently computable. 2) Should uniformly distribute the keys (Each table position equally likely for each key)
Know runtime complexity for AVL trees
Insert, delete, and search time is O(log N)
Insertion - Know how to balance AVL tree 'left right case' (y is left child of z and x is right child of y )
Left Rotate y Left Rotate z (see image)
Insertion - Know how to balance AVL tree 'right right case' (y is right child of z and x is right child of y )
Left Rotate z
Know the steps to follow for an AVL tree insert
Let the newly inserted node be w 1) Perform standard BST insert for w. 2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z. 3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements: a) y is left child of z and x is left child of y (Left Left Case) b) y is left child of z and x is right child of y (Left Right Case) c) y is right child of z and x is right child of y (Right Right Case) d) y is right child of z and x is left child of y (Right Left Case)
Know how to insert a node before a given node in a doubly linked list
Let the pointer to this given node be next_node and the data of the new node to be added as new_data. 1) Check if the next_node is NULL or not. If it's NULL, return from the function because any new node can not be added before a NULL 2) Allocate memory for the new node, let it be called new_node 3) Set new_node->data = new_data 4) Set the previous pointer of this new_node as the previous node of the next_node, new_node->prev = next_node->prev 5) Set the previous pointer of the next_node as the new_node, next_node->prev = new_node 6) Set the next pointer of this new_node as the next_node, new_node->next = next_node; 7) If the previous node of the new_node is not NULL, then set the next pointer of this previous node as new_node, new_node->prev->next = new_node 8) Else, if the prev of new_node is NULL, it will be the new head node. So, make (*head_ref) = new_node.
Know what a linked list is and its characteristics
Linked List is a linear data structure. Pros: -not stored at a contiguous location -the elements are linked using pointers. -dynamic size -ease of insertion/deletion Cons: -random access NOT allowed; access elements sequentially starting at head node -extra memory space needed for each element's pointer -not cache friendly
Know how height impacts BST operations
Most of the BST operations (e.g., search, max, min, insert, delete.. etc.) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree.
Know time complexity of inserting node after a given node in singly linked list
O(1)
Know time complexity of inserting node at head of singly linked list
O(1)
Know the time complexity of deleting a node from a doubly linked list
O(1) Since traversal of the linked list is not required so the time complexity is constant.
Know the time complexity for building a binary heap
O(N)
Know the runtime complexity of balanced BSTs
O(log n) time for search, insert and delete
Know the time complexity of postfix expression evaluation
O(n)
Insertion - Know how to balance AVL tree 'right left case' (y is right child of z and x is left child of y )
Right Rotate y Left Rotate z
Insertion - Know how to balance AVL tree 'left left case' (y is left child of z and x is left child of y )
Right Rotate z (see image)
Know what a collision is (hashing)?
Since a hash function gets us a small number for a key which is a big integer or string, there is a possibility that two keys result in the same value. The situation where a newly inserted key maps to an already occupied slot in the hash table is called collision and must be handled using some collision handling technique.
Know how to get the maximum number of nodes in a BST of height 'h'
The Maximum number of nodes in a binary tree of height 'h' is 2^h - 1 Note: if the height of the root is considered to 0 then the formula becomes: 2^(h+1) - 1
Know what a postfix expression is
The expression of the form a b op. When an operator is followed for every pair of operands. The compiler scans the expression either from left to right or from right to left.
Know what an infix expression is
The expression of the form a op b. When an operator is in-between every pair of operands.
Know what the height of an AVL tree always is (also its upper bound)
The height of an AVL tree is always O(Log n) where n is the number of nodes in the tree
Know how to get the maximum number of nodes at level 'l' of a BST
The maximum number of nodes at level 'l' of a binary tree is 2^l Ex.: -For root node, l = 0 and number of nodes = 2^0 or 1 -For node at level 1, max number of nodes is 2^1 or 2
Know how to insert a node at the end of a doubly linked list
The new node is always added after the last node of the given Linked List. Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.
Know how to add a node at the end of a singly linked list
The new node is always added after the last node of the given Linked List. Since a Linked List is typically represented by the head of it, we have to traverse the list till the end and then change the next to last node to a new node.
Know how to insert a node at the front of a doubly linked list
The new node is always added before the head of the given Linked List. And newly added node becomes the new head of DLL. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node
Know how to add a node at the beginning of a singly linked list
The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List.
Know how many undirected graphs (not necessarily connected) can be constructed out of a given set V = {v1, v2, ... vn} of n vertices
There are total n*(n-1)/2 possible edges. For every edge, there are to possible options, either we pick it or don't pick. So total number of possible graphs is 2^(n(n-1)/2)
Know the time complexity of inserting a node at the end of a singly linked list
Time complexity of append is O(n) where n is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work. This method can also be optimized to work in O(1) by keeping an extra pointer to the tail of the linked list
Know how to delete a node from a singly linked list (given a key)
To delete a node from the linked list, we need to do the following steps. 1) Find the previous node of the node to be deleted. 2) Change the next of the previous node. 3) Free memory for the node to be deleted.
Know how a stack can be implemented using a linked list
To keep the Last In First Out order, a stack can be implemented using linked list in two ways: a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from beginning. b) In push operation, if new nodes are inserted at the end of linked list, then in pop operation, nodes must be removed from end.
Know how search operations for BSTs work
To search for a value: -start at the root -compare the desired value with the root value -if the values are equal, search is done -if desired value is less than root, search the left subtree -if it's greater than the root, search the right subtree -continue to go either right or left, until value is found or not found -at each step, one of the subtrees is discarded
Know how to add a node after a given node in a singly linked list
We are given a pointer to a node, and the new node is inserted after the given node.
Know how to insert a node after given node in a doubly linked list
We are given pointer to a node as prev_node, and the new node is inserted after the given node.
Know the time complexity of search and insertion operations for BSTs (worst and best- case)
Worst case: O(n) Average case: O(h), where h is the height of the BST
Know what an undirected graph is
if for every pair of connected nodes, you can go from one node to the other in both directions
Know what a directed graph is
if for every pair of connected nodes, you can only go from one node to another in a specific direction. We use arrows instead of simple lines to represent directed edges.
Know post order traversal of BSTs
left, right, root node
Know in order traversal of BSTs
left, root node, right
Know the maximum number of edges in a cyclic undirected graph with n vertices
n * (n - 1) / 2
Know the maximum number of edges in an acyclic undirected graph with n vertices
n - 1
Know pre order traversal of BSTs
root node, left, right
Know how to delete a node in a doubly linked list
see image