DSA Questions
DSA Questions
2. What is an Algorithm?
An algorithm is a step-by-step procedure or formula for solving a problem
Each node contains data and a single reference to the next node.
Each node contains data, a reference to the next node, and a reference to the previous node.
2. Memory Usage
Requires less memory as each node only stores one reference (to the next node).
Requires more memory because each node stores two references (one to the next node and one to the
previous node).
3. Traversal
Can only be traversed in one direction (from the head to the tail).
Can be traversed in both directions (from the head to the tail and from the tail to the head).
Insertion: Adding a new node requires updating the next reference of the previous node to point to
the new node.
Deletion: Removing a node requires updating the next reference of the previous node to skip the
deleted node. No direct reference to the previous node, so often requires traversal from the head to
find the previous node.
Insertion: Adding a new node requires updating both the next reference of the previous node and the
previous reference of the next node.
Deletion: Removing a node is easier because each node has a direct reference to both its previous
and next nodes, making it straightforward to update references.
5. Complexity
More complex to implement and manage due to additional references per node.
6. Use Cases
Useful when memory usage is a concern and bidirectional traversal is not required.
Suitable for simple applications like implementing stacks or queues.
Useful when bidirectional traversal is needed or when frequent insertion and deletion of nodes from
both ends are required.
Suitable for more complex applications like implementing deques (double-ended queues), LRU
(Least Recently Used) cache, etc
10. What do you understand by Stacks?
A stack is a linear data structure that follows the Last In, First Out (LIFO)
principle, meaning the last element added to the stack is the first one
to be removed. Stacks are commonly used in various applications, such
as expression evaluation, backtracking, and memory management.
A queue is a linear data structure that follows the First In, First Out
(FIFO) principle.
Space Complexity
Definition: Space complexity measures the amount of memory an algorithm uses
as a function of the size of the input. It provides an upper bound on the
space needed, including both the space required for input storage and
additional space used by the algorithm.