Final Worksheet On Data Structure
Final Worksheet On Data Structure
1. What are the advantages of a linked list over an array? In which scenarios do we use
Linked List and when Array?
Insertion and Deletion: Insertion and deletion of nodes is an easier process, as we only
update the address present in the next pointer of a node. It’s expensive to do the same in
an array as the room has to be created for the new elements and existing elements must
be shifted.
Dynamic Data Structure: As a linked list is a dynamic data structure, there is no need to
give an initial size as it can grow and shrink at runtime by allocating and deallocating
memory. However, the size is limited in an array as the number of elements is statically
stored in the main memory.
No Wastage of Memory: As the size of a linked list can increase or decrease depending
on the demands of the program, and memory is allocated only when required, there is no
memory wasted. In the case of an array, there is memory wastage. For instance, if we
declare an array of size 10 and store only five elements in it, then the space for five
elements is wasted.
Implementation: Data structures like stack and queues are more easily implemented
using a linked list than an array.
When we want to insert items in the middle of the list, such as when implementing a priority
queue
Some scenarios in which we use array over the linked list are:
When we know the number of elements in the array beforehand, so we can allocate the
correct amount of memory
When we need speed when iterating through all the elements in the sequence
When memory is a concern; filled arrays use less memory than linked lists, as each element in
the array is the data but each linked list node requires the data as well as one or more pointers
to the other elements in the linked list
2. What is an algorithm?
Answer: O(n * n)
Explanation: We can see that the loop is running n * n times, and each time we are adding a
new element in the array. Thus, the space complexity will be equal to O(n * n).
What is the time complexity of the below snippet?
int unknown(int n){
int i, j, k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k = k + n/2;
return (k);
}
Answer:O(n * log(n))
Explanation:The first loop will run at max n/2 times, whereas the nested loop will run for a total
of log(n) times, thus the overall time complexity is O(n)* O(log(n)) ~ O(n * log(n)).
6. What is a postfix expression?
A postfix expression is made up of operators and operands, with the operator coming after the
operands. That is, in a postfix expression, the operator comes after the operands. Likewise, what
is the proper postfix form? The correct postfix phrase is A B + C *.
7. Consider the following postfix expression using stack: 6 5 2 3 + 8 * + 3 + *
8. Consider infix to postfix expression a+b*c+(d*e+f)*g
9. Consider an array with a maximum capacity of 100 elements. In case one, the user adds one
element to an array. In example 2, the user creates an array with 100 elements. What is the
space complexity in both cases?
Answer: O(1),
Explanation: To store one element, we need 100 memory spaces. To store two elements, we
need 100 memory spaces. To store n element, we will still require 100 memory spaces.
Here, the memory is constant and independent from n. Thus the complexity is O(1) in both cases.
10. What are common operations that can be performed on a data-structure?
The following operations are commonly performed on any data-structure −
o Insertion − adding a data item
o Deletion − removing a data item
o Traversal − accessing and/or printing all data items
o Searching − finding a particular data item
o Sorting − arranging data items in a pre-defined sequence
11. What are some examples of divide and conquer algorithms?
The below given problems find their solution using divide and conquer algorithm approach :
o Merge Sort
o Quick Sort
o Binary Search
A stack is an abstract data type that specifies a linear data structure, as in a real physical stack or
piles where you can only take the top item off the stack in order to remove things. Thus, insertion
(push) and deletion (pop) of items take place only at one end called top of the stack, with a
particular order: LIFO (Last In First Out) or FILO (First In Last Out).
16. Where are stacks used?
Expression, evaluation, or conversion of evaluating prefix, postfix, and infix expressions
Syntax parsing
String reversal
Parenthesis checking
Backtracking
Expression evaluation
Backtracking
Memory management
Function calling and return
17. What is a queue Data Structure?
In this type of data structure interview questions, you can also discuss your experience and
situations using queue. A queue is an abstract data type that specifies a linear data structure or an
ordered list, using the First In First Out (FIFO) operation to access elements. Insert operations
can be performed only at one end called REAR and delete operations can be performed only at
the other end called FRONT.
QuickSort algorithm is generally considered the fastest because it has the best performance for
most inputs.
The General Tree: A tree is referred to as a generic tree if its hierarchy is not constrained. In
the General Tree, each node can have an endless number of offspring, and all other trees are
subsets of the tree.
The Binary Tree: The binary tree is a type of tree in which each parent has at least two
offspring. The children are referred to as the left and right youngsters. This tree is more
popular than most others. When specific limitations and features are given to a Binary tree,
various trees such as AVL tree, BST (Binary Search Tree), RBT tree, and so on are also
utilized.
Tree of Binary Search: Binary Search Tree (BST) is a binary tree extension that includes
numerous optional constraints. In BST, a node's left child value should be less than or equal
to the parent value, while the correct child value should always be higher than or equal to the
parent's value.
A binary tree is a tree data structure made up of nodes, each of which has two offspring, known
as the left and right nodes. The tree begins with a single node called the root.