viva question cse 205
viva question cse 205
Q1 What is the significance of Omega (Ω), Theta (Θ), and Big O (O) notations in complexity analysis?
The significance of Ω (Omega), Θ (Theta), and O (Big O) notations lies in analyzing an algorithm's
efficiency. Big O represents the worst-case time/space complexity (upper bound), Theta (Θ) gives the
exact bound (average-case), and Omega (Ω) indicates the best-case (lower bound). These notations help
compare algorithms, predict scalability, and optimize performance.
Answer:
o Best case: O(1) (if the element is the first in the array).
o Worst case: O(n) (if the element is not present or is the last).
Answer:
Answer: A stable sorting algorithm preserves the relative order of equal elements (e.g., Bubble
Sort, Insertion Sort). An unstable algorithm does not guarantee this (e.g., Selection Sort).
Answer: Selection Sort repeatedly selects the smallest element from the unsorted part of the
array and swaps it with the first element of the unsorted part.
Answer:
Insertion Sort is a sorting algorithm that builds the sorted array one element at a time. It picks each
element and inserts it into its correct position by shifting larger elements to the right.
This process is repeated for all elements. Its time complexity is:
Best case: O(n)O(n)O(n) (already sorted array).
Answer: Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through
the array, compares adjacent elements, and swaps them if they are in the wrong order. This
process is repeated until the array is sorted. Passes are n-1.
Answer:
Answer: Linear Search scans each element of the array sequentially until the target element is
found or the end of the array is reached.
Answer: Binary Search is an efficient algorithm for searching in a sorted array. It repeatedly
divides the search interval in half and compares the target element with the middle element. If
they are not equal, it eliminates the half in which the target cannot lie.
Unit 2
o Answer: A linked list is a dynamic data structure where elements (nodes) are connected
via pointers. Unlike arrays, linked lists do not require contiguous memory, and their size
can grow or shrink dynamically.
o Answer: The time complexity is O(n), as each node must be visited sequentially.
o Answer: The head pointer stores the address of the first node in the list, which is
essential for accessing and traversing the linked list.
o Answer: Start from the head node and follow the links to visit each node until reaching
NULL.
8. What is the time complexity for inserting a node at the beginning of a linked list?
o Answer:
10. What happens when you attempt to access an element beyond the last node of a linked list?
o Answer: This results in an error or undefined behavior, as the pointer for the last node is
NULL.
Header Linked Lists
o Answer: A header linked list contains a special node (header) at the beginning, which
may store metadata (e.g., length) or simply act as a dummy node.
12. What is the difference between grounded and circular header linked lists?
o Answer:
Circular: The last node points back to the header node or the first node.
13. What are the advantages of using a header node in a linked list?
o Answer: Simplifies operations like insertion and deletion at the beginning by eliminating
the need for special cases.
o Answer: Start from the node after the header and continue until returning to the header
node.
15. What is the main difference between a header node and a regular node?
o Answer: A header node often stores auxiliary information or acts as a placeholder, while
regular nodes store actual data.
o Answer: In a circular linked list, the last node points back to the first node, forming a
circular structure.
17. What is the advantage of a circular linked list over a singly linked list?
o Answer: Easier to implement applications like buffers and loops where data needs to be
accessed repeatedly in a circular manner.
o Answer: Traverse the list and check if the last node's pointer references the head or any
previously visited node.
19. What is the time complexity for inserting a node at the end of a circular linked list?
o Answer: O(1) if the tail pointer is maintained; otherwise, O(n) for traversal.
20. Can a circular linked list have a NULL pointer? Why or why not?
o Answer: No, because the last node must point to another node to maintain the circular
structure.
o Answer: A doubly linked list is a linked list where each node has two pointers: one to the
next node and another to the previous node.
22. What are the advantages of doubly linked lists over singly linked lists?
o Answer:
o Answer: Each node requires extra memory for storing the pointer to the previous node.
o Answer:
Update the previous node's next pointer and the next node's prev pointer to
skip the node to be deleted.
25. What is the time complexity for inserting a node after a given node in a doubly linked list?
Unit 3
Answer: A stack is a linear data structure that follows the Last In First Out (LIFO)
principle. It can be represented using an array or a linked list. In the array representation,
elements are stored in contiguous memory locations, while in the linked list
representation, each node points to the next.
What is the time complexity of the push and pop operations in a stack using an array?
Answer: Both the push and pop operations have a time complexity of O(1) in an array-
based stack since they involve adding or removing elements from the top without needing
to traverse the entire stack.
What is the time complexity of the push and pop operations in a stack using a linked
list?
Answer: Both the push and pop operations have a time complexity of O(1) in a linked
list-based stack, as we can insert or remove elements at the head of the list without
traversing it.
What is the time complexity for evaluating an expression in Polish Notation using a
stack?
Answer: The time complexity for evaluating a Polish Notation expression is O(n), where
n is the number of operands and operators, as each element is processed once.
Answer: A queue is a linear data structure that follows the First In First Out (FIFO)
principle. It can be represented using an array or a linked list. In the array representation,
the front and rear pointers are used to manage the queue, while in the linked list
representation, nodes are linked sequentially.
What is the time complexity of enqueue and dequeue operations in a queue using an
array?
Answer:
o Enqueue: O(1) if there is space at the rear.
o Dequeue: O(1)
What is the time complexity of enqueue and dequeue operations in a queue using a
linked list?
Answer:
o Enqueue: O(1), as new elements can be added at the rear without traversing the
list.
o Dequeue: O(1), as elements can be removed from the front without traversing the
list.
What is a priority queue, and how does it differ from a regular queue?
Answer: A priority queue is a type of queue where each element is associated with a
priority. Elements with higher priority are dequeued before those with lower priority,
regardless of the order in which they were enqueued.
Answer:
o Insertion: O(log n), as elements need to be inserted in a way that maintains the
priority order.
o Deletion (dequeue): O(log n), as the highest-priority element must be removed,
and the heap structure must be rebalanced.
What is the time complexity of enqueue and dequeue operations in a deque (double-
ended queue)?
Answer:
o Enqueue (at both ends): O(1)
o Dequeue (from both ends): O(1)
This is because both ends can be accessed and modified directly in a deque.
Tree Questions
Recursion Questions
o Answer: An AVL tree is a self-balancing binary search tree where the difference in
heights between the left and right subtrees of any node (called the balance factor) is at
most 1.
o Answer: The time complexity is O(log n) because the tree remains balanced, and
rebalancing operations (rotations) are performed in O(1) time.
Left Rotation (LL): Fixes imbalance caused by an insertion in the left subtree of
the left child.
Right Rotation (RR): Fixes imbalance caused by an insertion in the right subtree
of the right child.
o Answer: The time complexity is O(log n) because rebalancing may occur at multiple
levels during the traversal back to the root.
5. Why are AVL trees used instead of plain binary search trees?
o Answer: AVL trees maintain balance, ensuring that operations like search, insertion, and
deletion are always performed in O(log n) time, unlike unbalanced binary search trees
that can degrade to O(n) in the worst case.
Heaps
o Answer: A binary heap is a complete binary tree that satisfies the heap property:
Max-Heap: The value of each node is greater than or equal to its children.
Min-Heap: The value of each node is less than or equal to its children.
o Answer: The time complexity is O(log n) because after removing the root element, the
last element is placed at the root, and it may need to sift down to maintain the heap
property.
o Answer:
HeapSort builds a max-heap from the array, then repeatedly extracts the
maximum element (root) and places it at the end of the array.
The time complexity of HeapSort is O(n log n) for both worst-case and average-
case scenarios.
o Answer:
B-Tree: Internal and leaf nodes both store data, and search may end at an
internal node.
B+ Tree: Only leaf nodes store data, while internal nodes store keys for
navigation. This structure makes sequential access faster in a B+ Tree.
Unit 6
Graphs
o Answer: The time complexity is O(V³), where VVV is the number of vertices.
o Answer: BFS explores all vertices at the current depth level before moving to the next
level, using a queue to keep track of vertices to visit.
o Answer: The time complexity is O(V + E), where VVV is the number of vertices and EEE is
the number of edges.
o Answer: BFS explores level by level using a queue, while DFS explores depth-first using a
stack or recursion.
o Answer: Floyd-Warshall is an algorithm to find the shortest paths between all pairs of
vertices in a weighted graph.
o Answer: The time complexity is O(V³), where VVV is the number of vertices.
10. What is the key difference between Warshall's algorithm and Floyd-Warshall algorithm?
Hashing
o Answer: Hashing is a technique to map data to a fixed-size hash table using a hash
function, enabling efficient data retrieval.
o Answer: A hash function is a function that takes input data and returns an integer (the
hash value), which serves as an index in the hash table.
o Answer: Linear probing resolves collisions by checking subsequent slots one by one until
an empty slot is found.
o Answer: Double hashing uses two hash functions: one for the initial index and another
for the step size, to resolve collisions.
19. What are the advantages of double hashing over linear and quadratic probing?
o Answer: Double hashing reduces clustering and ensures better distribution of keys in the
hash table.
o Answer: The average-case complexity is O(1), but in the worst case (e.g., when collisions
are excessive), it is O(n).