1.
Introduction to Algorithm Analysis
1.1 Complexity Theory
● Time Complexity: Measures how runtime grows with input size.
● Space Complexity: Measures memory usage relative to input size.
● Importance: Helps compare algorithms and optimize performance.
1.2 Asymptotic Notations
● Big-O (O): Upper bound (worst-case scenario).
● Omega (Ω): Lower bound (best-case scenario).
● Theta (Θ): Tight bound (average-case scenario).
2. Arrays, Stacks, and Queues
2.1 Arrays
● Definition: Contiguous memory allocation for homogeneous data.
● Operations:
○ Access: O(1) (direct indexing).
○ Insertion/Deletion: O(n) (shifting required).
2.2 Stacks (LIFO Principle)
● Operations:
○ Push: Insert at top (O(1)).
○ Pop: Remove from top (O(1)).
○ Peek: View top element (O(1)).
● Applications: Function call stack, undo operations.
2.3 Queues (FIFO Principle)
● Operations:
○ Enqueue: Insert at rear (O(1)).
○ Dequeue: Remove from front (O(1)).
● Variants:
○ Circular Queue: Efficient space usage.
○ Priority Queue: Serves highest priority first.
3. Linked Lists
3.1 Singly Linked List
● Structure: Nodes with data and next pointer.
● Operations:
○ Insertion:
■ At head (O(1)).
■ At tail (O(n)).
○ Deletion: Similar to insertion.
3.2 Doubly Linked List
● Structure: Nodes with prev and next pointers.
● Advantage: Bidirectional traversal.
3.3 Circular Linked List
● Tail node points back to head.
● Applications: Round-robin scheduling.
4. Trees
4.1 Binary Tree
● Properties:
○ Each node has ≤ 2 children.
○ Maximum nodes at level L = 2^L.
● Traversals:
○ Inorder (LNR): Left → Root → Right.
○ Preorder (NLR): Root → Left → Right.
○ Postorder (LRN): Left → Right → Root.
4.2 Binary Search Tree (BST)
● Property: Left subtree < Root < Right subtree.
● Search: O(log n) (if balanced), O(n) (worst-case).
4.3 AVL Tree (Self-Balancing BST)
● Balancing Condition: Height difference ≤ 1.
● Rotations: Left, Right, Left-Right, Right-Left.
5. Graphs
5.1 Types of Graphs
● Undirected: Edges have no direction (e.g., Facebook friends).
● Directed: Edges have direction (e.g., Twitter follows).
● Weighted/Unweighted: Edges may have weights.
5.2 Graph Traversals
● BFS (Breadth-First Search): Level-order traversal (uses queue).
● DFS (Depth-First Search): Explores deep paths first (uses stack).
5.4 Minimum Spanning Tree (MST)
● Prim’s Algorithm: Greedy approach, grows MST from a start node.
● Kruskal’s Algorithm: Uses Union-Find, sorts edges by weight.
6. Sorting & Searching
6.1 Sorting Algorithms
6.2 Searching Algorithms
● Linear Search: O(n) (works on unsorted data).
● Binary Search: O(log n) (requires sorted data).
● Hashing: O(1) average, O(n) worst-case (collisions).
7. C Programming Fundamentals
7.1 Memory Management
● Static Allocation: Fixed at compile time (e.g., arrays).
● Dynamic Allocation: Runtime allocation (e.g., malloc, free).
7.2 Structures vs. Unions
● Structure: Allocates memory for all members.
● Union: Shares memory among members (only one active at a time).
MCQS
1. Introduction to Algorithms & Complexity
1. What does Big-O notation describe?
a) Exact runtime of an algorithm
b) Upper bound of an algorithm's growth rate
c) Average-case performance
d) Space complexity only
Answer: b) Upper bound of an algorithm's growth rate
2. An algorithm with complexity O(1) is:
a) Linear
b) Constant time
c) Quadratic
d) Logarithmic
Answer: b) Constant time
3. The worst-case complexity of Bubble Sort is:
a) O(n)
b) O(n log n)
c) O(n²)
d) O(2ⁿ)
Answer: c) O(n²)
4. Which notation represents the tightest asymptotic bound?
a) Big-O
b) Big-Ω
c) Big-Θ
d) Little-o
Answer: c) Big-Θ
5. Recursive Fibonacci has a complexity of:
a) O(n)
b) O(2ⁿ)
c) O(log n)
d) O(n²)
Answer: b) O(2ⁿ)
2. Arrays, Stacks & Queues
6. Stack follows which principle?
a) FIFO
b) LIFO
c) Priority-based
d) Random access
Answer: b) LIFO
7. The operation to remove an element from a queue is called:
a) Push
b) Pop
c) Enqueue
d) Dequeue
Answer: d) Dequeue
8. In a circular queue, front and rear pointers are equal when:
a) Queue is full
b) Queue is empty
c) Queue has one element
d) Both a & b
Answer: d) Both a & b
9. Dynamic arrays overcome the limitation of static arrays by:
a) Allowing resizing
b) Using linked lists
c) Reducing access time
d) Fixed capacity
Answer: a) Allowing resizing
10.The postfix expression for A+B*C is:
a) ABC*+
b) AB+C*
c) A*BC+
d) +A*BC
Answer: a) ABC*+
3. Linked Lists
11.Singly linked list nodes contain:
a) Data + 2 pointers
b) Data + 1 pointer
c) Only data
d) Only pointers
Answer: b) Data + 1 pointer
12.Deleting a node in a doubly linked list requires updating:
a) 1 pointer
b) 2 pointers
c) 3 pointers
d) No pointers
Answer: b) 2 pointers
13.Which operation is O(1) in a linked list with tail pointer?
a) Insert at head
b) Insert at tail
c) Delete from middle
d) Search
Answer: b) Insert at tail
14.Garbage collection is used to:
a) Allocate memory
b) Reclaim unused memory
c) Defragment disks
d) Sort data
Answer: b) Reclaim unused memory
15.A circular linked list’s last node points to:
a) NULL
b) Head
c) Tail
d) Itself
Answer: b) Head
4. Trees
16.In a binary tree, the maximum number of nodes at level *i* is:
a) *i*
b) 2i
c) 2^i
d) i²
Answer: c) 2^i
17.Postorder traversal of A(B,C) gives:
a) ABC
b) BAC
c) BCA
d) CBA
Answer: c) BCA
18.An AVL tree ensures balance by:
a) Rotations
b) Hashing
c) Randomization
d) Indexing
Answer: a) Rotations
19.B-trees are commonly used in:
a) Databases
b) Stacks
c) Queues
d) Sorting
Answer: a) Databases
20.The height of a BST with *n* nodes can be:
a) O(log n)
b) O(n)
c) Both a & b
d) O(1)
Answer: c) Both a & b
5. Graphs
21.DFS uses which data structure?
a) Queue
b) Stack
c) Heap
d) Array
Answer: b) Stack
22.Dijkstra’s algorithm solves:
a) All-pairs shortest path
b) Single-source shortest path
c) Minimum spanning tree
d) Topological sort
Answer: b) Single-source shortest path
23.A DAG has:
a) No cycles
b) Undirected edges
c) Self-loops
d) All of the above
Answer: a) No cycles
24.Prim’s algorithm is used for:
a) Shortest path
b) Minimum spanning tree
c) Sorting
d) Hashing
Answer: b) Minimum spanning tree
25.The adjacency matrix of an undirected graph is:
a) Upper triangular
b) Symmetric
c) Diagonal
d) Sparse
Answer: b) Symmetric
6. Sorting & Searching
26.Which sort has O(n log n) worst-case complexity?
a) QuickSort
b) MergeSort
c) BubbleSort
d) InsertionSort
Answer: b) MergeSort
27.HeapSort uses a:
a) BST
b) Max-Heap
c) Queue
d) Linked list
Answer: b) Max-Heap
28.Binary search requires the input to be:
a) Unsorted
b) Sorted
c) Balanced
d) Hashed
Answer: b) Sorted
29.Hashing collision resolution method:
a) Chaining
b) Indexing
c) Sorting
d) Traversal
Answer: a) Chaining
30.RadixSort sorts based on:
a) Comparisons
b) Digits/characters
c) Randomness
d) Swaps
Answer: b) Digits/characters
7. Programming Fundamentals & C Language
31.Which is a compiled language?
a) Python
b) Java
c) C
d) JavaScript
Answer: c) C
32.int *ptr declares:
a) An integer
b) A pointer to integer
c) An array
d) A function
Answer: b) A pointer to integer
33.Dynamic memory allocation in C uses:
a) malloc()
b) alloc()
c) new
d) create()
Answer: a) malloc()
34.A struct in C can contain:
a) Only variables
b) Variables and functions
c) Only functions
d) Pointers only
Answer: a) Only variables
35.The scope of a static variable is:
a) Local to block
b) Global
c) File-level
d) Temporary
Answer: c) File-level
7. Programming Fundamentals & C Language (Continued)
36.Which of the following is NOT a valid storage class in C?
a) auto
b) extern
c) dynamic
d) static
Answer: c) dynamic
37.The volatile keyword in C is used to:
a) Optimize variable access
b) Indicate a variable may change unexpectedly
c) Allocate memory dynamically
d) Declare constants
Answer: b) Indicate a variable may change unexpectedly
38.What is the output of printf("%d", sizeof('A')); in C?
a) 1
b) 2
c) 4
d) Compiler-dependent
Answer: c) 4 (in C, characters are promoted to int)
39.Which operator is used to access a structure member via pointer?
a) .
b) ->
c) ::
d) *
Answer: b) ->
40.The #define directive is processed by the:
a) Compiler
b) Linker
c) Preprocessor
d) Interpreter
Answer: c) Preprocessor
8. Linked Lists (Advanced)
41.To reverse a singly linked list iteratively, you need:
a) 1 pointer
b) 2 pointers
c) 3 pointers
d) No pointers
Answer: c) 3 pointers (prev, current, next)
42.A circular linked list is primarily used for:
a) Random access
b) Implementing queues
c) Memory efficiency
d) Recursive algorithms
Answer: b) Implementing queues
43.Deleting a node in a singly linked list with only its pointer is:
a) O(1)
b) O(n)
c) Not possible
d) Requires a doubly linked list
Answer: c) Not possible (without previous node)
44.Which list uses a "dummy node" to simplify edge cases?
a) Singly linked
b) Doubly linked
c) Circular linked
d) Sentinel linked
Answer: d) Sentinel linked
45.The time complexity to find the middle node in a singly linked list is:
a) O(1)
b) O(log n)
c) O(n)
d) O(n²)
Answer: c) O(n) (using slow-fast pointers)
9. Trees (Advanced)
46.The worst-case height of an AVL tree with *n* nodes is:
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: b) O(log n)
47.In a B-tree of order *m*, each node has at most:
a) *m* children
b) *m-1* keys
c) Both a & b
d) *m+1* keys
Answer: c) Both a & b
48.A B+ tree differs from a B-tree in that:
a) All data is stored in leaves
b) Internal nodes store keys only
c) Supports range queries efficiently
d) All of the above
Answer: d) All of the above
49.Threaded binary trees are used to:
a) Replace pointers with threads
b) Enable inorder traversal without recursion/stack
c) Reduce memory usage
d) All of the above
Answer: d) All of the above
50.The inorder successor of a node in a BST is the:
a) Smallest node in the right subtree
b) Largest node in the left subtree
c) Parent node
d) Root node
Answer: a) Smallest node in the right subtree
10. Graphs (Advanced)
51.Kruskal’s algorithm uses which data structure?
a) Queue
b) Stack
c) Priority queue
d) Disjoint set (Union-Find)
Answer: d) Disjoint set
52.Topological sorting is applicable only to:
a) Undirected graphs
b) Directed acyclic graphs (DAGs)
c) Weighted graphs
d) Complete graphs
Answer: b) Directed acyclic graphs (DAGs)
53.The time complexity of Floyd-Warshall (all-pairs shortest path) is:
a) O(n)
b) O(n log n)
c) O(n²)
d) O(n³)
Answer: d) O(n³)
54.A graph with *n* vertices and *n-1* edges is a:
a) Complete graph
b) Tree
c) Cyclic graph
d) Disconnected graph
Answer: b) Tree
55.Bellman-Ford algorithm handles graphs with:
a) Only positive weights
b) Negative weights but no negative cycles
c) Negative cycles
d) Unweighted edges
Answer: b) Negative weights but no negative cycles
11. Sorting & Searching (Advanced)
56.Which sorting algorithm is stable?
a) QuickSort
b) HeapSort
c) MergeSort
d) SelectionSort
Answer: c) MergeSort
57.The best-case time complexity of QuickSort is:
a) O(n)
b) O(n log n)
c) O(n²)
d) O(log n)
Answer: b) O(n log n)
58.External sorting is used when data is too large to fit in:
a) CPU cache
b) RAM
c) Hard disk
d) Network
Answer: b) RAM
59.The worst-case time complexity of binary search is:
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: b) O(log n)
60.A hash function should minimize:
a) Collisions
b) Memory usage
c) Computation time
d) All of the above
Answer: d) All of the above
12. C Language (Advanced)
61.What is the output of printf("%d", 5 >> 1);?
a) 2
b) 3
c) 10
d) 20
Answer: a) 2 (right shift divides by 2)
62.The restrict keyword in C is used to:
a) Optimize pointer aliasing
b) Allocate memory
c) Declare constants
d) Define macros
Answer: a) Optimize pointer aliasing
63.Which is NOT a valid file opening mode in C?
a) "r+"
b) "wb"
c) "a+"
d) "xs"
Answer: d) "xs"
64.The typedef keyword is used to:
a) Define new data types
b) Allocate memory
c) Declare variables
d) Include headers
Answer: a) Define new data types
65.Which of the following is a dangling pointer?
a) Pointer to a freed memory location
b) Uninitialized pointer
c) NULL pointer
d) Pointer to a static variable
Answer: a) Pointer to a freed memory location
66.What is the output of printf("%d", sizeof("A")); in C?
a) 1
b) 2
c) 4
d) Compiler-dependent
Answer: b) 2
Explanation: The string literal "A" includes the null terminator (\0), making its
size 2 bytes.
67.The restrict keyword in C is used to:
a) Optimize pointer aliasing
b) Allocate memory
c) Declare constants
d) Define macros
Answer: a) Optimize pointer aliasing
Explanation: It tells the compiler that a pointer is the only reference to the
data, enabling optimizations.
68.Which is NOT a valid file opening mode in C?
a) "r+"
b) "wb"
c) "a+"
d) "xs"
Answer: d) "xs"
Explanation: Valid modes include "r", "w", "a", "r+", "w+", "a+", and their
binary variants ("b").
69.The typedef keyword is used to:
a) Define new data types
b) Allocate memory
c) Declare variables
d) Include headers
Answer: a) Define new data types
Explanation: Example: typedef int myInt; creates an alias for int.
70.What does #pragma once do?
a) Includes a header file once
b) Optimizes loops
c) Allocates memory
d) Defines inline functions
Answer: a) Includes a header file once
Explanation: Prevents multiple inclusions of the same header file.
13. Advanced Data Structures & Algorithms
71.In a B-tree of order 5, each node (except root) must have:
a) At least 2 keys
b) At least 3 keys
c) At most 4 keys
d) Both a and c
Answer: d) Both a and c
Explanation: Order *m* implies min keys = ⌈*m*/2⌉ - 1 (2), max keys = *m*-1
(4).
72.The worst-case time complexity to delete a node from an AVL tree is:
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: b) O(log n)
Explanation: AVL trees maintain balance through rotations, ensuring
logarithmic height.
73.Which graph algorithm uses a greedy approach?
a) Dijkstra’s
b) Floyd-Warshall
c) Bellman-Ford
d) BFS
Answer: a) Dijkstra’s
Explanation: It greedily selects the shortest path at each step.
74.A hash table with chaining resolves collisions using:
a) Linked lists
b) Rehashing
c) Open addressing
d) Binary trees
Answer: a) Linked lists
Explanation: Colliding elements are stored in a linked list at the same bucket.
75.The time complexity of HeapSort is:
a) O(n)
b) O(n log n)
c) O(n²)
d) O(log n)
Answer: b) O(n log n)
Explanation: Building the heap takes O(n), and each extraction takes O(log n).
14. C Programming (Memory & Pointers)
76.What is the output of printf("%d", sizeof(int(*)[5]));?
a) Size of an integer
b) Size of a pointer
c) Size of an array of 5 integers
d) Compiler-dependent
Answer: b) Size of a pointer
Explanation: int(*)[5] is a pointer to an array of 5 integers, so it prints the
pointer size (e.g., 8 bytes on 64-bit).
77.Which of the following is a memory leak?
a) Forgetting to free malloced memory
b) Accessing freed memory
c) Using uninitialized pointers
d) All of the above
Answer: a) Forgetting to free malloced memory
Explanation: Memory leaks occur when dynamically allocated memory is not
released.
78.The volatile keyword is used when a variable:
a) Is constant
b) May change unexpectedly (e.g., by hardware)
c) Is large in size
d) Is shared across threads
Answer: b) May change unexpectedly
Explanation: Prevents compiler optimizations that assume variable stability.
79.What is the output of printf("%p", NULL);?
a) 0
b) Garbage value
c) Segmentation fault
d) Depends on the system
Answer: a) 0
Explanation: NULL is typically defined as (void*)0.
80.Which is true about realloc?
a) It can extend or shrink memory blocks
b) It always moves the memory block
c) It initializes new memory to zero
d) It cannot fail
Answer: a) It can extend or shrink memory blocks
Explanation: realloc adjusts the size of the allocated memory dynamically.
15. Advanced Algorithms
81.The space complexity of MergeSort is:
a) O(1)
b) O(log n)
c) O(n)
d) O(n²)
Answer: c) O(n)
Explanation: Requires auxiliary space for merging subarrays.
82.Which sorting algorithm is in-place and stable?
a) QuickSort
b) MergeSort
c) BubbleSort
d) HeapSort
Answer: c) BubbleSort
Explanation: In-place (O(1) space) and stable (equal elements retain order).
83.RadixSort is most efficient when:
a) Keys are uniformly distributed
b) Keys are small integers
c) The dataset is small
d) The data is almost sorted
Answer: b) Keys are small integers
Explanation: Sorts digit-by-digit, ideal for fixed-size keys.
84.The worst-case time complexity of QuickSort occurs when the pivot is always
the:
a) Median
b) Smallest element
c) Largest element
d) Random element
Answer: b) Smallest element
Explanation: Leads to unbalanced partitions (O(n²) time).
85.Which data structure is used in Huffman coding?
a) Stack
b) Queue
c) Priority queue
d) Linked list
Answer: c) Priority queue
Explanation: Used to greedily select the least frequent nodes.
16. Graph Theory
86.Kruskal’s algorithm for MST uses:
a) BFS
b) Union-Find
c) DFS
d) Topological sort
Answer: b) Union-Find
Explanation: Efficiently checks for cycles while adding edges.
87.The all-pairs shortest path problem can be solved by:
a) Dijkstra’s (run for each node)
b) Floyd-Warshall
c) Both a and b
d) Prim’s algorithm
Answer: c) Both a and b
Explanation: Dijkstra’s (O(n² log n)) for sparse graphs, Floyd-Warshall (O(n³))
for dense graphs.
88.Topological sorting is possible only for:
a) Trees
b) Directed acyclic graphs (DAGs)
c) Undirected graphs
d) Complete graphs
Answer: b) Directed acyclic graphs (DAGs)
Explanation: Requires no cycles to define a linear order.
89.The time complexity of BFS on a graph with V vertices and E edges is:
a) O(V)
b) O(E)
c) O(V + E)
d) O(V log E)
Answer: c) O(V + E)
Explanation: Visits each node and edge once.
90.A graph with *n* vertices and *n*(*n*-1)/2 edges is a:
a) Tree
b) Complete graph
c) Bipartite graph
d) Disconnected graph
Answer: b) Complete graph
Explanation: Every pair of vertices is connected.
17. Final Mixed Questions
91.Which is true about static functions in C?
a) They can be accessed globally
b) They are visible only within their file
c) They retain state between calls
d) They cannot return values
Answer: b) They are visible only within their file
Explanation: Limits scope to the translation unit.
92.The strtok function in C is used to:
a) Concatenate strings
b) Compare strings
c) Tokenize strings
d) Copy strings
Answer: c) Tokenize strings
Explanation: Splits a string into tokens based on delimiters.
93.Which is NOT a valid way to pass an array to a function in C?
a) void func(int arr[])
b) void func(int *arr)
c) void func(int arr[10])
d) void func(int arr)
Answer: d) void func(int arr)
Explanation: This passes a single integer, not an array.
94.The exit() function is declared in which header?
a) <stdio.h>
b) <stdlib.h>
c) <math.h>
d) <string.h>
Answer: b) <stdlib.h>
Explanation: Also includes malloc, free, etc.
95.What is the output of printf("%d", ~0);?
a) 0
b) -1
c) 1
d) Implementation-defined
Answer: b) -1
Explanation: Bitwise NOT of 0 flips all bits, resulting in -1 in two’s complement.
96.Which is true about memcpy vs memmove?
a) memcpy is faster but undefined for overlapping regions
b) memmove is slower but handles overlaps
c) Both a and b
d) They are identical
Answer: c) Both a and b
Explanation: memmove checks for overlap and copies safely.
97.The const keyword in C:
a) Makes a variable constant
b) Prevents modification of a variable
c) Both a and b
d) Only applies to pointers
Answer: b) Prevents modification of a variable
Explanation: const int x = 5; means x cannot be modified.
98.Which is true about fprintf?
a) Writes to a file
b) Formats output like printf
c) Both a and b
d) Only works with stdout
Answer: c) Both a and b
Explanation: fprintf(file, format, ...) writes formatted text to a file.
99.The sizeof operator returns the size in:
a) Bits
b) Bytes
c) Words
d) Depends on the system
Answer: b) Bytes
Explanation: sizeof(int) typically returns 4 (bytes).
100. Which is true about #pragma pack(1)?
a) Disables struct padding
b) Aligns data to 1-byte boundaries
c) Both a and b
d) Only applies to arrays
Answer: c) Both a and b
Explanation: Forces tight packing of structs, reducing memory usage but
potentially slowing access.