Q1. What is an Algorithm? Explain the Characteristics of Algorithms.
Definition of Algorithm:
An algorithm is a step-by-step procedure or a set of rules to be followed in calculations or problem-solving operations,
especially by a computer. It is a well-defined, finite sequence of instructions that provides a solution to a problem.
Characteristics of Algorithms:
1. Finiteness: An algorithm must always terminate after a finite number of steps. It should not go into an infinite loop.
2. Definiteness: Each step of the algorithm must be clear and unambiguous. The operations to be performed must be
precisely defined.
3. Input: An algorithm should have zero or more inputs, which are the data given to the algorithm for processing.
4. Output: An algorithm should have one or more outputs, which are the results of the processing of the inputs.
5. Effectiveness: The steps in the algorithm should be basic enough to be carried out, in principle, by a human using a
pen and paper. They should be realizable.
6. Generality: An algorithm should solve a general class of problems, not just a specific problem instance. It should be
applicable to different sets of input values.
Q2. What is a Stack? What Operations Can Be Performed on Stacks?
Definition of Stack:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. In a stack, the last element added to
the stack is the first one to be removed. It's analogous to a stack of plates where you can only take the top plate off the
stack.
Operations on Stacks:
1. Push: Adds an element to the top of the stack.
2. Pop: Removes the element from the top of the stack.
3. Peek/Top: Returns the top element of the stack without removing it.
4. isEmpty: Checks if the stack is empty.
5. isFull: Checks if the stack is full (in case of a bounded stack with limited capacity).
Q3. Explain Different Types of Linked List Data Structures.
1. Singly Linked List:
- Each node contains data and a reference (or pointer) to the next node in the sequence.
- Traversal is possible only in one direction, from the head (starting node) to the end.
2. Doubly Linked List:
- Each node contains data, a reference to the next node, and a reference to the previous node.
- Allows traversal in both directions (forward and backward).
3. Circular Linked List:
- Similar to a singly linked list, but the last node points back to the first node, forming a circle.
- Can be either singly or doubly circular linked, enabling traversal to loop back to the starting point.
4. Circular Doubly Linked List:
- Combines the properties of both circular and doubly linked lists.
- Each node has references to both the next and previous nodes, and the last node points to the first node, forming a
circle.
Q4. What Are Graph Traversing Techniques?
Graph traversal refers to the process of visiting all the nodes or vertices in a graph. The primary graph traversal
techniques are:
1. Depth-First Search (DFS):
- Explores as far as possible along a branch before backtracking.
- Uses a stack data structure (either explicitly or via recursion).
- Suitable for solving problems related to connectivity, pathfinding, and cycle detection.
2. Breadth-First Search (BFS):
- Explores all the nodes at the present depth level before moving on to the nodes at the next depth level.
- Uses a queue data structure.
- Suitable for finding the shortest path in unweighted graphs and for level-order traversal.
Q5. Difference Between Array and Linked List.
| Feature | Array | Linked List |
| Structure | Contiguous memory allocation | Non-contiguous memory allocation |
| Size | Fixed size (static array) or dynamic size (dynamic array, but resizing is costly) | Dynamic size, can grow or
shrink during runtime |
| Access Time | O(1) – Direct access using index | O(n) – Sequential access, requires traversal |
| Memory Utilization | May have unused memory (if array size is overestimated) | Memory is used as needed, efficient
for varying sizes |
| Insertion/Deletion | Costly, O(n) for insertion/deletion (requires shifting elements) | Efficient, O(1) if
insertion/deletion is at the beginning; O(n) for general case |
| Search | O(n) for unsorted arrays; O(log n) for sorted arrays (using binary search) | O(n) – Requires traversal, no
direct access |
| Overhead | No overhead, except for unused space | Extra memory is needed for storing pointers |
| Use Cases | Best for scenarios where quick access is needed and the size is known in advance | Best for scenarios
where the size of the data structure changes frequently and quick insertions/deletions are needed |
These differences highlight when to use arrays or linked lists based on the problem requirements and constraints.