Most interviews these days are whiteboard interviews demonstrating your knowledge of DSA. Unless you're interviewing for a top company, you can expect the questions to be fairly standard. The reason why questions about Data Structures are asked, has little to do with the actual problem asked. Whiteboarding a question like that shows a lot of knowledge that a programmer should have: analyzing a problem, designing a solution, syntax of a language, determining test cases and walking through them, knowledge of language specific things, and critical thinking. Follow along and check 32 Advanced Data Structures Interview Questions with Answers you may be asked and must be ready for on your next programming interview.
A normal tree has no restrictions on the number of children each node can have. A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element.
There are three different types of binary trees:
Trees are well-known as a non-linear data structure. They don’t store data in a linear way. They organize data hierarchically.
A tree is a collection of entities called nodes. Nodes are connected by edges. Each node contains a value or data or key, and it may or may not have a child node. The first node of the tree is called the root. Leaves are the last nodes on a tree. They are nodes without children.
A stack is a recursive data structure, so it's:
Arrays are:
A hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values. Hash tables implement an associative array, which is indexed by arbitrary objects (keys). A hash table uses a hash function to compute an index, also called a hash value, into an array of buckets or slots, from which the desired value can be found.
A Heap is a special Tree-based data structure which is an almost complete tree that satisfies the heap property:
A common implementation of a heap is the binary heap, in which the tree is a binary tree.
A priority queue is a data structure that stores priorities (comparable values) and perhaps associated information. A priority queue is different from a "normal" queue, because instead of being a "first-in-first-out" data structure, values come out in order by priority. Think of a priority queue as a kind of bag that holds priorities. You can put one in, and you can take out the current highest priority.
A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.
A graph is a common data structure that consists of a finite set of nodes (or vertices) and a set of edges connecting them. A pair (x,y) is referred to as an edge, which communicates that the x vertex connects to the y vertex.
Graphs are used to solve real-life problems that involve representation of the problem space as a network. Examples of networks include telephone networks, circuit networks, social networks (like LinkedIn, Facebook etc.).
Linked lists are very useful when you need :
Using an array based list for these purposes has severe limitations:
A dynamic array is an array with a big improvement: automatic resizing.
One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.
Queue can be classified into following types:
A Binary Heap is a Binary Tree with following properties:
10 10
/ \ / \
20 100 15 30
/ / \ / \
30 40 50 100 40Binary search tree is a data structure that quickly allows to maintain a sorted list of numbers.
O(log n) time.The properties that separates a binary search tree from a regular binary tree are:
O(n).O(n).O(1).O(1).The space complexity of a datastructure indicates how much space it occupies in relation to the amount of elements it holds. For example a space complexity of O(1) would mean that the datastructure alway consumes constant space no matter how many elements you put in there. O(n) would mean that the space consumption grows linearly with the amount of elements in it.
A hashtable typically has a space complexity of O(n).
Graph:
Tree:
Because they help manage your data in more a particular way than arrays and lists. It means that when you're debugging a problem, you won't have to wonder if someone randomly inserted an element into the middle of your list, messing up some invariants.
Arrays and lists are random access. They are very flexible and also easily corruptible. If you want to manage your data as FIFO or LIFO it's best to use those, already implemented, collections.
More practically you should:
Heap is generally preferred for priority queue implementation because heaps provide better performance compared arrays or linked lists:
insert() operation can be implemented by adding an item at end of array in O(1) time.getMax() operation can be implemented by linearly searching the highest priority item in array. This operation takes O(n) time.deleteMax() operation can be implemented by first linearly searching an item, then removing the item by moving all subsequent items one position back.getMax() can be implemented in O(1) time, insert() can be implemented in O(log n) time and deleteMax() can also be implemented in O(log n) time.This can be achieved by traversing the tree in the in-order manner that is, left the child -> root ->right node.
In an in-order traversal, first the left sub-tree is traversed, then the root is visited, and finally the right sub-tree is traversed.
One simple way of solving this problem is to start with an empty doubly linked list. While doing the in-order traversal of the binary tree, keep inserting each element output into the doubly linked list. But, if we look at the question carefully, the interviewer wants us to convert the binary tree to a doubly linked list in-place i.e. we should not create new nodes for the doubly linked list.
This problem can be solved recursively using a divide and conquer approach. Below is the algorithm specified.
Recursive solution has O(h) memory complexity as it will consume memory on the stack up to the height of binary tree h. It will be O(log n) for balanced tree and in worst case can be O(n).
public static void BinaryTreeToDLL(Node root) {
if (root == null)
return;
BinaryTreeToDLL(root.left);
if (prev == null) { // first node in list
head = root;
} else {
prev.right = root;
root.left = prev;
}
prev = root;
BinaryTreeToDLL(root.right);
if (prev.right == null) { // last node in list
head.left = prev;
prev.right = head;
}
}AVL trees are height balancing binary search tree. It is named after Adelson-Velsky and Landis, the inventors of the AVL tree. AVL tree checks the height of the left and the right sub-trees and assures that the difference is not more than 1. This difference is called the Balance Factor. This allows us to search for an element in the AVL tree in O(log n), where n is the number of elements in the tree.
The balance factor of a node (N) in a binary tree is defined as the height difference:
BalanceFactor(N) = height(left_sutree(N)) − height(right_sutree(N)) // BalanceFactor(N) belongs to the set {-1,0,1}If the balance factor doesn’t equal -1,0, or 1 then our tree is unbalanced, and we need to perform certain operations (called rotations) to balance the tree. Specifically we need to do one or more of 4 tree rotations:
A tree is said to be balanced only when all three conditions satisfy:
Height-balancing requirement:
In a balanced BST, the height of the tree is log n where n is the number of elements in the tree. In the worst case and in an unbalanced BST, the height of the tree can be upto n which makes it same as a linked list. In the worst case each of the operations (lookup, insertion and deletion) takes time O(n) that shall be avoided.
Balanced BST maintains
h=O(log n)so all operations run inO(log n)time.
In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of key, value pairs, such that each possible key appears at most once in the collection. Associative array can be implemented as:
Hash tables are O(1) average and amortized time complexity case complexity, however it suffers from O(n) worst case time complexity (when you have only one bucket in the hash table, then the search complexity is O(n)). A hashtable typically has a space complexity of O(n).
Hash tables suffer from O(n) worst time complexity due to two reasons:
O(n) time (for linked lists)However, it is said to be O(1) average and amortized case because:
O(n), can at most happen after n/2 ops, which are all assumed O(1): Thus when you sum the average time per op, you get : (n*O(1) + O(n)) / n) = O(1)Rust has been Stack Overflow’s most loved language for four years in a row and emerged as a compelling language choice for both backend and system developers, offering a unique combination of memory safety, performance, concurrency without Data races...
Clean Architecture provides a clear and modular structure for building software systems, separating business rules from implementation details. It promotes maintainability by allowing for easier updates and changes to specific components without affe...
Azure Service Bus is a crucial component for Azure cloud developers as it provides reliable and scalable messaging capabilities. It enables decoupled communication between different components of a distributed system, promoting flexibility and resili...