Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
167 views

Worksheet On Data Structure

Linked lists have advantages over arrays for insertion and deletion operations as they only require updating node pointers, whereas arrays require shifting elements. Linked lists are also dynamic data structures that allocate memory as needed, avoiding wasted space, and are easier to implement certain data structures like stacks and queues. Arrays are more suitable when random access is needed, the number of elements is known beforehand, or memory usage is critical.

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)
167 views

Worksheet On Data Structure

Linked lists have advantages over arrays for insertion and deletion operations as they only require updating node pointers, whereas arrays require shifting elements. Linked lists are also dynamic data structures that allocate memory as needed, avoiding wasted space, and are easier to implement certain data structures like stacks and queues. Arrays are more suitable when random access is needed, the number of elements is known beforehand, or memory usage is critical.

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/ 8

1. What are the advantages of a linked list over an array?

In which scenarios do we use Linked


List and when Array?
 This is another frequently asked data structure interview questions! Advantages of a
linked list over an array are:
 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 access 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 ways. 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 boundation/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.
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)
Consider the following postfix expression using stack: 6 5 2 3 + 8 * + 3 + *
Consider infix to postfix expression a+b*c+(d*e+f)*g
What Are Common Operations That Can Be Performed On A Data-structure?
Answer :
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
What Are Some Examples Of Divide And Conquer Algorithms?
Answer :
The below given problems find their solution using divide and conquer algorithm
approach :
o Merge Sort
o Quick Sort
o Binary Search

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.
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.
 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.
 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.
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.
How Breadth First Traversal Works?
Answer :
Breadth First Search algorithm(BFS) traverses a graph in a breadthwards motion and uses a
queue to remember to get the next vertex to start a search when a dead end occurs in any
iteration.
1. What Is A Tree?
Answer :
A tree is a minimally connected graph having no loops and circuits.
2. Question 42. What Is A Binary Tree?
Answer :
A binary tree has a special condition that each node can have two children at maximum.

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

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 find by
providing its key values.
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 + *

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

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

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

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

21. List some applications of queue Data Structure.

To prioritize jobs as in the following scenarios:

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

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

 Which sorting algorithm is considered the fastest? Why?

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

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.
Define the graph Data Structure?

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. 

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

35. List the types of trees?

Data structure interview questions like this are very common and frequently asked

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

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

Application of Queues

Print server- maintains a queue of print jobs

Disk Driver – maintains a queue of disk input/output requests

iii. Task scheduler in multiprocessing system – maintains priority queues of

processes

iv. Telephone calls in a busy environment – maintains a queue of telephone calls

v. Simulation of waiting line – maintains a queue of persons

You might also like