DS Questions
DS Questions
Short Questions:
h
Q2) List out applications of arrays.
ab
A) Some key applications of arrays in a concise format:
ur
- Data Storage: Store collections of elements of the same type.
Sa
- Lists and Collections: Implement lists, stacks, queues, and linked lists.
- Graphs and Trees: Represent adjacency matrices or lists for graphs and
it
trees.
oh
- Hash Tables: Used for fast retrieval and storage in key-value pairs.
modeling.
ad
h
Q4) What will happen in a C++ program when you assign a value to an
ab
array element whose subscripts exceed the size of the array?
A) In C++, accessing an array element with an out-of-bounds index results in
undefined behaviour. This means anything can happen:
ur
● Crash: Most likely scenario, program crashes due to memory corruption.
● Unexpected behaviour: Program might appear to work for a while but
Sa
produce incorrect results.
● No effect (rare): In some rare cases, no immediate issue might occur.
d
Q5) What is the index number of the last element of an array of 20
an
elements?
A)19
it
A) 5
h
Q 10) What is the row major order?
ab
A) Row-major order is a way of
arranging multidimensional arrays in
memory where elements of each row
ur
are stored contiguously, meaning
elements of the same row are adjacent in memory.
Sa
Q 11) What is column major order?
A) Column-major order is a way of
arranging multidimensional arrays in d
an
memory where elements of each
column are stored contiguously,
meaning elements of the same column are adjacent in memory.
it
oh
Where:
● Address of element: The memory address where the desired element is
by
located.
● B = Base address: The memory address of the first element of the array.
● S = Storage size of one element store in any array(in byte).
e
three-dimensional array.
A)
● n1 as the size of the first dimension (number of elements along dimension
1).
● n2 as the size of the second dimension (number of elements along
dimension 2).
● n3 as the size of the third dimension (number of elements along dimension
3).
● s is the size of each element in bytes.
● i as the index along the first dimension (0-based index).
● j as the index along the second dimension (0-based index).
● k as the index along the third dimension (0-based index).
The formula to calculate the address of the element at indices (i,j,k) in a
row-major order 3-dimensional array is given by:
h
Address = BaseAddress + ((i∗n2∗n3)+(j∗n3)+k)∗s
ab
Where:
● BaseAddress is the starting address of the array in memory.
● (i∗n2∗n3)+(j∗n3)+k calculates the offset of the element from the base
ur
address based on its indices.
● s is the size of each element in bytes, which helps in calculating the
Sa
correct byte offset
decided by a list we have. So, each row or column in the matrix follows the
sequence given in that list. It's like organizing things neatly according to a set
by
plan.
Q 16) If the starting address of array a[-2,23] is 100 then what will be the
e
each element is 4)
Q 17) If the starting address of array a[1:5,1:6] is 100, then, what will be
the address of a[3,4] element?
A) 160
Q 18) If the starting address of array a[1:5,1:6,1:4] is 100 then what will
be the address of a[3,4,5] element?
A) 286[Address = 100 + ((3∗6∗4)+(4∗4)+5)∗2]
[Address = BaseAddress + ((i∗n2∗n3)+(j∗n3)+k)∗s]
Q19) Write any one difference between row major and column major.
A) In row-major order, the consecutive elements of the rows of the array are
stored next to each other in memory.
h
Column-major order stores the consecutive elements of the columns of the
ab
array next to each other in memory.
ur
A) Here are the disadvantages of arrays:
Sa
➢ Fixed Size: Arrays have a fixed size, making it difficult to resize
dynamically.
➢ Contiguous Memory: Requires contiguous memory allocation, limiting
d
flexibility and leading to memory fragmentation.
an
➢ Inefficient Insertions/Deletions: Inserting or deleting elements in the middle
requires shifting elements, which can be inefficient for large arrays.
➢ Homogeneous Data: Arrays store elements of the same data type, limiting
it
h
ab
ur
Sa
d
an
it
oh
manner.
4. Search: Finding a specific element or node within the data structure.
ad
h
➔ When a DS offers such operations, it is termed as ADT.
ab
➔ The word ‘abstract’ refers to the fact that the data and the basic operations
defined on it are being studied independently of how they are implemented.
It involves what can be done with data, not how it has to be done.
ur
Q4) Explain the properties of algorithms.
Sa
A) The properties of algorithms define their characteristics and behaviours.
Here are the fundamental properties of algorithms:
d
1. Input: Every algorithm must have zero or more inputs. These inputs are the
an
data upon which the algorithm operates to produce the desired output.
2. Output: For each valid input, an algorithm must produce one or more
it
h
unexpected conditions. It should handle exceptional cases gracefully, such as
ab
by providing meaningful error messages or recovering from errors without
crashing.
ur
10. Independence: An algorithm should be independent of the programming
language, hardware platform, or specific environment in which it is
Sa
implemented. It should be portable and adaptable to different computing
environments.
d
These properties ensure that algorithms provide a reliable and systematic
an
approach to problem-solving, enabling the development of efficient and
scalable software solutions across various domains.
it
A) To develop an algorithm:
6. Optimize if Necessary:
- Analyze time and space complexity.
- Optimize code logic and data structures.
h
7. Document and Maintain:
ab
- Document purpose, inputs, outputs, and usage.
- Include comments in code and update as needed.
ur
These steps ensure correctness, efficiency, and maintainability of the
algorithm.
Sa
Q6) Differentiate Between linear and non-linear data structure.
A) Linear and non-linear data structures are two fundamental types of data
d
structures used in computer science. They differ in terms of how the elements
an
or nodes are organized and connected within the structure. Here's a
differentiation between linear and non-linear data structures:
it
1. Organization:
oh
- Linear Data Structure: In linear data structures, the elements are organized
sequentially or linearly. Each element has exactly one predecessor and one
successor, except for the first and last elements.
M
2. Connectivity:
e
3. Examples:
- Linear Data Structure: Examples of linear data structures include arrays,
linked lists, stacks, and queues, where elements are arranged in a linear
sequence.
- Non-linear Data Structure: Examples of non-linear data structures include
trees, graphs, and heaps, where elements can have multiple connections,
leading to more complex relationships.
4. Traversal:
h
- Linear Data Structure: Traversal of linear data structures is typically
ab
straightforward, involving sequential access to each element in the structure.
- Non-linear Data Structure: Traversal of non-linear data structures may
require specialized algorithms, as the relationships among elements can be
ur
more complex. Traversal methods like depth-first search (DFS) and
breadth-first search (BFS) are commonly used for non-linear structures like
Sa
trees and graphs.
5. Memory Allocation:
d
- Linear Data Structure: Linear data structures often have contiguous
an
memory allocation, making them efficient for access and traversal.
- Non-linear Data Structure: Non-linear data structures may require dynamic
memory allocation and pointers to represent relationships among elements,
it
6. Usage:
- Linear Data Structure: Linear data structures are suitable for representing
M
- Non-linear Data Structure: Non-linear data structures are used for modeling
more complex relationships, such as hierarchical structures (trees), network
connections (graphs), or priority-based data (heaps).
e
ad
h
of the algorithm scales with input size, while space complexity measures its
ab
memory usage. Efficient algorithms have low time and space complexities,
performing well with larger inputs and minimal memory requirements.
ur
Q) Write a short note on asymptotic notations.
A) Asymptotic notations such as Big O (O), Omega (Ω), and Theta (Θ) are
Sa
used in computer science to describe the time or space complexity of
algorithms as input sizes grow.
- Big O (O) represents the upper bound or worst-case scenario.
d
- Omega (Ω) represents the lower bound or best-case scenario.
an
- Theta (Θ) represents both upper and lower bounds, providing a tight bound
on complexity.
it
These notations help in analyzing and comparing the efficiency and scalability
oh
algorithm.
A)
by
h
data types allow for different actions on the data, like adding numbers or
ab
joining words. They also set rules, like how big the number can be or how long
the word can get. Data types help programs understand and handle
information correctly. They include things like numbers, words, yes/no
ur
answers, lists, and more. Knowing how to use data types helps make sure
programs work right and don't mess up the data.
Sa
Q) Find the complexity of following code.
for (int i = 0; i < n; i++) {
for(int j=1;j<n;j++) d
an
cout<<j
}
A) O(n^2)
it
oh
A)
M
h
Q1) Define data structure.
A) A data structure is a way of organizing and storing data in a computer so that it
ab
can be accessed and used efficiently. It refers to the logical or mathematical
representation of data, as well as the implementation in a computer program.
ur
Examples include arrays, linked lists, and trees. Each type has its own strengths and
weaknesses, making them suitable for different tasks.
Sa
Q2) Define algorithm.
A) Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
d
created independent of underlying languages, i.e. an algorithm can be implemented
an
in more than one programming language.
Game Development: For storing game states, player data, and in-game assets.
M
h
Q7) Define Abstract Data Type.
ab
A)
➔ Operations in DS involve tasks like adding or deleting items, accessing
ur
elements based on priority, or sorting/searching within the structure.
➔ When a DS offers such operations, it is termed as ADT.
Sa
➔ The word ‘abstract’ refers to the fact that the data and the basic operations
defined on it are being studied independently of how they are implemented.
It involves what can be done with data, not how it has to be done.
d
Q8) List out the classification of data structure
an
A)
it
oh
M
by
e
ad
M
h
level instructions.
● They focus on formation of a set of data elements that is either homogeneous
ab
(same data type) or heterogeneous (different data type).
● These are further divided into linear and non-linear data structures based on the
ur
structure and arrangement of data.
Sa
A) Arrays, linked lists, stacks, queues.
A)
h
Q18) What is the use of theoretical testing?
A) Theoretical testing is used to analyze and predict the performance, efficiency, and
ab
correctness of algorithms or systems based on mathematical analysis rather than
practical experimentation.
ur
Q19) What is average, best and worst case complexity?
Sa
A)
BEST CASE
If an algorithm takes the least amount of time to execute a specific set of input,
then it is called the best case time complexity. The best case efficiency of an
d
algorithm is the efficiency for the best case input of size n. Because of this
an
input, the algorithm runs the fastest among all the possible inputs of the same
size.
AVERAGE CASE
it
average, then such a time complexity is called average case time complexity.
Average case analysis provides necessary information about an algorithm’s
behavior on a typical or random input.
M
WORST CASE
If an algorithm takes a maximum amount of time to execute for a specific set of
by
input, then it is called the worst case time complexity. The worst case
efficiency of an algorithm is the efficiency for the worst case input of size n.
The algorithm runs the longest among all the possible inputs of the similar size
e
➔ Big O is the method used to express the upper bound of the running time of an
algorithm.
➔ It is used to describe the performance or time complexity of the algorithm.
➔ Big O specifically describes the worst-case scenario and can be used to describe
the execution time required or the space used by the algorithm.
➔ This notation helps in calculating the maximum amount of time taken by an
algorithm to compute a problem. Big O is defined as: f(n) ≤ c ∗ g(n)
Q21) List out the notation that is used to express the time complexity of the
algorithm.
A) The notations used to express the time complexity of algorithms are:
h
4. Little O Notation (o): Strict upper bound.
5. Little Omega Notation (ω): Strict lower bound.
ab
These notations help in understanding how an algorithm's performance scales with
ur
input size and provide valuable insights into algorithm efficiency and scalability.
Sa
Long questions:-0.
2535
2 d
an
0Q1) Explain the classification of data structure.
A)
it
oh
M
by
e
ad
M
h
- Access/Retrieval: Retrieving elements/data.
- Traversal Algorithms: Algorithms for exploring data structures.
ab
- Comparison: Comparing data structures.
- Memory Management: Efficient memory allocation/deallocation.
ur
- Serialization/Deserialization: Converting data for storage/transmission.
- Hashing: Using hash functions for efficient data retrieval.
- Split/Joint Operations: Operations specific to certain data structures.
Sa
These operations are crucial for manipulating and managing data efficiently within
data structures.
d
Q3) Write a short note on abstract data type.
an
A) Abstract Data Type (ADT) is a model that defines data and operations without
specifying implementation. It encapsulates data and operations, providing a clear
interface. ADTs promote implementation independence, data abstraction, and are
used widely in programming for code organization and reusability. Examples include
it
h
4. Implement the Algorithm:
- Translate algorithms into code.
ab
5. Test and Debug:
ur
- Test with various inputs and fix errors.
6. Optimize if Needed:
Sa
- Analyze and improve efficiency.
A)
oh
Certainly, here are some points to differentiate between linear and non-linear data
structures:
M
2. Each element has a unique predecessor and successor, except for the first and
last elements.
3. Linear data structures are suitable for simple storage and retrieval operations.
e
end to another.
M
h
process it, and produce the desired output. Algorithms are fundamental in computer
science and are used in programming, mathematics, and problem-solving.
ab
Example of an Algorithm:
ur
Let's consider an algorithm to find the maximum number in an array of integers.
Sa
2. Output: The maximum number in the array..
usage.
oh
A) Mentioned Above
ad
Q11) What do you mean by Time and Space complexity and how to represent
these complexity?
M
A) Mentioned Above
h
Q13) Find the complexity of the following code.
ab
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
ur
cout << j << " ";
}
cout << endl;
Sa
}
A)Let's analyze the time complexity of the given code snippet:
d
an
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
cout << j << " ";
}
it
- ...
- When i = n-1, the inner loop runs 1 time.
M
Therefore, the time complexity of the given code is O(n^2) because the nested loops
result in quadratic time complexity in terms of input size 'n'.
h
times each loop is executed:
ab
for send = 1 to n do
for receive = 1 to send do
ur
for ack = 2 to receive do
message = send - (receive + ack)
ack = ack - 1
Sa
end
send = send + 1
end
end d
an
Let's break it down:
1. Outer Loop (`send`):
- It runs 'n' times because it starts from 1 and goes up to 'n'.
it
oh
- For each iteration of the middle loop, it runs from 2 to the current value of
`receive`.
ad
- In the first iteration of the middle loop (when `receive` is 1), it doesn't run because
the condition `ack = 2 to receive` is not met.
M
- In the second iteration of the middle loop (when `receive` is 2), it runs 1 time
(when `ack` is 2).
- In the third iteration of the middle loop (when `receive` is 3), it runs 2 times (when
`ack` is 2 and 3).
- And so on, until it runs 'receive-1' times in each iteration of the middle loop.
Therefore, the total frequency count of the given code is n^2 * (n-1).
Q15.) Find The Total Frequency Count Of Following Code.
i=2n
h
for j=1 to i
for k=3 to j
ab
n=n+1
end
ur
end
A) Let's analyze the total frequency count of the given code:
Sa
i = 2n
for j = 1 to i
for k = 3 to j
n=n+1 d
an
end
end
- For each iteration of the outer loop, the inner loop runs from 3 to the current value
of `j`.
- In the first iteration of the outer loop (when `j` is 1), the inner loop doesn't run
e
h
Total frequency count = Sum from 1 to (2n - 3) + Sum from 1 to (2n - 3) + Sum from 1
to (2n - 3) + ... (2n times)
ab
Total frequency count = 2n * [(2n - 3) * (2n - 2) / 2]
ur
Simplifying this expression gives the total frequency count.
Sa
Therefore, the total frequency count of the given code is 2n * [(2n - 3) * (2n - 2) / 2].
d
an
it
oh
M
by
e
ad
M
LINKED LIST
Short Questions
h
ab
Q) What is the limitation of sequential data structures?
A) Fixed Size: They have a set size, making it hard to handle changing
ur
amounts of data.
Linear Access: Finding data means searching through every piece one by
one, which can be slow for big sets.
Sa
Inefficient Insertions and Deletions: Adding or removing data from the middle
can be slow because it might mean moving a lot of other data around.
Limited Flexibility: They might not be great for handling different types of
d
data or complex relationships between pieces of data.
an
Slow for Dynamic Changes: Making lots of changes to the data, like adding
or removing items frequently, can be slow and inefficient.
No Quick Access: If you want to find something specific, you might need to
it
look through everything from the start, which can take a while.
oh
Limited Scalability: As the data gets bigger or changes more, these structures
might struggle to keep up and become less efficient.
M
➢ A linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list. This allows for dynamic memory
allocation and efficient insertion and deletion operations compared to
e
arrays.
ad
➢ Linked lists are useful when the number of elements to be stored in a list is
indefinite.
h
ab
ur
Sa
Q) What are the advantages of a singly linked list?
A)
Advantages:
d
Dynamic Size: The size of a linked list can be increased or decreased at
an
runtime.
Efficient Insertions/Deletions: Insertion or deletion of a node can be done
more efficiently compared to an array.
it
memory waste.
Versatility: They support various operations like insertion, deletion, traversal,
and concatenation.
M
Flexibility: Singly linked lists can hold elements of different types and sizes.
by
Backward Traversal: Singly linked lists are less flexible as they allow
movement in one direction only.
h
in certain scenarios.
ab
Q) Define circular linked list.
A) In a circular linked list, only one pointer is used to point to another node or
ur
next element. It is known as a circular list because the last node’s pointer
points to the HEAD node.
Sa
Q) What are the advantages of circular linked lists?
A) Circular Navigation: It provides a way to cyclically traverse the list since the last
node points to the first node. d
an
Efficient Operations: Operations like insertion and deletion at the beginning and
end are efficient as they require a single pointer update.
Infinite Looping: They are used in applications where the system needs to go
it
A)
ad
M
Q) Define doubly linked list.
A)
● A doubly linked list (DLL) is a special type of linked list in which each node
contains a pointer to the previous node as well as the next node of the linked list.
● The first pointer points to the next element and the second pointer points to the
h
previous element.
● The previous pointer for the HEAD node points to NULL and the next pointer for
ab
the last node points to NULL.
● Doubly-linked list is also known as a two-way list as both forward and backward
ur
traversal is possible.
Sa
A) Advantages:
● A DLL can be traversed in both forward and backward directions.
● The delete operation in DLL is more efficient if a pointer to the node to be deleted
is given.
d
● We can quickly insert a new node before a given node.
an
● In a singly linked list, to delete a node, a pointer to the previous node is needed.
To get this previous node, sometimes the list is traversed. In DLL, we can get the
it
Q) What is the difference between circular linked list and linear linked list?
A)
h
ab
ur
Sa
d
an
it
oh
M
variables, coefficients, and exponents. They are typically represented using linked
lists or arrays, where each term in the polynomial is stored as a separate node or
by
Q) Give node structure for the term of a polynomial having a single variable.
A) struct TermNode {
M
h
contains data and a reference (or link) to the next node in the sequence. Key points
about linked lists include:
ab
1. Node Structure:
ur
Each node contains data and a pointer/reference to the next node.
Sa
2. Types of Linked Lists:
- Singly Linked List: Nodes have a pointer to the next node.
d
- Doubly Linked List: Nodes have pointers to both the next and previous nodes.
an
- Circular Linked List: Last node points back to the first node, forming a circle.
Linked lists allow dynamic memory allocation, enabling flexible memory usage.
oh
Elements can be inserted/deleted easily at any position in the list, unlike arrays.
5. Traversal:
by
Traversal is typically sequential, starting from the head node and following links to
subsequent nodes.
e
ad
6. Memory Efficiency:
Each node only requires memory for data and pointers, leading to efficient memory
M
usage.
7. Size Flexibility:
Linked lists can grow or shrink dynamically based on operations.
8. Complexity:
While insertion and deletion are efficient at the beginning/end of the list, middle
operations may require traversal, impacting efficiency compared to arrays for random
access.
Overall, linked lists are versatile data structures suitable for scenarios requiring
dynamic memory allocation, flexible size management, and efficient insertion/deletion
operations.
h
ab
Q2. Explain Operation of singly linked list with algorithm.
A) Operations of Singly Linked List
ur
1. Insertion at Beginning:
Sa
- Create a new node, set it next to the current head, and update the head to the
new node.
2. Insertion at End: d
an
- Create new node, traverse to the end, set the last node's next to the new node.
3. Deletion at Beginning:
it
- Store reference to head, update head to next node, free stored node.
oh
4. Deletion at End:
M
- Traverse to second-to-last node, update its next pointer to NULL, free last node.
by
5. Traversal:
- Start at head, process the current node, move to next until the pointer points to
NULL.
e
ad
Q4. What are the advantages of circular linked lists over singly linked lists?
A) The advantages of circular linked lists over singly linked lists include:
- Efficient traversal for continuous looping operations.
- Support for circular operations like round-robin scheduling.
- Constant-time insertions and deletions at the beginning or end.
- Memory efficiency in certain scenarios.
- Avoidance of NULL checks for end of list, simplifying algorithms.
- Natural representation of looping structures or cyclic data patterns.
h
ab
Q5. Write pseudo code add node attending circular linked list.
A)Here is a short pseudo-code example for adding a node to a circular linked list:
ur
Sa
// Define the Node structure
struct Node {
int data;
struct Node* next; d
an
};
if (*head == NULL) {
by
*head = newNode;
newNode->next = newNode; // Make it circular
e
} else {
ad
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head; // Make it circular
}
}
```
This concise pseudo-code demonstrates adding a node to a circular linked list. It first
creates a new node with the given data and then adds it to the end of the list,
ensuring the circular structure is maintained. Adjustments can be made as needed
for specific implementations or error handling.
h
ab
Q6. Explain doubly linked list with advantages and disadvantages of it.
ur
A) Doubly Linked List:
Sa
Advantages:
1. Bidirectional Traversal: Allows traversal in both forward and backward directions,
enabling efficient operations like reverse traversal and deletion in both directions.
d
2. Efficient Insertions and Deletions: Insertions and deletions at the beginning, end,
an
or at known positions can be done in O(1) time complexity.
3. Improved Flexibility: Suitable for implementing undo/redo functionality, managing
sorted lists, and reverse operations efficiently.
it
oh
Disadvantages:
1. Higher Memory Overhead: Requires additional memory for storing the previous
M
4. Space and Time Overhead: Extra pointers increase space overhead, and certain
ad
operations may have slightly higher time complexity compared to singly linked lists.
M
```plaintext
// Node structure for doubly linked list
struct Node {
int data;
h
struct Node* prev;
ab
struct Node* next;
};
ur
// Function to delete a node from a doubly linked list
Sa
void deleteNode(struct Node head, struct Node* delNode) {
// Base case: If list is empty or delNode is NULL, return
if (*head == NULL || delNode == NULL) {
return;
d
an
}
it
if (*head == delNode) {
*head = delNode->next; // Update head to next node
}
M
by
}
ad
if (delNode->next != NULL) {
delNode->next->prev = delNode->prev;
M
h
doubly linked list structure.
ab
- Finally, it frees the memory of the deleted node (`delNode`).
ur
This pseudo code demonstrates the basic logic for deleting a node from a doubly
linked list. Adjustments may be needed based on specific requirements or
Sa
optimizations, such as handling edge cases or maintaining list properties.
d
Q8. Explain operation of doubly linked list with algorithm.
an
A)
A)
oh
A)
by
A)
M
Q 16. How are push and pop operations implemented on a linked stack?
A)
h
Q17. Write Algorithm for insertion/deletion operation on linked queue.
ab
A)
ur
Q18. Write Short note on Dynamic memory management.
A)
Sa
Q19. Explain Application Of Linked Stack and linked queue.
A)
d
an
Q20. Write a pseudo code for implementing stack using linked list.
A)
Q 21. Write a pseudo code for implementing a queue using a linked queue.
it
A
oh
M
by
e
ad
M