UNIT I
Basic Concepts
1. What is a data structure? Give examples.
A data structure organizes data for efficient access and modification. Examples: Arrays,
Linked Lists, Stacks.
2. Differentiate between linear and non-linear data structures.
Linear: Sequential arrangement (e.g., Arrays). Non-linear: Hierarchical (e.g., Trees).
3. What are arrays? How are they stored in memory?
Arrays are fixed-size, same-type collections stored in contiguous memory locations.
4. What is a pointer in C, and how is it used in data structures?
A pointer stores the address of a variable, enabling dynamic memory allocation and
linked data structures.
5. Explain the difference between static and dynamic memory allocation in C.
Static: Fixed size at compile time. Dynamic: Allocated during runtime using functions
like malloc.
6. How is malloc() different from calloc()?
malloc allocates uninitialized memory, while calloc initializes memory to zero.
Linked List
7. What is a linked list? How is it different from an array?
A linked list is a dynamic structure where elements are linked via pointers, unlike arrays
with fixed sizes.
8. Explain the types of linked lists: singly, doubly, and circular.
Singly: Points to the next node. Doubly: Points to next and previous. Circular: Last node
points to the first.
9. What is the time complexity of inserting a node at the beginning of a singly linked
list?
O(1), as only the head pointer and the new node's pointer need to be updated.
10. How can you reverse a singly linked list?
By iterating through the list and reversing the next pointers of nodes.
11. Write a C function to delete a node from a linked list.
Free the node after updating the previous node’s pointer to skip the target node.
Stacks
12. What is a stack? Where is it used?
A stack is a LIFO structure used in recursion, expression evaluation, and undo operations.
13. Explain the difference between stack implemented using arrays and linked lists.
Arrays have fixed size, whereas linked lists are dynamic.
14. What is stack overflow and underflow?
Overflow: Adding to a full stack. Underflow: Removing from an empty stack.
15. What is the role of the top variable in stack implementation?
top indicates the position of the most recently added element.
16. Write a C program to evaluate a postfix expression using a stack.
Push operands; pop, compute, and push results for operators.
Queues
17. What is a queue? How does it differ from a stack?
Queue: FIFO. Stack: LIFO.
18. What is a circular queue? How does it overcome the limitations of a simple queue?
A circular queue uses available space by connecting the rear to the front.
19. What are priority queues, and where are they used?
A queue where higher priority elements are dequeued first, e.g., in scheduling.
20. Write a C function to implement enqueue and dequeue operations for a queue.
Use an array to insert at rear and remove from front.
UNIT II
1. What is a hash table, and why is it used?
A hash table is a data structure that stores key-value pairs for fast access, used for quick
searches, insertions, and deletions.
2. What is hashing, and how does it work?
Hashing is the process of converting a key into an index using a hash function to store
data in a hash table.
3. What are the key advantages of using a hash table?
Hash tables offer average-case O(1) time complexity for search, insert, and delete
operations.
4. What is a hash function? What properties should a good hash function have?
A hash function maps keys to table indices. It should minimize collisions, distribute keys
uniformly, and be efficient to compute.
5. Can two different keys have the same hash value? What is this called?
Yes, it is called a collision.
Collisions
6. What is a hash collision?
A collision occurs when two different keys produce the same hash value.
7. What are the methods to handle hash collisions?
Common methods are separate chaining and open addressing.
8. Explain open addressing.
In open addressing, collisions are resolved by finding another empty slot in the hash table
using techniques like linear probing, quadratic probing, or double hashing.
9. What is separate chaining?
In separate chaining, each slot in the hash table contains a linked list to store multiple
keys with the same hash value.
10. Which is better: open addressing or chaining, and why?
Chaining is better for handling large numbers of collisions, while open addressing is
space-efficient.
Performance
11. What is the time complexity of searching, inserting, and deleting in a hash table?
Average case: O(1); Worst case (with collisions): O(n).
12. What is load factor in a hash table?
Load factor = (Number of elements) ÷ (Size of hash table). It indicates how full the table
is.
13. How does the load factor affect the performance of a hash table?
Higher load factors increase collisions, slowing performance. A load factor > 0.7 often
triggers resizing.
14. What is rehashing? When is it done?
Rehashing is resizing the hash table and re-computing hash values to reduce collisions,
typically when the load factor exceeds a threshold.
15. How can a poorly designed hash function impact performance?
A poor hash function increases collisions, leading to slower search, insert, and delete
operations.
Applications
16. Give some real-world applications of hash tables.
Used in database indexing, caches, symbol tables, and sets/maps in programming.
17. How are hash tables used in database indexing?
Hash tables quickly map keys (e.g., IDs) to records for fast retrieval in databases.
18. What is a hash map, and how is it different from a hash table?
A hash map is similar to a hash table but does not support synchronization (used in
multithreading).
19. Why are hash tables not suitable for ordered data?
Hash tables do not maintain order; they focus on fast access.
20. How can you implement a hash table in C?
Use an array for storage, a hash function for index calculation, and separate chaining or
open addressing for collision handling.
UNIT III
Basic Concepts
1. What is a tree in data structures?
A tree is a hierarchical data structure consisting of nodes, where one node is the root, and
every other node has a parent and possibly children.
2. What are the basic terms associated with trees?
o Root: Topmost node.
o Parent/Child: Nodes connected by edges.
o Leaf: Node with no children.
o Height: Longest path from the root to a leaf.
o Depth: Distance of a node from the root.
3. What is the difference between a binary tree and a binary search tree (BST)?
o Binary Tree: Each node has at most two children.
o BST: A binary tree where left children are smaller, and right children are greater
than the parent.
4. What is the height of a tree?
The height is the number of edges on the longest path from the root to a leaf.
5. What is the degree of a node?
The degree is the number of children a node has.
Types of Trees
6. What is a complete binary tree?
A binary tree where all levels except possibly the last are fully filled, and nodes in the last
level are as left as possible.
7. What is a full binary tree?
A binary tree where every node has either 0 or 2 children.
8. What is an AVL tree?
A self-balancing binary search tree where the difference in height between the left and
right subtrees of any node is at most 1.
9. What is a B-tree?
A self-balancing search tree designed for databases and file systems, where nodes can
have multiple keys and children.
10. What is a red-black tree?
A self-balancing binary search tree with nodes colored red or black, ensuring balanced
height.
Traversals
11. What are tree traversal methods?
o Inorder: Left → Root → Right.
o Preorder: Root → Left → Right.
o Postorder: Left → Right → Root.
o Level-order: Visit nodes level by level.
12. What is the time complexity of tree traversal?
O(n), where n is the number of nodes.
13. Which tree traversal is used for sorting in BST?
Inorder traversal, as it visits nodes in ascending order.
Properties and Applications
14. What is the maximum number of nodes in a binary tree of height h?
2^(h+1) - 1, where h is the height of the tree.
15. What are the applications of trees?
o File systems.
o Binary search trees for searching.
o Expression evaluation in binary expression trees.
o Network routing.
16. What is a heap tree?
A binary tree where the value of each node is greater (max heap) or smaller (min heap)
than its children.
17. What is the difference between a tree and a graph?
A tree is a connected acyclic graph, while a graph may have cycles and multiple
connections.
18. What is a spanning tree?
A subgraph of a graph that includes all the vertices with the minimum number of edges
and no cycles.
Binary Search Tree Operations
19. What is the time complexity for searching in a balanced BST?
O(log n), where n is the number of nodes.
20. How do you delete a node in a BST?
o Leaf node: Remove it directly.
o One child: Replace the node with its child.
o Two children: Replace the node with its in-order successor or predecessor.
UNIT IV
Basic Concepts
1. What is a graph in data structures?
A graph is a collection of vertices (nodes) connected by edges, representing relationships
between elements.
2. What are the types of graphs?
o Directed Graph: Edges have directions.
o Undirected Graph: Edges have no direction.
o Weighted Graph: Edges have weights.
o Unweighted Graph: Edges have no weights.
3. What is the difference between a graph and a tree?
A tree is a connected, acyclic graph, whereas a graph may have cycles and need not be
connected.
4. What are the basic terminologies in graphs?
o Vertex: A node in the graph.
o Edge: Connection between two vertices.
o Degree: Number of edges connected to a vertex.
o Path: Sequence of vertices connected by edges.
o Cycle: A path that starts and ends at the same vertex.
5. What is an adjacency matrix?
A 2D array where each cell represents the presence (1) or absence (0) of an edge between
vertices.
Graph Representation
6. What are the ways to represent a graph?
o Adjacency Matrix: Uses a 2D array.
o Adjacency List: Uses a list where each vertex points to its neighbors.
7. Which is better: adjacency matrix or adjacency list?
o Adjacency Matrix: Better for dense graphs (many edges).
o Adjacency List: Better for sparse graphs (fewer edges).
8. What is an incidence matrix?
A matrix where rows represent vertices, columns represent edges, and entries show the
connection.
Graph Traversal
9. What are the methods of graph traversal?
o Depth-First Search (DFS): Explores as far as possible along each branch before
backtracking.
o Breadth-First Search (BFS): Explores all neighbors of a vertex before moving
to the next level.
10. What is the time complexity of DFS and BFS?
O(V + E), where V is the number of vertices and E is the number of edges.
Graph Properties
11. What is a connected graph?
A graph where there is a path between every pair of vertices.
12. What is a complete graph?
A graph where every pair of vertices is connected by an edge.
13. What is a bipartite graph?
A graph whose vertices can be divided into two sets such that no two vertices within the
same set are adjacent.
14. What is a directed acyclic graph (DAG)?
A directed graph with no cycles, used in applications like task scheduling.
UNIT V
Basic Concepts
1. What is pattern matching in computer science?
Pattern matching is the process of finding one or more occurrences of a pattern
(substring) within a text.
2. What are the different types of pattern matching?
o Exact Pattern Matching: Matches exact sequences.
o Approximate Pattern Matching: Matches sequences with some variations.
o Regular Expression Matching: Matches patterns using rules and symbols.
3. What are the applications of pattern matching?
o Text searching (e.g., word processors).
o DNA sequence analysis.
o Search engines.
o Spam filters and plagiarism detection.
Pattern Matching Algorithms
4. What are some common algorithms used for pattern matching?
o Naive Pattern Matching: Brute force approach.
o Knuth-Morris-Pratt (KMP): Uses prefix table for efficient matching.
o Rabin-Karp: Uses hashing for pattern search.
o Boyer-Moore: Skips unnecessary comparisons using heuristics.
5. Explain the Naive Pattern Matching algorithm.
Compares the pattern with every possible substring in the text, one character at a time.
6. What is the time complexity of Naive Pattern Matching?
O(m * n), where m is the pattern length and n is the text length.
7. What is the Knuth-Morris-Pratt (KMP) algorithm?
KMP uses a prefix table to skip redundant comparisons, improving efficiency.
8. What is the time complexity of the KMP algorithm?
O(m + n), where m is the pattern length and n is the text length.
9. What is the Rabin-Karp algorithm?
A pattern-matching algorithm that uses hashing to compare the pattern with substrings in
the text.
10. What is the time complexity of the Rabin-Karp algorithm?
Average case: O(m + n), Worst case: O(m * n) (when hash collisions occur).
11. What is the Boyer-Moore algorithm?
A pattern matching algorithm that uses bad character and good suffix heuristics to skip
unnecessary comparisons.
12. What is the time complexity of the Boyer-Moore algorithm?
Best case: O(n/m), Worst case: O(m * n).
Properties and Performance
13. Why is KMP better than Naive Pattern Matching?
KMP avoids redundant comparisons by precomputing a prefix table, making it more
efficient for large inputs.
14. What is a prefix table in KMP?
A table that stores the lengths of the proper prefix, which is also a suffix, for the pattern
to optimize matching.
15. How does the Rabin-Karp algorithm handle hash collisions?
If the hash values of the pattern and substring match, it performs character-by-character
comparison as a fallback.
16. Which algorithm is better for single pattern matching in large texts?
KMP or Boyer-Moore, depending on the input and preprocessing requirements.
17. What is the role of preprocessing in pattern matching algorithms?
Preprocessing (e.g., prefix table in KMP or heuristics in Boyer-Moore) reduces the
number of comparisons during matching.
Applications
18. How is pattern matching used in text editors?
It is used to search and highlight words, phrases, or patterns in documents.
19. How is pattern matching applied in bioinformatics?
It is used to identify genes, protein sequences, or DNA motifs.
20. What is regular expression (regex) matching?
Regex matching uses a formal language to define patterns for searching or manipulating
strings.

