0% found this document useful (0 votes)
20 views13 pages

viva question cse 205

The document covers various data structures and their complexities, including algorithms like Linear Search, Binary Search, and sorting methods such as Selection Sort and Bubble Sort. It also explains linked lists, their types, and operations, as well as stacks and queues, including priority queues and deques. Additionally, it discusses binary trees and Binary Search Trees, detailing their properties and time complexities for various operations.

Uploaded by

nishantprep07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

viva question cse 205

The document covers various data structures and their complexities, including algorithms like Linear Search, Binary Search, and sorting methods such as Selection Sort and Bubble Sort. It also explains linked lists, their types, and operations, as well as stacks and queues, including priority queues and deques. Additionally, it discusses binary trees and Binary Search Trees, detailing their properties and time complexities for various operations.

Uploaded by

nishantprep07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Questions

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.

Q2 What is the time complexity of Linear Search?

 Answer:

o Best case: O(1) (if the element is the first in the array).

o Average case: O(n/2) ~ O(n).

o Worst case: O(n) (if the element is not present or is the last).

Q3 What is the time complexity of Binary Search?

 Answer:

o Best case: O(1) (if the middle element is the target).

o Worst and Average case: O(log n).

Q4 What is the difference between stable and unstable sorting algorithms?

 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).

Q5 What is Selection Sort, and how does it work?

 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.

Q6 What is the time complexity of Selection Sort?

 Answer:

o Best case: O(n²).

o Average case: O(n²).

o Worst case: O(n²).

Q7 What is the time complexity of insertion Sort?

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).

 Worst and Average case: O(n2)O(n^2)O(n2) (repeated shifting in reverse-sorted or random


arrays).

Q 8What is Bubble Sort, and how does it work?

 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.

Q7 What is the time complexity of Bubble Sort?

 Answer:

o Best case: O(n) (when the array is already sorted).

o Average case: O(n²).

o Worst case: O(n²) (when the array is sorted in reverse order).

Q8 What is Linear Search?

 Answer: Linear Search scans each element of the array sequentially until the target element is
found or the end of the array is reached.

Q9 What is Binary Search, and how does it work?

 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

1. What is a linked list, and how does it differ from an array?

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.

2. What are the advantages of using linked lists over arrays?


o Answer:

 Dynamic memory allocation.

 Efficient insertion and deletion operations.

 No need to define size in advance.

3. Explain the structure of a node in a linked list.

o Answer: A node contains two parts:

 Data: Stores the element value.

 Pointer/Link: Points to the next node in the list.

4. What is the time complexity for traversal in a linked list?

o Answer: The time complexity is O(n), as each node must be visited sequentially.

5. What is the significance of the head pointer in a linked list?

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.

Single Linked Lists

6. How is memory dynamically allocated for nodes in a linked list?

o Answer: Memory is allocated using functions like malloc() in C or new in C++/Java at


runtime.

7. Explain the process of traversing a 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: O(1), as it requires updating the head pointer only.

 O(n) at the end because of traversal

9. How do you delete a node in the middle of a linked list?

o Answer:

 Traverse to the node before the one to be deleted.

 Update its pointer to skip the node to be deleted.

 Free the memory of the deleted node.

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

11. What is a header linked list?

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:

 Grounded: The last node points to NULL.

 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.

14. How do you traverse a circular header linked list?

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.

Circular Linked Lists

16. What is a circular linked list?

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.

18. How do you check if a linked list is circular?

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.

Two-Way (Doubly) Linked Lists

21. What is a two-way (doubly) linked list?

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:

 Easier traversal in both directions.

 Efficient deletion of nodes without traversing from the head.

23. What is the additional memory overhead in a doubly linked list?

o Answer: Each node requires extra memory for storing the pointer to the previous node.

24. How do you delete a node in a doubly linked list?

o Answer:

 Update the previous node's next pointer and the next node's prev pointer to
skip the node to be deleted.

 Free the memory of the deleted node.

25. What is the time complexity for inserting a node after a given node in a doubly linked list?

o Answer: O(1) if the node is already known.

Time Complexity for Deleting a Node

26. Singly Linked List


o Time Complexity:
 O(1): If the node to be deleted is the head and you have a pointer to it.
 O(n): If the node is in the middle or end (traversal required to locate the
previous node).
27. Circular Linked List
o Time Complexity:
 O(1): If the node is the head and you have a pointer to it.
 O(n): For other nodes, as traversal may be needed to locate the previous
