Ds Question Paper With Answer
Ds Question Paper With Answer
Ds Question Paper With Answer
in
1)Refer notes
A stack is a linear data structure in which the insertion of a new element and removal
of an existing element takes place at the same end represented as the top of the stack.
Applications of Stacks:
• Function calls: Stacks are used to keep track of the return addresses of function
calls, allowing the program to return to the correct location after a function has
finished executing.
• Recursion: Stacks are used to store the local variables and return addresses of
recursive function calls, allowing the program to keep track of the current state of
the recursion.
• Expression evaluation: Stacks are used to evaluate expressions in postfix notation
(Reverse Polish Notation).
• Syntax parsing: Stacks are used to check the validity of syntax in programming
languages and other formal languages.
• Memory management: Stacks are used to allocate and manage memory in some
operating systems and programming languages.
.
Advantages of Stacks:
• Simplicity: Stacks are a simple and easy-to-understand data structure, making them
suitable for a wide range of applications.
• Efficiency: Push and pop operations on a stack can be performed in constant
time (O(1)), providing efficient access to data.
• Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last
element added to the stack is the first one removed. This behavior is useful in many
scenarios, such as function calls and expression evaluation.
• Limited memory usage: Stacks only need to store the elements that have been
pushed onto them, making them memory-efficient compared to other data structures.
•
Disadvantages of Stacks:
• Limited access: Elements in a stack can only be accessed from the top, making it
difficult to retrieve or modify elements in the middle of the stack.
• Potential for overflow: If more elements are pushed onto a stack than it can hold,
an overflow error will occur, resulting in a loss of data.
• Not suitable for random access: Stacks do not allow for random access to
elements, making them unsuitable for applications where elements need to be
accessed in a specific order.
• Limited capacity: Stacks have a fixed capacity, which can be a limitation if the
number of elements that need to be stored is unknown or highly variable.
4) Refer notes
malloc() Allocates memory and returns a pointer to the first byte of allocated space
calloc() Allocates space for an array of elements, initializes them to zero and returns a pointer to
the memory
free() Frees previously allocated memory
realloc() Alters the size of previously allocated memory
7
8
Difference between Full and Complete Binary Tree
A binary tree is a type of data structure where each node can only have two offspring at most
named as “left” and “right” child.
9
BINARY HEAPS
A binary heap is a complete binary tree in which every node satisfies the heap property
which states that:
If B is a child of A, then key(A) ≥ key(B)
This implies that elements at every node will be either greater than or equal to the element
at its left and right child. Thus, the root node has the highest key value in the heap. Such a
heap is commonly known as a max-heap. Alternatively, elements at every node will be
either less than or equal to the element at its left and right child. Thus, the root has the
lowest key value. Such a heap is called a min-heap
10
Hashing refers to the process of generating a fixed-size output from an input of variable size
using the mathematical formulas known as hash functions. This technique determines an index
or location for the storage of an item in a data structure.
Applications of Hashing
1. Hashing provides constant time search, insert and delete operations on average. This is why
hashing is one of the most used data structure, example problems are, distinct elements,
counting frequencies of items, finding duplicates, etc.
2. Database indexing: Hashing is used to index and retrieve data efficiently in databases and
other data storage systems.
3. Dictionaries : To implement a dictionary so that we can quickly search a word
4. Password storage: Hashing is used to store passwords securely by applying a hash function
to the password and storing the hashed result, rather than the plain text password.
5. Network Routing: Determining the best path for data packets
6. Bloom Filters : Bloom filter is a space optimized and probabilistic version of hashing and
has huge applications like spam filtering, recommendations.
7. Cryptography: Hashing is used in cryptography to generate digital signatures, message
authentication codes (MACs), and key derivation functions.
8. Load balancing: Hashing is used in load-balancing algorithms, such as consistent hashing,
to distribute requests to servers in a network.
9. Blockchain: Hashing is used in blockchain technology, such as the proof-of-work
algorithm, to secure the integrity and consensus of the blockchain.
10. Image processing: Hashing is used in image processing applications, such as perceptual
hashing, to detect and prevent image duplicates and modifications.
11. File comparison: Hashing is used in file comparison algorithms, such as the MD5 and SHA-
1 hash functions, to compare and verify the integrity of files.
12. Fraud detection: Hashing is used in fraud detection and cybersecurity applications, such as
intrusion detection and antivirus software, to detect and prevent malicious activities.
13. Caching: Storing frequently accessed data for faster retrieval. For example browser caches,
we can use URL as keys and find the local storage of the URL.
14. Symbol Tables: Mapping identifiers to their values in programming languages
15. Associative Arrays: Associative arrays are nothing but hash tables only. Commonly SQL
library functions allow you retrieve data as associative arrays so that the retrieved data in
RAM can be quickly searched for a key.
2. Best Fit The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process. This algorithm first searches the entire list of free partitions
and considers the smallest hole that is adequate. It then tries to find a hole which is close to actual
process size needed.
Advantage Memory utilization is much better than first fit as it searches the smallest free partition
first available.
Disadvantage It is slower and may even tend to fill up memory with tiny useless holes.
3. Worst fit In worst fit approach is to locate largest available free portion so that the portion left
will be big enough to be useful. It is the reverse of best fit.
Advantage Reduces the rate of production of small gaps.
Disadvantage If a process requiring larger memory arrives at a later stage then it cannot be
accommodated as the largest hole is already split and occupied.
16 (a)
Adding two polynomials using Linked List
Given two polynomial numbers represented by a linked list. The task is to add these
lists meaning the coefficients with the same variable powers will be added.
Note: Given polynomials are sorted in decreasing order of power.
Example:
Input:
head1: [[5, 2], [4, 1], [2, 0]]
head2: [[5, 1], [5, 0]]
Output: [[5, 2], [9, 1], [7, 0]]
Explanation:
ALGORITHM:
6. Add the co-efficients of poly1 and poly2 having the equal power and store it in poly.
7. If there is no co-efficient having equal power in poly1 and poly2, then add it to poly.
17(a)
A Binary Search Tree (or BST) is a data structure used in computer science for
organizing and storing data in a sorted manner. Each node in a Binary Search Tree has
at most two children, a left child and a right child, with the left child containing values
less than the parent node and the right child containing values greater than the parent
node. This hierarchical structure allows for efficient searching, insertion,
and deletion operations on the data stored in the tree.
Extra
Comparison Between B Trees and B+ Trees
Table 11.1 shows the comparison between B trees and B+ trees.
Table 11.1 Comparison between B trees and to B+ trees
B Tree B+ Tree
1. Search keys are not repeated 1. Stores redundant search key
2. Data is stored in internal or leaf nodes 2. Data is stored only in leaf nodes
3. Searching takes more time as data may
be found in a leaf or non-leaf node
3. Searching data is very easy as the
data can be found in leaf nodes only
4. Deletion of non-leaf nodes is very complicated 4. Deletion is very simple because data
will be in the leaf node
5. Leaf nodes cannot be stored using linked lists 5. Leaf node data are ordered
using sequential linked lists
6. The structure and operations are complicated 6. The structure and operations are
Simple
17(b)
18(a)
Refer record
18(b)
Degree of a node It is equal to the number of children that a node has. The degree of a leaf node is
zero. For example, in the ree, degree of node 4 is 2, degree of node 5 is zero and degree
of node 7 is 1.
Height of a tree It is the total number of nodes on the path from the root node
to the deepest node in the tree. A tree with only a root node has a height of 1.
20 (b)
Stable (does not change the Not stable by default (can be made stable
Stability
relative order of equal elements) with some modifications)
Auxiliary
O(1) (in-place) O(1) (in-place)
Space