Solved Unit 1 Q-Bank
Solved Unit 1 Q-Bank
Q1 - What is data structure? Why to study data structure? Enlist the five areas of
computer science in which data structure is used?
Ans –
A data structure is a way to organize, manage, and store data efficiently for specific
operations and applications. It provides a means to perform various operations on data, such as
searching, inserting, deleting, and sorting, effectively and efficiently. Common examples of data
structures include arrays, linked lists, stacks, queues, trees, graphs, hash tables, and heaps.
1. Efficiency: Proper choice of data structures can significantly improve the performance of
algorithms, making applications faster and more efficient.
2. Problem-Solving: Understanding data structures equips you to design and implement
solutions for complex computational problems.
3. Software Development: Data structures form the foundation of many algorithms and
systems, enabling the development of robust and scalable applications.
4. Optimization: Helps in optimizing both memory usage and processing speed, critical in
resource-constrained systems.
5. Core Computer Science: Data structures are fundamental to understanding computer
science concepts like databases, networks, and operating systems.
Integer (int)
Character (char)
Boolean (_Bool)
Ans –
Enlistment of Primitive and Non-Primitive Data Structures
Primitive Data Structures
• Integer (int)
• Floating-point numbers (float, double)
• Character (char)
• Boolean (_Bool or bool)
2|Page
Non-Primitive Data Structures
• Arrays
• Strings
• Structures (struct)
• Unions (union)
• Pointers
• Linked Lists
• Stacks
• Queues
• Trees
• Graphs
3|Page
Q4 – Define Space Complexity?
Ans -
Space Complexity
Space Complexity refers to the total amount of memory space required by an algorithm
to execute. It includes the memory needed to store the inputs, outputs, and any intermediate
computations during the algorithm's execution. It is typically expressed as a function of the input
size n.
Space complexity plays a critical role in evaluating and optimizing algorithms,
particularly when working with large datasets or embedded systems with limited memory.
Space Complexity
4|Page
Space Complexity specifically measures the total amount of memory space
required by an algorithm to execute successfully. It is crucial for understanding how
efficiently an algorithm uses memory, especially in systems with limited resources.
Components of Space Complexity
1. Fixed Space Requirements:
o Memory for constants, program instructions, and static variables.
o Independent of input size.
2. Variable Space Requirements:
o Memory for the input data structure.
o Memory for output data structure.
o Memory for temporary or auxiliary variables during computation.
The total space complexity is the sum of these fixed and variable requirements.
5|Page
Big-O Notation
Time complexity is commonly expressed using Big-O notation, which represents the upper
bound of the algorithm's growth rate.
Common Time Complexities:
1. O(1): Constant time – execution time is the same regardless of input size.
2. O(log n): Logarithmic time – execution time increases logarithmically as input size
grows.
3. O(n): Linear time – execution time grows proportionally to input size.
4. O(n²): Quadratic time – execution time grows quadratically as input size increases.
5. O(2ⁿ): Exponential time – execution time doubles with every additional input element.
• Regardless of the array size, the function always performs one operation.
• Time Complexity: O(1)O(1)O(1).
• The loop iterates nnn times, performing one addition per element.
• Time Complexity: O(n)O(n)O(n).
6|Page
Q7 - Types of Data Structures
Ans –
7|Page
• Integer (int): Stores whole numbers.
• Floating-point (float, double): Stores decimal numbers.
• Character (char): Stores single characters.
• Boolean (bool): Stores true/false values.
Ans –
An array is a collection of elements, all of the same data type, stored in contiguous memory
locations. It allows efficient access to elements using an index.
Homogeneous: All elements in an array must be of the same data type (e.g., integers, floats,
or characters).
Fixed Size: The size of an array is defined at the time of its declaration and cannot be
changed.
Random Access: Elements can be accessed directly using their index in constant time, O(1).
8|Page
Zero-Based Indexing: In most programming languages, including C, array indexing starts at
0.
• Example:
int numbers[5];
• Initialization:
int numbers[5] = {1, 2, 3, 4, 5};
• Accessing Elements
printf("%d", numbers[2]); // Accesses the 3rd element (value: 3)
Advantages of Arrays
1. Fast Access: Elements can be accessed in constant time using the index.
2. Efficient Memory Use: Contiguous allocation reduces overhead.
3. Easy Traversal: Loops can iterate through the elements efficiently.
Limitations of Arrays
1. Fixed Size: Once declared, the size of an array cannot be changed.
2. Insertion/Deletion: Adding or removing elements can be inefficient as it may require
shifting elements.
3. Homogeneity: Only supports elements of the same data type.
Memory Representation of Arrays
In memory, arrays are represented as a contiguous block of memory. Each element in
the array is stored at consecutive memory addresses.
Representation in Memory
• If the base address of the array is A and the size of each element S:
o Address of array[i]=A+i×S
Example
Consider an array:
int arr[4] = {10, 20, 30, 40};
Assume:
9|Page
Index Element Address
2 30 1008
3 40 1012
Accessing Memory
To access arr[2]:
Address = 1000+2×4=1008
10 | P a g e
•
Explanation: The algorithm will scan all nnn elements before finding the key or
concluding that it is absent.
• Time Complexity: O(n)
3. Average Case (O(n)):
• On average, the algorithm checks half of the elements in the array.
int arr[] = {5, 8, 12, 3}; // key = 8
• Explanation: The algorithm will find the key after checking a few elements,
averaging around half of the total array length.
• Time Complexity: O(n)
Q10 - What are some common operations on data structures, and why are they important?
Ans –
Data structures are essential for organizing and manipulating data efficiently. They allow
for the implementation of various algorithms, such as searching, sorting, and traversal. Below
are some of the most common operations on data structures and their importance:
1. Insertion
• Description: Insertion involves adding an element to a data structure. The position of
insertion can vary depending on the data structure type (e.g., beginning, end, or a specific
position).
• Example: Inserting a new element at the end of a list or array.
void insertAtEnd(int arr[], int *size, int value) {
arr[*size] = value;
(*size)++;
}
• Importance:
o Essential for dynamically growing data structures like lists and queues.
o Allows data to be added without disrupting the structure's integrity (e.g., inserting
in a sorted array).
2. Deletion
• Description: Deletion refers to removing an element from a data structure. Like
insertion, the position of deletion can vary depending on the data structure (e.g., first, last,
or specific position).
• Example: Removing an element from an array or stack.
void deleteElement(int arr[], int *size, int index) {
for (int i = index; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}
• Importance:
11 | P a g e
o Necessary for maintaining the size of dynamic data structures like queues, stacks,
and lists.
o Helps remove outdated, redundant, or unnecessary data from the structure.
3. Access/Search
• Importance:
o Traversal is key for operations such as displaying the contents, applying an
operation to all elements (e.g., sorting, updating), or searching for a specific
condition.
o It is crucial for data structures like trees and graphs, where hierarchical
relationships need to be explored (e.g., in-order traversal for binary trees).
Q11 - What is a stack, and how does it follow the Last-In-First-Out (LIFO) principle?
Ans-
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle,
meaning that the most recently added element is the first one to be removed. It operates
similarly to a stack of plates, where you can only add or remove plates from the top of the
stack.
12 | P a g e
Stack Operations:
1. Push: Adds an item to the stack.
2. Pop: Removes the item from the top of the stack.
3. Peek/Top: Returns the item at the top of the stack without removing it.
4. IsEmpty: Checks if the stack has no elements.
How Does a Stack Follow the LIFO Principle?
The Last-In-First-Out (LIFO) principle means that the last element added to the stack
will be the first one to be removed. In other words, the stack "remembers" the order in which
elements were added, and the most recent element always "comes out" first.
This makes stacks ideal for reversing operations, such as undo features in text editors,
or handling recursive function calls in programming languages.
The stack data structure is widely used in algorithms and applications like:
1. Expression evaluation (e.g., evaluating arithmetic expressions using infix, prefix, or
postfix notation).
2. Function calls and recursion (as the stack stores the state of function calls).
3. Backtracking algorithms (e.g., maze solving, depth-first search in trees or graphs).
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
stack[++top] = value;
printf("Pushed %d\n", value);
}
}
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
} else {
printf("Popped %d\n", stack[top]);
return stack[top--];
}
}
int peek() {
if (top == -1) {
printf("Stack is empty\n");
return -1;
} else {
13 | P a g e
return stack[top];
}
}
int isEmpty() {
return top == -1;
}
int main() {
push(10);
push(20);
push(30);
printf("Top element: %d\n", peek());
pop();
printf("Top element after pop: %d\n", peek());
return 0;
}
Ans –
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle.
This means that the first element added to the queue will be the first one to be removed, much
like a queue at a ticket counter or a line at a checkout. In other words, the first person to stand in
line is the first one to be served.
A queue operates with two main ends:
• Front: The end where elements are removed (dequeued).
• Rear: The end where elements are added (enqueued).
Queue Operations:
1. Enqueue: Adds an element to the rear of the queue.
2. Dequeue: Removes an element from the front of the queue.
3. Front/Peek: Returns the element at the front without removing it.
4. IsEmpty: Checks if the queue is empty.
5. Size: Returns the number of elements in the queue.
14 | P a g e
Example:
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue overflow\n");
} else {
if (front == -1) {
front = 0;
}
queue[++rear] = value;
printf("Enqueued %d\n", value);
}
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow\n");
return -1;
} else {
int dequeuedValue = queue[front++];
printf("Dequeued %d\n", dequeuedValue);
return dequeuedValue;
}
}
int peek() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return -1;
} else {
return queue[front];
}
}
int isEmpty() {
return front == -1 || front > rear;
}
int size() {
if (front == -1 || front > rear) {
return 0;
} else {
return rear - front + 1;
}
}
15 | P a g e
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("Front element: %d\n", peek());
dequeue();
printf("Front element after dequeue: %d\n", peek());
return 0;
}
Q13 - What are the differences between static and dynamic data structures?
Ans –
16 | P a g e
Feature Static Data Structures Dynamic Data Structures
Operations like insertion and Insertion, deletion, and resizing are easier
Operations deletion are harder or less and more efficient since the structure can
efficient because the size is fixed. grow or shrink.
Suitable when the number of Suitable for scenarios where the number
elements is known in advance of elements is not known in advance or
Usage
and does not change (e.g., static changes frequently (e.g., dynamic data
arrays). collection).
17 | P a g e
Feature Queue Stack
Can be implemented using an array or Can be implemented using an array
Implementation
a linked list with pointers. or a linked list.
Memory Access Non-contiguous memory allocation Non-contiguous memory allocation
Pattern (if implemented with a linked list). (if implemented with a linked list).
Size is dynamic in most Size is dynamic, but may have a
Size
implementations but can have fixed limit if implemented with a static
Management
capacity in array-based queues. array.
The order of elements in a queue is The order of elements in a stack is
Order of
maintained in the sequence in which reversed (i.e., last added is first
Elements
they were added. removed).
Q15 - Explain the difference between an abstract data type (ADT) and a data structure.
Ans –
18 | P a g e
Aspect Abstract Data Type (ADT) Data Structure
Implements the actual logic
Defines operations that must be
behind those operations (e.g., how
Operations performed (e.g., insertion, deletion,
to insert or delete elements, how
access).
to organize them in memory).
Directly involves memory
Memory No concern for memory management.
management (e.g., allocation and
Management It's a high-level design.
deallocation of memory).
Example of ADTs - Stack (ADT): Push, pop, peek
- Array-based Stack
and Data - Queue (ADT): Enqueue, dequeue,
- Linked-list-based Queue
Structures peek
• An Abstract Data Type (ADT) is a high-level, abstract definition of data and the
operations that can be performed on it. It is concerned with what operations are available
and their behavior.
• A Data Structure is the actual implementation of the ADT, describing how the data is
stored, organized, and manipulated in memory, with specific details about algorithms and
memory management.
The distinction is important because it allows for flexibility in the implementation of data
structures while ensuring that the abstract behavior (defined by the ADT) remains consistent.
This separation allows programmers to focus on the logical aspects of data handling (ADT)
while still having the ability to choose the most efficient way to implement these concepts (data
structure).
19 | P a g e