node.
28. Doubly Linked List
o Time Complexity:
 O(1): No traversal is required, as each node has a pointer to its previous
node, allowing direct access for deletion.
O(n): if the element to be deleted is last element.

29. Circular Header Linked List
o Time Complexity:
 O(1): If the node to be deleted is immediately after the header node or at a
known position.
 O(n): If traversal is required to locate the node in cases where it is further
down the list.

Unit 3

 What is a stack, and how is it represented in memory?

 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 are the primary operations on a stack?

 Answer: The main operations on a stack are:


o Push: Add an element to the top of the stack.
o Pop: Remove the element from the top of the stack.
o Peek/Top: Retrieve the element from the top without removing it.
o IsEmpty: Check if the stack is empty.

 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 Polish Notation, and how is it related to stack operations?

 Answer: Polish Notation (also known as prefix notation) is a mathematical notation


where operators precede their operands. It can be evaluated using a stack by pushing
operands onto the stack and applying operators to operands as they are encountered.

 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.

 What is a queue, and how is it represented in memory?

 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 are the primary operations on a queue?

 Answer: The main operations on a queue are:


o Enqueue: Add an element to the rear of the queue.
o Dequeue: Remove an element from the front of the queue.
o Front: Retrieve the element at the front without removing it.
o IsEmpty: Check if the queue is empty.

 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.

 What is the time complexity of insertion and deletion in a priority queue?

 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

1. What is a binary tree?


o Answer: A binary tree is a tree data structure where each node has at most two
children, referred to as the left child and the right child.
2. What is a complete binary tree?
o Answer: A complete binary tree is a binary tree where all levels are fully filled
except possibly the last, which is filled from left to right.
3. What is an extended binary tree?
o Answer: An extended binary tree is a binary tree where every node has two
children, either an actual child or a dummy (null) child.
4. What are the memory representations of binary trees?
o Answer: A binary tree can be represented in two ways:
 Linked representation: Each node contains a data value, a pointer to the
left child, and a pointer to the right child.
 Sequential representation: Stored in an array where the index of each
node is used to represent its position, and children are found at indices 2i +
1 (left child) and 2i + 2 (right child).
5. What is a Binary Search Tree (BST)?
o Answer: A Binary Search Tree is a binary tree in which for each node, the left
child has a value smaller than the parent node, and the right child has a value
greater than the parent node.
6. What is the time complexity of searching for an element in a Binary Search Tree?
o Answer: The time complexity is O(h), where h is the height of the tree. In the
worst case, the height could be O(n) for a skewed tree, and in the best case, it
could be O(log n) for a balanced tree.
7. What is the time complexity of inserting a node in a Binary Search Tree?
o Answer: The time complexity is O(h), where h is the height of the tree, similar to
the search operation. For a balanced tree, h is O(log n), and for a skewed tree, h is
O(n).
8. How do you delete a node in a Binary Search Tree?
o Answer: There are three cases:
 If the node has no children, simply remove it.
 If the node has one child, replace the node with its child.
 If the node has two children, replace the node with the in-order
predecessor or successor and remove the predecessor/successor.
9. What is the time complexity of deletion in a Binary Search Tree?
o Answer: The time complexity is O(h), where h is the height of the tree. In a
balanced tree, h = O(log n), and in the worst case, h = O(n).
10. What is In-order traversal of a binary tree?
o Answer: In-order traversal visits nodes in the following order: left subtree, root,
right subtree. This traversal produces nodes in ascending order for a Binary
Search Tree.
11. What is the time complexity of In-order traversal?
o Answer: The time complexity is O(n), where n is the number of nodes in the tree,
as each node is visited once.
12. What is Pre-order traversal of a binary tree?
o Answer: Pre-order traversal visits nodes in the following order: root, left subtree,
right subtree. This is useful for creating a copy of the tree.
13. What is the time complexity of Pre-order traversal?
o Answer: The time complexity is O(n), where n is the number of nodes in the tree.
14. What is Post-order traversal of a binary tree?
o Answer: Post-order traversal visits nodes in the following order: left subtree,
right subtree, root. This is useful for deleting nodes or evaluating expressions.
15. What is the time complexity of Post-order traversal?
o Answer: The time complexity is O(n), where n is the number of nodes in the tree.

Recursion Questions

16. What is recursion?


