0% found this document useful (0 votes)
116 views

Final Worksheet On Data Structure

The document discusses advantages of linked lists over arrays, scenarios for using linked lists vs arrays, definitions of algorithms, asymptotic analysis, time complexity analysis of algorithms, postfix expressions, infix to postfix conversion, space complexity analysis, common data structure operations, examples of divide and conquer algorithms, descriptions of bubble sort, merge sort, and quick sort algorithms, definitions of stacks and queues, and applications of stacks and queues.

Uploaded by

Wiki Ethiopia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Final Worksheet On Data Structure

The document discusses advantages of linked lists over arrays, scenarios for using linked lists vs arrays, definitions of algorithms, asymptotic analysis, time complexity analysis of algorithms, postfix expressions, infix to postfix conversion, space complexity analysis, common data structure operations, examples of divide and conquer algorithms, descriptions of bubble sort, merge sort, and quick sort algorithms, definitions of stacks and queues, and applications of stacks and queues.

Uploaded by

Wiki Ethiopia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Worksheet on Data Structure and Algorithm

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.

Some scenarios where we use linked list over array are:

 When we know the upper limit on the number of elements in advance

 When there are a large number of add or remove operations

 When there are no large number of random accesses to elements

 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 need to index or randomly access elements

 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? 

An algorithm is a step-by-step method of solving a problem or manipulating data. It defines a set


of instructions to be executed in a certain order to get the desired output. 

3. Why we need to do algorithm analysis?


A problem can be solved in more than one way. So, many solution algorithms can be derived for
a given problem. We analyze available algorithms to find and implement the best suitable
algorithm.
4. What is asymptotic analysis of an algorithm?
Asymptotic analysis of an algorithm, refers to defining the mathematical foundation/framing of
its run-time performance. Using asymptotic analysis, we can very well conclude the best case,
average case and worst-case scenario of an algorithm.
5. What is the time complexity of the following questions
int total (int n)
{
int sum=0;
for (int i=1;i<=n;i++)
sum=sum+1;
return sum;
}
T (n) = 1+ (1+n+1+n) +2n+1 = 4n+4 = O(n)
int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum + (i * i * i);
return partial_sum;
}
T (n) = 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)
Suppose we have hardware capable of executing 106 instructions per second. How long would it
take to execute an algorithm whose complexity function was
T (n) = 2n2 on an input size of n =108?
The total number of operations to be performed would be T(108 ): T(108 ) = 2*(108 ) 2 =2*1016
The required number of seconds would be given by T(108 )/106 so:
Running time = 2*1016/106 = 2*1010
f(n) = 10n + 5 and g(n) = n. Show that f(n) is O(g(n)).
To show that f(n) is O(g(n)) we must show that constants c and k such that
f(n) <= c.g(n) for all n >= k
Or 10n + 5 <= c.n for all n >= k
Try c = 15. Then we need to show that 10n + 5 <= 15n
Solving for n we get: 5 < 5n or 1 <= n.
So f(n) =10n + 5 <= 15.g(n) for all n >= 1.
(c = 15, k = 1).
f(n) = 3n2 + 4n + 1. Show that f(n) = O(n2 ).
4n <= 4n2 for all n >= 1 and 1 <= n 2 for all n >= 1
3n2 + 4n+1 <= 3n2 + 4n2 + n 2
for all n >= 1 <= 8n2 for all n >= 1
So, we have shown that f(n)<= 8n2 for all n >= 1
Therefore, f (n) is O(n2 ) (c = 8, k = 1)
What is the space complexity of the code snippet given below? 
int arr = [];
for (int i = 0; i < n; ++i) {
   for (int j = 0; j < n; ++j) {
       arr[i * n + j](i + j);
   }
}

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

12. What is bubble sort and how bubble sort works?


Answer : Bubble sort is comparison based algorithm in which each pair of adjacent elements is
compared and elements are swapped if they are not in order. Because the time complexity is
Ο(n2), it is not suitable for large set of data.
13. What is merge sort and how it works?
Answer : Merge sort is sorting algorithm based on divide and conquer programming approach. It
keeps on dividing the list into smaller sub-list until all sub-list has only 1 element. And then it
merges them in a sorted way until all sub-lists are consumed. It has run-time complexity of Ο(n
log n) and it needs Ο(n) auxiliary space.
14. How Quick Sort works?
Answer: Quick sort uses divide and conquer approach. It divides the list in smaller 'partitions'
using 'pivot'. The values which are smaller than the pivot are arranged in the left partition and
greater values are arranged in the right partition. Each partition is recursively sorted using quick
sort.
15. What is a stack?

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. 

18. List some applications of queue Data Structure.


 As waiting lists for a single shared resource in a printer, CPU, call center systems, or
image uploads; where the first one entered is the first to be processed
 In the asynchronous transfer of data; or example pipes, file IO, and sockets
 As buffers in applications like MP3 media players and CD players
 To maintain the playlist in media players (to add or remove the songs)
 Print server- maintains a queue of print jobs
 Disk Driver – maintains a queue of disk input/output requests
 Task scheduler in multiprocessing system – maintains priority queues of processes
 Telephone calls in a busy environment – maintains a queue of telephone calls
19. What operations can be performed on queues?
 enqueue() adds an element to the end of the queue
 dequeue() removes an element from the front of the queue
 init() is used for initializing the queue
 isEmpty tests for whether or not the queue is empty
 The front is used to get the value of the first data item but does not remove it
 The rear is used to get the last item from a queue
20. Which sorting algorithm is considered the fastest? Why?

QuickSort algorithm is generally considered the fastest because it has the best performance for
most inputs.

21. What is the merge sort? How does it work?


Merge sort is a divide-and-conquer algorithm for sorting the data. It works by merging and
sorting adjacent data to create bigger sorted lists, which are then merged recursively to form even
bigger sorted lists until you have one single sorted list.

22. What is a graph?


Answer: A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed as vertices, and
the links that connect the vertices are called edges.
It is a type of non-linear data structure that consists of vertices or nodes connected by edges or
arcs to enable storage or retrieval of data. Edges may be directed or undirected. 

23. How Depth First Traversal Works?


Answer: Depth First Search algorithm (DFS) traverses a graph in a depthward motion and uses a
stack to remember to get the next vertex to start a search when a dead end occurs in any iteration.
24. How Breadth First Traversal Works?
Answer: Breadth First Search algorithm (BFS) traverses a graph in a breadth wards motion and
uses a queue to remember to get the next vertex to start a search when a dead end occurs in any
iteration.
25. What are the applications of graph Data Structure?
 Transport grids where stations are represented as vertices and routes as the edges of the
graph
 Utility graphs of power or water, where vertices are connection points and edge the wires
or pipes connecting them
 Social network graphs to determine the flow of information and hotspots (edges and
vertices)
 Neural networks where vertices represent neurons and edge the synapses between them
26. What is a tree?
Answer: A tree is a minimally connected graph having no loops and circuits.
27. List the types of trees?

 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.

28. What are Binary trees?

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.

 Each node in the tree carries the following information:


 Data
 A pointing device indicates the left kid.
 An arrow pointing to the correct child

29. What is a binary tree?


Answer: A binary tree has a special condition that each node can have two children at
maximum.
30. What Is Tree Traversal?
Answer: Tree traversal is a process to visit all the nodes of a tree. Because, all nodes are
connected via edges (links) we always start from the root (head) node.
There are three ways which we use to traverse a tree:
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversa
31. What Is The Prefix And Post Fix Notation Of (a + B) * (c + D) ?
Answer :
Prefix Notation − * + a b + c d
Postfix Notation − a b + c d + *

32. What Is Hashing?


Answer: Hashing is a technique to convert a range of key values into a range of indexes of an
array. By using hash tables, we can create an associative data storage where data index can be
found by providing its key values.

You might also like