What are the different types of data structures?
Linear data structure: If the elements of a data structure result in a sequence or a linear list then it is called a linear data structure. Example: Arrays, Linked List, Stacks, Queues etc.
Non-linear data structure: If the elements of data structure results in a way that traversal of nodes is not done in a sequential manner, then it is a non linear data structure. Example: Trees, Graphs etc.
How do 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.
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.
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.
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.
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.
How are linked lists more efficient than arrays?
Explain the scenarios where you can use linked lists and arrays.
What is a doubly-linked list (DLL)? What are its applications.
What is a stack? What are the applications of stack?
What is a queue? What are the applications of queue?
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.
Explain the process behind storing a variable in memory.
What is hashmap?
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.
What is the requirement for an object to be used as key or value in HashMap?
How does HashMap handle collisions in Java?
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.
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:
What is a priority queue?
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.
What is a tree data structure?
What are Binary trees?
A binary Tree is a special type of tree where each node can have at most two children. Binary tree is generally partitioned into three disjoint subsets, i.e. the root of the tree, left sub-tree and right sub-tree.
What is the maximum number of nodes in a binary tree of height k?
The maximum nodes are : 2k+1-1 where k >= 1
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)
- 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)
- Postorder Traversal:
- Algorithm:
- Step 1. Traverse the left subtree, i.e., call Postorder(root.left)
- Step 2. Traverse the right subtree, i.e., call Postorder(root.right)
- Step 3. Visit the root.