Data Structure viva questions for lab exam

  • 1.
    UNIT I Basic Concepts 1.What is a data structure? Give examples. A data structure organizes data for efficient access and modification. Examples: Arrays, Linked Lists, Stacks. 2. Differentiate between linear and non-linear data structures. Linear: Sequential arrangement (e.g., Arrays). Non-linear: Hierarchical (e.g., Trees). 3. What are arrays? How are they stored in memory? Arrays are fixed-size, same-type collections stored in contiguous memory locations. 4. What is a pointer in C, and how is it used in data structures? A pointer stores the address of a variable, enabling dynamic memory allocation and linked data structures. 5. Explain the difference between static and dynamic memory allocation in C. Static: Fixed size at compile time. Dynamic: Allocated during runtime using functions like malloc. 6. How is malloc() different from calloc()? malloc allocates uninitialized memory, while calloc initializes memory to zero. Linked List 7. What is a linked list? How is it different from an array? A linked list is a dynamic structure where elements are linked via pointers, unlike arrays with fixed sizes. 8. Explain the types of linked lists: singly, doubly, and circular. Singly: Points to the next node. Doubly: Points to next and previous. Circular: Last node points to the first. 9. What is the time complexity of inserting a node at the beginning of a singly linked list? O(1), as only the head pointer and the new node's pointer need to be updated. 10. How can you reverse a singly linked list? By iterating through the list and reversing the next pointers of nodes. 11. Write a C function to delete a node from a linked list. Free the node after updating the previous node’s pointer to skip the target node. Stacks 12. What is a stack? Where is it used? A stack is a LIFO structure used in recursion, expression evaluation, and undo operations. 13. Explain the difference between stack implemented using arrays and linked lists. Arrays have fixed size, whereas linked lists are dynamic.
  • 2.
    14. What isstack overflow and underflow? Overflow: Adding to a full stack. Underflow: Removing from an empty stack. 15. What is the role of the top variable in stack implementation? top indicates the position of the most recently added element. 16. Write a C program to evaluate a postfix expression using a stack. Push operands; pop, compute, and push results for operators. Queues 17. What is a queue? How does it differ from a stack? Queue: FIFO. Stack: LIFO. 18. What is a circular queue? How does it overcome the limitations of a simple queue? A circular queue uses available space by connecting the rear to the front. 19. What are priority queues, and where are they used? A queue where higher priority elements are dequeued first, e.g., in scheduling. 20. Write a C function to implement enqueue and dequeue operations for a queue. Use an array to insert at rear and remove from front.
  • 3.
    UNIT II 1. Whatis a hash table, and why is it used? A hash table is a data structure that stores key-value pairs for fast access, used for quick searches, insertions, and deletions. 2. What is hashing, and how does it work? Hashing is the process of converting a key into an index using a hash function to store data in a hash table. 3. What are the key advantages of using a hash table? Hash tables offer average-case O(1) time complexity for search, insert, and delete operations. 4. What is a hash function? What properties should a good hash function have? A hash function maps keys to table indices. It should minimize collisions, distribute keys uniformly, and be efficient to compute. 5. Can two different keys have the same hash value? What is this called? Yes, it is called a collision. Collisions 6. What is a hash collision? A collision occurs when two different keys produce the same hash value. 7. What are the methods to handle hash collisions? Common methods are separate chaining and open addressing. 8. Explain open addressing. In open addressing, collisions are resolved by finding another empty slot in the hash table using techniques like linear probing, quadratic probing, or double hashing. 9. What is separate chaining? In separate chaining, each slot in the hash table contains a linked list to store multiple keys with the same hash value. 10. Which is better: open addressing or chaining, and why? Chaining is better for handling large numbers of collisions, while open addressing is space-efficient. Performance 11. What is the time complexity of searching, inserting, and deleting in a hash table? Average case: O(1); Worst case (with collisions): O(n). 12. What is load factor in a hash table? Load factor = (Number of elements) ÷ (Size of hash table). It indicates how full the table is.
  • 4.
    13. How doesthe load factor affect the performance of a hash table? Higher load factors increase collisions, slowing performance. A load factor > 0.7 often triggers resizing. 14. What is rehashing? When is it done? Rehashing is resizing the hash table and re-computing hash values to reduce collisions, typically when the load factor exceeds a threshold. 15. How can a poorly designed hash function impact performance? A poor hash function increases collisions, leading to slower search, insert, and delete operations. Applications 16. Give some real-world applications of hash tables. Used in database indexing, caches, symbol tables, and sets/maps in programming. 17. How are hash tables used in database indexing? Hash tables quickly map keys (e.g., IDs) to records for fast retrieval in databases. 18. What is a hash map, and how is it different from a hash table? A hash map is similar to a hash table but does not support synchronization (used in multithreading). 19. Why are hash tables not suitable for ordered data? Hash tables do not maintain order; they focus on fast access. 20. How can you implement a hash table in C? Use an array for storage, a hash function for index calculation, and separate chaining or open addressing for collision handling.
  • 5.
    UNIT III Basic Concepts 1.What is a tree in data structures? A tree is a hierarchical data structure consisting of nodes, where one node is the root, and every other node has a parent and possibly children. 2. What are the basic terms associated with trees? o Root: Topmost node. o Parent/Child: Nodes connected by edges. o Leaf: Node with no children. o Height: Longest path from the root to a leaf. o Depth: Distance of a node from the root. 3. What is the difference between a binary tree and a binary search tree (BST)? o Binary Tree: Each node has at most two children. o BST: A binary tree where left children are smaller, and right children are greater than the parent. 4. What is the height of a tree? The height is the number of edges on the longest path from the root to a leaf. 5. What is the degree of a node? The degree is the number of children a node has. Types of Trees 6. What is a complete binary tree? A binary tree where all levels except possibly the last are fully filled, and nodes in the last level are as left as possible. 7. What is a full binary tree? A binary tree where every node has either 0 or 2 children. 8. What is an AVL tree? A self-balancing binary search tree where the difference in height between the left and right subtrees of any node is at most 1. 9. What is a B-tree? A self-balancing search tree designed for databases and file systems, where nodes can have multiple keys and children. 10. What is a red-black tree? A self-balancing binary search tree with nodes colored red or black, ensuring balanced height.
  • 6.
    Traversals 11. What aretree traversal methods? o Inorder: Left → Root → Right. o Preorder: Root → Left → Right. o Postorder: Left → Right → Root. o Level-order: Visit nodes level by level. 12. What is the time complexity of tree traversal? O(n), where n is the number of nodes. 13. Which tree traversal is used for sorting in BST? Inorder traversal, as it visits nodes in ascending order. Properties and Applications 14. What is the maximum number of nodes in a binary tree of height h? 2^(h+1) - 1, where h is the height of the tree. 15. What are the applications of trees? o File systems. o Binary search trees for searching. o Expression evaluation in binary expression trees. o Network routing. 16. What is a heap tree? A binary tree where the value of each node is greater (max heap) or smaller (min heap) than its children. 17. What is the difference between a tree and a graph? A tree is a connected acyclic graph, while a graph may have cycles and multiple connections. 18. What is a spanning tree? A subgraph of a graph that includes all the vertices with the minimum number of edges and no cycles. Binary Search Tree Operations 19. What is the time complexity for searching in a balanced BST? O(log n), where n is the number of nodes. 20. How do you delete a node in a BST? o Leaf node: Remove it directly. o One child: Replace the node with its child. o Two children: Replace the node with its in-order successor or predecessor.
  • 7.
    UNIT IV Basic Concepts 1.What is a graph in data structures? A graph is a collection of vertices (nodes) connected by edges, representing relationships between elements. 2. What are the types of graphs? o Directed Graph: Edges have directions. o Undirected Graph: Edges have no direction. o Weighted Graph: Edges have weights. o Unweighted Graph: Edges have no weights. 3. What is the difference between a graph and a tree? A tree is a connected, acyclic graph, whereas a graph may have cycles and need not be connected. 4. What are the basic terminologies in graphs? o Vertex: A node in the graph. o Edge: Connection between two vertices. o Degree: Number of edges connected to a vertex. o Path: Sequence of vertices connected by edges. o Cycle: A path that starts and ends at the same vertex. 5. What is an adjacency matrix? A 2D array where each cell represents the presence (1) or absence (0) of an edge between vertices. Graph Representation 6. What are the ways to represent a graph? o Adjacency Matrix: Uses a 2D array. o Adjacency List: Uses a list where each vertex points to its neighbors. 7. Which is better: adjacency matrix or adjacency list? o Adjacency Matrix: Better for dense graphs (many edges). o Adjacency List: Better for sparse graphs (fewer edges). 8. What is an incidence matrix? A matrix where rows represent vertices, columns represent edges, and entries show the connection. Graph Traversal
  • 8.
    9. What arethe methods of graph traversal? o Depth-First Search (DFS): Explores as far as possible along each branch before backtracking. o Breadth-First Search (BFS): Explores all neighbors of a vertex before moving to the next level. 10. What is the time complexity of DFS and BFS? O(V + E), where V is the number of vertices and E is the number of edges. Graph Properties 11. What is a connected graph? A graph where there is a path between every pair of vertices. 12. What is a complete graph? A graph where every pair of vertices is connected by an edge. 13. What is a bipartite graph? A graph whose vertices can be divided into two sets such that no two vertices within the same set are adjacent. 14. What is a directed acyclic graph (DAG)? A directed graph with no cycles, used in applications like task scheduling.
  • 9.
    UNIT V Basic Concepts 1.What is pattern matching in computer science? Pattern matching is the process of finding one or more occurrences of a pattern (substring) within a text. 2. What are the different types of pattern matching? o Exact Pattern Matching: Matches exact sequences. o Approximate Pattern Matching: Matches sequences with some variations. o Regular Expression Matching: Matches patterns using rules and symbols. 3. What are the applications of pattern matching? o Text searching (e.g., word processors). o DNA sequence analysis. o Search engines. o Spam filters and plagiarism detection. Pattern Matching Algorithms 4. What are some common algorithms used for pattern matching? o Naive Pattern Matching: Brute force approach. o Knuth-Morris-Pratt (KMP): Uses prefix table for efficient matching. o Rabin-Karp: Uses hashing for pattern search. o Boyer-Moore: Skips unnecessary comparisons using heuristics. 5. Explain the Naive Pattern Matching algorithm. Compares the pattern with every possible substring in the text, one character at a time. 6. What is the time complexity of Naive Pattern Matching? O(m * n), where m is the pattern length and n is the text length. 7. What is the Knuth-Morris-Pratt (KMP) algorithm? KMP uses a prefix table to skip redundant comparisons, improving efficiency. 8. What is the time complexity of the KMP algorithm? O(m + n), where m is the pattern length and n is the text length. 9. What is the Rabin-Karp algorithm? A pattern-matching algorithm that uses hashing to compare the pattern with substrings in the text. 10. What is the time complexity of the Rabin-Karp algorithm? Average case: O(m + n), Worst case: O(m * n) (when hash collisions occur). 11. What is the Boyer-Moore algorithm? A pattern matching algorithm that uses bad character and good suffix heuristics to skip unnecessary comparisons.
  • 10.
    12. What isthe time complexity of the Boyer-Moore algorithm? Best case: O(n/m), Worst case: O(m * n). Properties and Performance 13. Why is KMP better than Naive Pattern Matching? KMP avoids redundant comparisons by precomputing a prefix table, making it more efficient for large inputs. 14. What is a prefix table in KMP? A table that stores the lengths of the proper prefix, which is also a suffix, for the pattern to optimize matching. 15. How does the Rabin-Karp algorithm handle hash collisions? If the hash values of the pattern and substring match, it performs character-by-character comparison as a fallback. 16. Which algorithm is better for single pattern matching in large texts? KMP or Boyer-Moore, depending on the input and preprocessing requirements. 17. What is the role of preprocessing in pattern matching algorithms? Preprocessing (e.g., prefix table in KMP or heuristics in Boyer-Moore) reduces the number of comparisons during matching. Applications 18. How is pattern matching used in text editors? It is used to search and highlight words, phrases, or patterns in documents. 19. How is pattern matching applied in bioinformatics? It is used to identify genes, protein sequences, or DNA motifs. 20. What is regular expression (regex) matching? Regex matching uses a formal language to define patterns for searching or manipulating strings.