Data Struc
Data Struc
1. Linear Collection:
a. A linear collection is a data structure that organizes elements sequentially,
where each element has a unique predecessor and a unique successor,
except for the first and last elements.
b. It represents a linear sequence of elements (e.g., arrays, linked lists).
2. Non-Linear Collection:
a. A non-linear collection does not organize elements sequentially.
b. Elements can have multiple predecessors or successors, forming complex
relationships (e.g., trees, graphs).
• Purpose: Sort an array by building the sorted list one element at a time.
• Steps:
o Start with an unsorted array.
o Iterate through the array from the second element to the last.
o For each element, compare it with the sorted region (from right to left).
o Shift elements to the right until the correct position is found for the current
element.
o Insert the current element in its correct position in the sorted region.
• Time Complexity: O(n²) in the worst case, making it slower than more efficient
algorithms like quicksort.
• Constant Time (O(1)): An operation that takes a fixed amount of time regardless of
the size of the input (e.g., accessing an element in an array).
• Logarithmic Time (O(log n)): An operation where the time grows logarithmically
with the input size (e.g., binary search).
• Linear Time (O(n)): An operation where the time grows linearly with the size of the
input (e.g., iterating through an array).
• Log-linear Time (O(n log n)): An operation that grows faster than linear but slower
than quadratic, like quicksort or mergesort.
• Quadratic Time (O(n²)): An operation where the time grows quadratically with the
input size (e.g., bubble sort, insertion sort).
Stack Operations:
Postfix Evaluation:
• Steps:
o Create an empty stack for operands.
o Scan the expression from left to right:
▪ If the element is a number, push it onto the stack.
▪ If the element is an operator, pop the required operands, perform the
operation, and push the result back onto the stack.
o After processing all elements, the result will be in the stack.
• Time Complexity: O(n), where n is the number of elements in the expression.
Infix to Postfix Conversion:
• Steps:
o Scan the infix expression from left to right.
o If the element is an operand, append it directly to the postfix expression.
o If the element is an operator:
▪ Pop and append operators from the stack that have higher or equal
precedence.
▪ Push the current operator onto the stack.
o Handle parentheses:
▪ Push left parentheses onto the stack.
▪ Pop operators from the stack when encountering right parentheses
until the left parenthesis is encountered.
o After scanning the infix expression, pop all remaining operators from the
stack and append them to the postfix expression.
• Time Complexity: O(n), where n is the number of elements in the infix expression.
Composition vs Inheritance:
1. Benefits of Composition:
a. Code Reusability: By composing objects, you can reuse existing
functionality without inheriting unnecessary interfaces.
b. Modularity and Encapsulation: Composition allows functionality to be
divided into smaller, self-contained objects, leading to better separation of
concerns.
c. Flexibility and Extensibility: You can easily add or remove composed
objects to modify behavior without changing the containing class.
d. Simplified Interface: The containing class can expose a simplified interface,
hiding unnecessary details of the composed objects.
e. Reduced Coupling: Composition reduces dependency between classes
compared to inheritance, making the system more maintainable.
5. What is the time complexity of Bubble Sort in the best case (when the input
array is already sorted)? a) O(n)
b) O(log n)
c) O(n²)
d) O(1)
b) dequeue
7. In which scenario would you use a Non-Linear Collection like a tree or graph? a)
When you need to represent hierarchical data.
8. Which algorithm has the time complexity O(n log n) in the average case? a)
Merge Sort
b) Bubble Sort
c) Quick Sort
d) Insertion Sort
9. What is the main advantage of using a stack for reversing a string? a) The
process is faster than using recursion.
10. In a linked list, which of the following is the most efficient operation? a)
Searching for an element in the middle.
11. What is the time complexity of performing a binary search on a sorted list of
size 'n'? a) O(n)
b) O(log n)
c) O(n²)
d) O(n log n)
13. What is the primary difference between a Queue and a Stack? a) A stack is a Last
In, First Out (LIFO) structure, while a queue is a First In, First Out (FIFO) structure.
b) A queue uses an array to store elements, while a stack uses a linked list.
d) A stack allows insertion at both ends, whereas a queue only allows insertion at one end.
1. Write a C++ function to implement Insertion Sort. The function should take a
vector of integers and return the sorted vector.
#include <vector>
using namespace std;
2. Write a C++ function that checks whether a given string contains balanced
brackets (parentheses, square brackets, and curly braces) using a stack. The
function should return true if balanced, and false otherwise.
#include <stack>
#include <string>
using namespace std;
3. Write a C++ function to convert a decimal number to binary using a stack. The
function should return the binary representation as a string.
#include <stack>
#include <string>
using namespace std;
string decimalToBinary(int n) {
stack<int> s;
while (n > 0) {
s.push(n % 2);
n /= 2;
}
string result = "";
while (!s.empty()) {
result += to_string(s.top());
s.pop();
}
return result;
}
4. Write a C++ function that reverses a given string using a stack. The function
should return the reversed string.
#include <stack>
#include <string>
using namespace std;
5. Write a C++ function to implement the Binary Search algorithm. The function
should take a sorted array and a target value as input, and return the index of
the target value or -1 if the target is not found.
#include <vector>
using namespace std;
1. Explain the differences between Linear Collections (e.g., Arrays and Linked
Lists) and Non-Linear Collections (e.g., Trees and Graphs). Provide examples of
when to use each type of collection and the advantages they offer.
2. Discuss the importance of time complexity in algorithm analysis. Describe how
different time complexities (O(1), O(n), O(log n), O(n²), O(n log n)) affect the
performance of algorithms. Provide real-world examples for each time
complexity.
3. Explain the process of evaluating a Postfix expression using a stack. Discuss
the advantages of using a stack for postfix evaluation and provide an example
of how the stack is used to evaluate a postfix expression.