100 MCQs on Data Structures and Algorithms (DSA) with Answers
Arrays and Strings (15 MCQs)
1. What is the time complexity to access an element in an array by index?
o a) O(1) ✔
o b) O(n)
o c) O(log n)
o d) O(n log n)
2. What will be the output of: "abc".substring(1,3) in Java?
o a) ab
o b) bc ✔
o c) ac
o d) abc
3. Which method is used to find the length of an array in Java?
o a) length()
o b) size()
o c) length ✔
o d) count()
4. Which data structure is used to implement strings in most programming languages?
o a) Array ✔
o b) Stack
o c) Queue
o d) Graph
5. What is the output of: "abc" + 1 + 2?
o a) abc3
o b) abc12 ✔
o c) 3abc
o d) 1abc2
6. How is a 2D array represented in memory?
o a) As an array of arrays ✔
o b) As a tree
o c) As a stack
o d) As a graph
7. Which of the following is not a valid string operation?
o a) Concatenation
o b) Slicing
o c) Traversing
o d) Insertion at middle ✔
8. Which of the following is immutable?
o a) String ✔
o b) ArrayList
o c) LinkedList
o d) Queue
9. How do you find duplicates in an array?
o a) HashMap ✔
BY: coding_error1
o b) Stack
o c) Queue
o d) Graph
10. Which is faster: array or linked list (for access by index)?
o a) Array ✔
o b) Linked List
o c) Both
o d) None
11. What is the default value of elements in an int array in Java?
o a) -1
o b) 1
o c) 0 ✔
o d) undefined
12. What is the index of first element in an array?
o a) 0 ✔
o b) 1
o c) -1
o d) Depends on language
13. Which method is used to reverse a string in Java?
o a) reverse()
o b) reverseString()
o c) StringBuilder.reverse() ✔
o d) strrev()
14. What data structure would you use for implementing a leaderboard?
o a) ArrayList
o b) Priority Queue ✔
o c) HashMap
o d) Stack
15. Which of the following is best for random access?
o a) Array ✔
o b) Linked List
o c) Stack
o d) Queue
Linked Lists (10 MCQs)
16. Time complexity to insert at head of a singly linked list?
• a) O(n)
• b) O(1) ✔
• c) O(log n)
• d) O(n log n)
17. In a doubly linked list, each node has:
• a) 1 pointer
BY: coding_error1
• b) 2 pointers ✔
• c) 3 pointers
• d) 0 pointers
18. Circular linked list is:
• a) Last node points to null
• b) Last node points to head ✔
• c) Head points to tail
• d) None
19. How to detect loop in linked list?
• a) DFS
• b) Two pointers (slow/fast) ✔
• c) BFS
• d) Sort
20. Which operation is easiest in singly linked list?
• a) Insertion at head ✔
• b) Deletion at tail
• c) Random access
• d) Sorting
21. What is the time complexity to delete a node at position n in a singly linked list?
• a) O(1)
• b) O(n) ✔
• c) O(n log n)
• d) O(log n)
22. In linked list, memory is:
• a) Contiguous
• b) Non-contiguous ✔
• c) Fixed
• d) None
23. Sentinel node is used:
• a) For recursion
• b) For circular list
• c) To simplify operations ✔
• d) To reduce memory
24. Which is better for memory efficiency?
• a) Array
BY: coding_error1
• b) Linked List ✔
• c) Queue
• d) Tree
25. Advantage of linked list over array?
• a) Fixed size
• b) Better cache
• c) Dynamic size ✔
• d) Faster indexing
Stacks and Queues (10 MCQs)
26. Which data structure uses LIFO order?
• a) Queue
• b) Stack ✔
• c) Array
• d) Linked List
27. Which of the following uses FIFO order?
• a) Stack
• b) Queue ✔
• c) Tree
• d) Graph
28. What is the time complexity of push operation in stack?
• a) O(1) ✔
• b) O(n)
• c) O(log n)
• d) O(n log n)
29. Which method is used to insert an element in a queue?
• a) pop()
• b) push()
• c) enqueue() ✔
• d) delete()
30. Which method removes element from front of queue?
• a) dequeue() ✔
• b) push()
• c) pop()
BY: coding_error1
• d) insert()
31. What data structure is used in recursion?
• a) Queue
• b) Stack ✔
• c) Tree
• d) Graph
32. Which data structure can be used to check balanced parentheses?
• a) Array
• b) Stack ✔
• c) Queue
• d) Tree
33. What happens when stack overflows?
• a) Program crashes ✔
• b) Data is lost
• c) Returns 0
• d) Starts again
34. What is the time complexity of dequeue operation in queue?
• a) O(1) ✔
• b) O(n)
• c) O(log n)
• d) O(n log n)
35. Which is used to implement stack?
• a) Array ✔
• b) Graph
• c) Tree
• d) HashMap
Trees (10 MCQs)
36. Which tree traversal is used for expression trees?
• a) Inorder
• b) Postorder ✔
• c) Preorder
• d) Level Order
37. What is the maximum number of nodes in a binary tree of height h?
BY: coding_error1
• a) 2^h
• b) 2^(h+1) - 1 ✔
• c) h^2
• d) h * log h
38. In a BST, what is the time complexity for search in average case?
• a) O(1)
• b) O(n)
• c) O(log n) ✔
• d) O(n log n)
39. Which traversal gives nodes in sorted order in BST?
• a) Preorder
• b) Inorder ✔
• c) Postorder
• d) Level Order
40. Which data structure is used in level order traversal?
• a) Stack
• b) Queue ✔
• c) Array
• d) Graph
41. What is a complete binary tree?
• a) Every level except last is full ✔
• b) All nodes have 2 children
• c) Every node has 1 child
• d) Tree with only leaf nodes
42. What is height of a tree with only root node?
• a) 0 ✔
• b) 1
• c) -1
• d) 2
43. AVL Tree is a type of:
• a) Binary Tree
• b) Balanced Binary Search Tree ✔
• c) Heap
• d) Graph
44. Which of these is not a tree traversal?
BY: coding_error1
• a) Inorder
• b) BFS ✔
• c) Preorder
• d) Postorder
45. What is the number of null links in a binary tree with n nodes?
• a) n
• b) n+1 ✔
• c) 2n
• d) 2n+1
Graphs (10 MCQs)
46. Which of the following is used in DFS traversal?
• a) Queue
• b) Stack ✔
• c) Priority Queue
• d) Hash Table
47. Which algorithm works only on Directed Acyclic Graphs (DAG)?
• a) Kruskal
• b) Dijkstra
• c) Topological Sort ✔
• d) Prim
48. Which traversal is used in shortest path (unweighted graph)?
• a) DFS
• b) BFS ✔
• c) Inorder
• d) Preorder
49. What is the time complexity of DFS in adjacency list?
• a) O(V+E) ✔
• b) O(V^2)
• c) O(E^2)
• d) O(log V)
50. Which algorithm is used to detect cycle in directed graph?
• a) DFS ✔
• b) BFS
BY: coding_error1
• c) Prim
• d) Dijkstra
51. What is a strongly connected graph?
• a) All nodes connected in one direction
• b) There is a path from every node to every other node ✔
• c) All nodes have even degree
• d) None
52. Which algorithm is used in Minimum Spanning Tree?
• a) DFS
• b) Prim’s ✔
• c) BFS
• d) Bellman-Ford
53. Which graph representation is most space efficient for sparse graphs?
• a) Adjacency Matrix
• b) Adjacency List ✔
• c) Incidence Matrix
• d) Edge List
54. In a complete graph with n vertices, how many edges exist?
• a) n
• b) n^2
• c) n(n-1)/2 ✔
• d) n(n+1)/2
55. Which algorithm is used for shortest path with negative weights?
• a) Dijkstra
• b) Bellman-Ford ✔
• c) Prim
• d) Kruskal
Searching & Sorting (10 MCQs)
56. What is the time complexity of binary search?
• a) O(n)
• b) O(log n) ✔
• c) O(1)
• d) O(n log n)
BY: coding_error1
57. Merge Sort follows which algorithmic paradigm?
• a) Greedy
• b) Divide and Conquer ✔
• c) Dynamic Programming
• d) Backtracking
58. Which sorting algorithm is best for small datasets?
• a) Merge Sort
• b) Quick Sort
• c) Insertion Sort ✔
• d) Radix Sort
59. Which sorting algorithm is not comparison based?
• a) Merge Sort
• b) Radix Sort ✔
• c) Heap Sort
• d) Quick Sort
60. Quick Sort’s worst-case time complexity is:
• a) O(n log n)
• b) O(log n)
• c) O(n^2) ✔
• d) O(n)
61. Heap Sort uses which data structure?
• a) Stack
• b) Heap ✔
• c) Queue
• d) Tree
62. What is the best case of Bubble Sort?
• a) O(n) ✔
• b) O(n log n)
• c) O(n^2)
• d) O(log n)
63. Which of the following is stable sort?
• a) Quick Sort
• b) Heap Sort
• c) Merge Sort ✔
• d) Selection Sort
BY: coding_error1
64. What is the average case of Quick Sort?
• a) O(n^2)
• b) O(n log n) ✔
• c) O(n)
• d) O(log n)
65. Which sorting algorithm is best for nearly sorted array?
• a) Merge Sort
• b) Quick Sort
• c) Insertion Sort ✔
• d) Selection Sort
Recursion & Backtracking (10 MCQs)
66. Which of the following problems is solved using backtracking?
• a) Tower of Hanoi
• b) N-Queens ✔
• c) Fibonacci
• d) Binary Search
67. What is the base case in recursion?
• a) The case with the smallest input ✔
• b) The infinite loop
• c) The recursive call
• d) None
68. Which data structure is used in recursion?
• a) Queue
• b) Stack ✔
• c) Linked List
• d) Heap
69. Which is NOT a characteristic of recursion?
• a) Base case
• b) Recursive call
• c) Iterative loop ✔
• d) Stack usage
70. Backtracking algorithm is:
• a) Greedy
• b) Brute-force with optimization ✔
• c) Divide and Conquer
BY: coding_error1
• d) Dynamic Programming
71. What is the time complexity of recursive Fibonacci?
• a) O(1)
• b) O(log n)
• c) O(n)
• d) O(2^n) ✔
72. Which problem is solved using recursion naturally?
• a) Tree Traversal ✔
• b) Searching
• c) Sorting
• d) Hashing
73. What happens if base case is missing in recursion?
• a) Returns null
• b) Infinite recursion ✔
• c) Compilation error
• d) Stack is unused
74. In recursion, every call is stored in:
• a) Heap
• b) Stack ✔
• c) Queue
• d) Array
75. The function calls itself in:
• a) Iteration
• b) Recursion ✔
• c) Inheritance
• d) Encapsulation
Dynamic Programming (10 MCQs)
76. What is dynamic programming?
• a) Divide & Conquer
• b) Solving problems by combining solutions to subproblems ✔
• c) Sorting method
• d) Recursion without memoization
77. Which of these is a classic DP problem?
BY: coding_error1
• a) Prim’s Algorithm
• b) Longest Common Subsequence ✔
• c) Binary Search
• d) DFS
78. In DP, overlapping subproblems mean:
• a) Subproblems are unrelated
• b) Same subproblems are solved multiple times ✔
• c) Problem can't be solved
• d) Using recursion
79. What is memoization?
• a) Looping
• b) Storing results of expensive function calls ✔
• c) Searching algorithm
• d) Sorting algorithm
80. Which method is bottom-up DP?
• a) Recursion
• b) Tabulation ✔
• c) Memoization
• d) Brute force
81. Knapsack problem is solved by:
• a) Greedy
• b) DP ✔
• c) BFS
• d) DFS
82. What is the space complexity of bottom-up DP?
• a) Always O(1)
• b) Depends on problem ✔
• c) O(log n)
• d) O(1)
83. What is optimal substructure?
• a) Problem can be broken into optimal subproblems ✔
• b) Problem cannot be divided
• c) Only works in sorting
• d) Related to hash table
84. Which of these uses DP?
BY: coding_error1
• a) N-Queens
• b) Matrix Chain Multiplication ✔
• c) Kruskal
• d) Prim’s
85. Top-down approach is also called:
• a) Tabulation
• b) Memoization ✔
• c) Brute Force
• d) DFS
Greedy Algorithms (5 MCQs)
86. Greedy algorithms always:
• a) Choose local optimum ✔
• b) Choose global optimum
• c) Use recursion
• d) None
87. Greedy strategy fails in:
• a) Fractional Knapsack
• b) 0-1 Knapsack ✔
• c) Activity Selection
• d) Huffman Encoding
88. Huffman coding is based on:
• a) Sorting
• b) Greedy ✔
• c) Backtracking
• d) DFS
89. Which of the following uses greedy?
• a) Dijkstra ✔
• b) Bellman-Ford
• c) Binary Search
• d) Merge Sort
90. Activity Selection Problem uses:
• a) Dynamic Programming
• b) Greedy Approach ✔
BY: coding_error1
• c) Divide & Conquer
• d) Brute Force
Complexity & Big-O (10 MCQs)
91. What is the time complexity of linear search?
• a) O(1)
• b) O(n) ✔
• c) O(log n)
• d) O(n^2)
92. Best case time complexity of binary search?
• a) O(log n)
• b) O(n)
• c) O(1) ✔
• d) O(n log n)
93. Time complexity of accessing array element by index?
• a) O(log n)
• b) O(1) ✔
• c) O(n)
• d) O(n log n)
94. What does Big O represent?
• a) Best case
• b) Average case
• c) Worst case ✔
• d) No case
95. Which of the following grows fastest?
• a) O(1)
• b) O(n)
• c) O(n log n)
• d) O(n!) ✔
96. What is the space complexity of recursive factorial?
• a) O(n) ✔
• b) O(log n)
• c) O(1)
• d) O(n^2)
BY: coding_error1
97. Which is slower in growth rate?
• a) O(n)
• b) O(n log n) ✔
• c) O(n^2)
• d) O(2^n)
98. What is the time complexity of inserting in a hash table (average case)?
• a) O(log n)
• b) O(n)
• c) O(1) ✔
• d) O(n^2)
99. What is the time complexity of traversing a linked list?
• a) O(1)
• b) O(log n)
• c) O(n) ✔
• d) O(n^2)
100. Time complexity of enqueue operation in queue?
• a) O(1) ✔
• b) O(log n)
• c) O(n)
• d) O(n log n)
BY: coding_error1