o Answer: Recursion is a programming technique where a function calls itself to
solve smaller instances of the same problem. It usually has a base case to
terminate the recursive calls.
17. What is the time complexity of the recursive implementation of the Towers of Hanoi
problem?
o Answer: The time complexity of the Towers of Hanoi problem is O(2^n), where
n is the number of disks. The number of moves required to solve the problem
doubles with each additional disk.
18. How does Merge Sort work, and what is its time complexity?
o Answer: Merge Sort is a divide-and-conquer algorithm that divides the array into
two halves, recursively sorts them, and then merges the two sorted halves. Its time
complexity is O(n log n), where n is the number of elements to be sorted.
19. How does Quick Sort work, and what is its time complexity?
o Answer: Quick Sort is a divide-and-conquer algorithm that selects a pivot,
partitions the array into two sub-arrays based on the pivot, and recursively sorts
the sub-arrays. Its average time complexity is O(n log n), but in the worst case
(when the pivot is poorly chosen), it can be O(n^2).
20. What is the space complexity of Merge Sort and Quick Sort?
o Answer:
 Merge Sort: Space complexity is O(n) because it requires extra space for
the temporary arrays during merging.
 Quick Sort: Space complexity is O(log n) on average due to the recursive
stack calls, but in the worst case (for a skewed partition), it can be O(n).
Unit 5
1. What is an AVL tree?

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.

2. What is the time complexity for insertion in an AVL tree?

o Answer: The time complexity is O(log n) because the tree remains balanced, and
rebalancing operations (rotations) are performed in O(1) time.

3. How is rebalancing achieved in an AVL tree after insertion?

o Answer: Rebalancing is done through rotations:

 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.

 Left-Right Rotation (LR): Fixes imbalance caused by an insertion in the right


subtree of the left child.

 Right-Left Rotation (RL): Fixes imbalance caused by an insertion in the left


subtree of the right child.

4. What is the time complexity of deletion in an AVL tree?

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

6. What is a binary heap?

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.

7. What is the time complexity of insertion in a binary heap?


o Answer: The time complexity is O(log n) because the new element may need to bubble
up to maintain the heap property.

8. What is the time complexity of deletion in a binary heap?

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.

9. How does HeapSort work, and what is its time complexity?

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.

10. What is the difference between a B-Tree and a B+ Tree?

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

1. What is Warshall's algorithm used for?

o Answer: Warshall's algorithm is used to compute the transitive closure of a directed


graph, determining the reachability of nodes.

2. What is the time complexity of Warshall's algorithm?

o Answer: The time complexity is O(V³), where VVV is the number of vertices.

3. How does Breadth-First Search (BFS) work?

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.

4. What is the time complexity of BFS?

o Answer: The time complexity is O(V + E), where VVV is the number of vertices and EEE is
the number of edges.

5. How does Depth-First Search (DFS) work?


o Answer: DFS explores as far along a branch as possible before backtracking, using
recursion or a stack for implementation.

6. What is the time complexity of DFS?

o Answer: The time complexity is O(V + E).

7. What is the difference between BFS and DFS?

o Answer: BFS explores level by level using a queue, while DFS explores depth-first using a
stack or recursion.

8. What is Floyd-Warshall Algorithm?

o Answer: Floyd-Warshall is an algorithm to find the shortest paths between all pairs of
vertices in a weighted graph.

9. What is the time complexity of Floyd-Warshall Algorithm?

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?

o Answer: Warshall's algorithm computes transitive closure, while Floyd-Warshall


computes the shortest paths between all pairs of nodes.

Hashing

11. What is hashing?

o Answer: Hashing is a technique to map data to a fixed-size hash table using a hash
function, enabling efficient data retrieval.

12. What is a hash function?

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.

13. What is open hashing?

o Answer: Open hashing, or separate chaining, resolves collisions by maintaining a linked


list for all elements that hash to the same index.

14. What is closed hashing?

o Answer: Closed hashing, or open addressing, resolves collisions by probing (searching


for) other slots in the hash table.

15. What is linear probing?

o Answer: Linear probing resolves collisions by checking subsequent slots one by one until
an empty slot is found.

16. What is the time complexity of insertion in linear probing?


o Answer: The average-case complexity is O(1), but the worst case is O(n) when the hash
table is nearly full.

17. What is quadratic probing?

o Answer: Quadratic probing resolves collisions by checking slots at an increasing interval


(e.g., i2i^2i2, 222^222, etc.) to reduce clustering.

18. What is double hashing?

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.

20. What is the time complexity of search operations in a 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).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy