0% found this document useful (0 votes)
25 views6 pages

DSA Exam Notes

Uploaded by

Shravani Salunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

DSA Exam Notes

Uploaded by

Shravani Salunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is Data Structure? Explain any 4 to 5 data structure operation.

Ans:-
Data structures are essential components that help organize and store data efficiently in computer
memory. They provide a way to manage and manipulate data effectively, enabling faster access,
insertion, and deletion operations.It defines the relationship between the data and the operations that
can be performed on the data
Common data structures include arrays, linked lists, stacks, queues, trees, and graphs, each
serving specific purposes based on the requirements of the problem.
Data structure operations are as follows:

1. Insertion : This operation involves adding a new element to the data structure. The way
insertion is performed depends on the type of data structure. For example, in an array, you
might insert an element at a specific index, shifting elements to accommodate the new one.
In a linked list, you typically create a new node and adjust the pointers to include it in the
list.

2. Deletion : Deletion refers to removing an existing element from the data structure. Similar
to insertion, the method of deletion varies based on the data structure. In an array, you might
shift elements to fill the gap left by the deleted element, while in a linked list, you adjust
pointers to bypass the deleted node.

3. Searching : Searching involves finding a specific element within the data structure. The
efficiency of searching depends on the structure itself and the algorithm used. In an array,
you might perform a linear search by iterating through each element until you find the
desired one. Alternatively, if the array is sorted, you could use a binary search for faster
results. In a tree structure, you might use techniques like depth-first search (DFS) or
breadth-first search (BFS) to locate an element.
4. Traversing:Traversing a Data Structure means to visit the element stored in it. It visits data
in a systematic manner. Traversal allows you to inspect or manipulate each element in the
structure in a specific order. This can be done with any type of Data strcuture.
2. What is an algorithm? Define space and trade-off and time complexity of an algorithm.
Ans:-
Algorithm:
An algorithm is a step-by-step procedure for solving a problem or accomplishing a task. In the
context of data structures and algorithms, it is a set of well-defined instructions for performing a
specific computational task. Algorithms are fundamental to computer science and play very
important role in designing efficient solutions for various problems.

Algorithms typically follow a logical structure:


•Input: The algorithm receives input data.
•Processing: The algorithm performs a series of operations on the input data.
•Output: The algorithm produces the desired output.
Space Complexicity:-
Space complexity refers to the amount of memory space required by an algorithm to solve a
problem as a function of the input size. It measures how much memory the algorithm needs to store
and manipulate data during its execution. Space complexity is typically expressed in terms of Big
O notation, indicating the worst-case scenario.
Time Complexicity:-
Time complexity, refers to the amount of time taken by an algorithm to solve a problem as a
function of the input size. It measures the number of basic operations, such as comparisons and
assignments, performed by the algorithm as the size of the input grows. Like space complexity,
time complexity is also expressed using Big O notation, representing the upper bound of the
algorithm's running time.

Space-time trade-off:-

The space-time trade-off refers to the relationship between space complexity and time complexity
in algorithms. In many cases, reducing space complexity may increase time complexity, and vice
versa. For example, an algorithm that uses more memory to store precomputed results may have a
lower time complexity because it doesn't need to recalculate those results frequently. Conversely,
reducing memory usage may require more computation, leading to higher time complexity.
3. Advantages and disadvantages of linear search and binary search algorithm.
Ans:-
Linear Search:

Advantages:

1. Simplicity: Linear search is straightforward to implement and understand. It involves


iterating through each element until the target element is found.
2. Applicability: Linear search can be used on both sorted and unsorted arrays.
3. Memory Efficiency: Linear search does not require any additional memory beyond what is
needed to store the array itself.
4. Easy to debug: Because linear search is a simple algorithm, it is easy to debug and
troubleshoot any issues that may arise.

Disadvantages:

1. Time Complexity: In the worst-case scenario, linear search has a time complexity of O(n),
where n is the number of elements in the array. This means that for large arrays, linear
search can be relatively slow.
2. Inefficiency with Large Datasets: As the size of the dataset increases, the time taken by
linear search also increases linearly. This makes it inefficient for large datasets.
3. Limited applicability: Linear search is only suitable for datasets that are not too large or
not too complex.
4. No early termination: Linear search does not have a mechanism to terminate early once
the target element is found.
5. Inefficient for sorted data: When the data is already sorted, linear search is not efficient
because it needs to check each element one by one, even if it has already passed the target
element.

Binary Search:

Advantages:
1. Efficiency: Binary search has a time complexity of O(log n) in the worst case, where n is
the number of elements in the array. This makes it significantly faster than linear search,
especially for large datasets.
2. Suitable for Sorted Arrays: Binary search requires the array to be sorted, but it can
efficiently search sorted arrays.
3. Reduced Number of Comparisons: Binary search divides the search space in half with
each comparison, reducing the number of comparisons needed to find the target element.
4. Simple to implement: Binary search is relatively easy to implement and understand.
5. Versatile: Binary search can be used in a wide variety of applications.
6. Reliable: Binary search is a reliable algorithm that will always find the target element if it
exists in the array.
Disadvantages:

1. Requirement of Sorted Array: Binary search requires the array to be sorted beforehand. If
the array is not sorted, additional sorting steps are needed, which can increase the overall
time complexity.
2. Complexity of Implementation: Binary search is more complex to implement compared to
linear search, especially for those unfamiliar with the algorithm.
3. Memory Overhead: Binary search may require additional memory for recursive function
calls if implemented recursively, which could be a disadvantage in memory-constrained
environments.
4. Not suitable for unsorted data: Binary search is not suitable for searching unsorted data,
as it will not be able to find the target element efficiently.
5. May not be the best choice for large arrays: For very large arrays (e.g., billions of
elements), other search algorithms such as interpolation search or hash tables may be more
efficient.
4. Give the formula to calculate location/ address of any element of linear array in memory.
Ans:-
loc ( Linear_arr[ k ] ) = base ( Linear_arr ) + W( k - lower bound )

Linear_arr = name of array


base = base address
W = words per cell (size of one element)
k = index of element
lower bound = lowest index of array

5. Give the formula to calculate address of any element of a two dimensional array in memory.
Ans:-

-> Row major order


loc ( Arr[ i, j ] ) = base( Arr ) + W[ n(i - 1) + (j - 1) ]

->column major order


loc ( Arr[ i, j ] ) = base( Arr ) + W[ m(j - 1) + (i - 1) ]

Arr = name of array


i, j = index of element
base = base address
W = words per cell (size of one element)
m = total no of rows in array
n = total no of columns in array

6. Explain the process of inserting an element into a linked list.


Ans:- (Algorithm from book)

7. Explain the process of deletion of element in linked list.


Ans:-(Algorithm from book)
8. Write a note a Memory allocation and Garbage collection.
Ans:-
Memory Allocation:-
Memory allocation is the process of assigning portions of a computer's memory to programs
or data structures during their execution. It involves reserving memory space for variables, objects,
or other data entities to be used by a program. Memory allocation can be static, where memory is
allocated at compile-time and remains fixed throughout program execution, or dynamic, where
memory is allocated at runtime and can change as the program runs.
Static Memory Allocation:Static Memory is allocated for declared variables by the compiler. The
address can be found using the address of operator and can be assigned to a pointer. The memory is
allocated during compile time.
Dynamic Memory Allocation: Memory allocation done at the time of execution(run time) is known
as dynamic memory allocation. Functions calloc() and malloc() support allocating dynamic
memory. In the Dynamic allocation of memory space is allocated by using these functions when the
value is returned by functions and assigned to pointer variables.

Garbage collection:-
Garbage collection is a process in which a programming language's runtime system automatically
deallocates memory that is no longer in use by a program. It helps prevent memory leaks and
ensures efficient memory usage by reclaiming memory occupied by objects that are no longer
needed. Garbage collection techniques vary across programming languages and implementations,
but they generally involve identifying and reclaiming memory occupied by objects that are no
longer reachable or referenced by the program.

You might also like