
Data Structures and Algorithms, or DSA if you’re in the know, are pretty much the bread and butter of tech interviews—especially if you’re just starting out and dreaming of those top-tier companies. Nailing DSA isn’t just about memorizing stuff; it genuinely boosts your problem-solving mojo and shows you can whip up code that’s fast and can actually handle real-world messes.
So, here’s the lowdown: this article rounds up the top 100 DSA interview questions you’ll probably run into in 2025, all sorted by category—arrays, strings, linked lists, stacks, queues, trees, graphs, sorting, searching, and, yep, dynamic programming too. Each question is beginner-friendly, with quick explanations and the occasional code snippet to make things click. Whether you’re gearing up for college placements or eyeing an entry-level dev job, these questions are your ticket to not freezing up when the whiteboard comes out.
Arrays
Arrays are fundamental data structures that store elements in contiguous memory locations, often tested for their simplicity and versatility.
1. What is an array?
A collection of elements of the same type stored at contiguous memory locations, accessed via indices.
2. How do you find the largest element in an unsorted array?
Iterate through the array, tracking the maximum value. Time complexity: O(n).
3. How do you find the smallest element in an unsorted array?
Similar to finding the largest, track the minimum value while iterating. Time complexity: O(n).
4. How do you find the second largest element in an array?
Track the largest and second largest while iterating, updating them as needed. Time complexity: O(n).
5. How do you find a missing number in an array of 1 to n?
Calculate the expected sum of numbers from 1 to n, subtract the actual sum of the array. Time complexity: O(n).
6. How do you find duplicate numbers in an array?
Use a hash set to track seen numbers or sort the array to find adjacent duplicates. Time complexity: O(n) with hash set, O(n log n) with sorting.
7. How do you remove duplicates from an array in place?
Use two pointers or a hash set to keep unique elements in the array. Time complexity: O(n).
8. How do you reverse an array in place?
Swap elements from the start and end, moving inward. Time complexity: O(n/2).
def reverse_array(arr):
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
9. How do you find all pairs in an array that sum to a given number?
Use a hash set to store complements while iterating. Time complexity: O(n).
10. How do you rotate an array by k positions?
Reverse the entire array, then reverse the first k and the rest separately. Time complexity: O(n).
11. How do you find the maximum subarray sum (Kadane’s Algorithm)?
Track the current sum and maximum sum while iterating, resetting the current sum if negative. Time complexity: O(n).
12. How do you find the intersection of two arrays?
Use a hash set to store elements of one array and check for matches in the other. Time complexity: O(n).
13. How do you move all zeros to the end of an array?
Use two pointers to place non-zero elements at the front, then fill the rest with zeros. Time complexity: O(n).
14. How do you check if an array is sorted?
Compare adjacent elements to ensure they are in order. Time complexity: O(n).
15. How do you find the kth largest element in an array?
Use a min-heap of size k or sort the array and access the kth element. Time complexity: O(n log k) with heap, O(n log n) with sorting.
Strings
Strings are arrays of characters, frequently tested for manipulation and pattern-matching problems.
16. How do you reverse a string?
Convert to a char array and swap characters from ends. Time complexity: O(n).
17. How do you check if two strings are anagrams?
Sort both strings and compare, or use a frequency array/hash map. Time complexity: O(n log n) with sorting, O(n) with hash map.
18. How do you find the first non-repeating character in a string?
Use a hash map to count character frequencies, then find the first with count 1. Time complexity: O(n).
19. How do you check if a string is a palindrome?
Compare characters from both ends moving inward. Time complexity: O(n).
20. How do you find the length of the longest substring without repeating characters?
Use a sliding window with a hash set to track unique characters. Time complexity: O(n).
21. How do you remove duplicate characters from a string?
Use a hash set to track seen characters while building a new string. Time complexity: O(n).
22. How do you convert a string to an integer (atoi)?
Parse the string, handling signs, digits, and overflow. Time complexity: O(n).
23. How do you find all permutations of a string?
Use recursion or backtracking to generate all possible arrangements. Time complexity: O(n!).
24. How do you check if a string is a valid parenthesis sequence?
Use a stack to match opening and closing parentheses. Time complexity: O(n).
25. How do you find the longest palindromic substring?
Use dynamic programming or expand around the center for each character. Time complexity: O(n²).
Linked Lists
Linked lists are linear data structures with nodes connected by pointers, tested for traversal and manipulation.
26. What is a linked list?
A sequence of nodes where each node contains data and a pointer to the next node.
27. How do you reverse a linked list?
Iterate and update the next pointers to reverse the direction. Time complexity: O(n).
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head):
prev, curr = None, head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
return prev
28. How do you detect a cycle in a linked list?
Use Floyd’s Tortoise and Hare algorithm with two pointers. Time complexity: O(n).
29. How do you find the middle node of a linked list?
Use two pointers (slow and fast) where fast moves twice as fast. Time complexity: O(n).
30. How do you merge two sorted linked lists?
Compare nodes and build a new list by selecting the smaller value. Time complexity: O(n).
31. How do you find the intersection point of two linked lists?
Traverse both lists to equalize lengths, then find the common node. Time complexity: O(n).
32. How do you remove the nth node from the end of a linked list?
Use two pointers with a gap of n nodes, then adjust links. Time complexity: O(n).
33. How do you check if a linked list is a palindrome?
Find the middle, reverse the second half, and compare. Time complexity: O(n).
34. How do you delete a node with only a pointer to that node?
Copy the next node’s data and update the pointer to skip it. Time complexity: O(1).
35. How do you implement a doubly linked list?
Each node has pointers to both the next and previous nodes.
Stacks
Stacks follow the Last In, First Out (LIFO) principle, used for problems like expression evaluation.
36. What is a stack?
A data structure where elements are added and removed from the top (LIFO).
37. How do you implement a stack using an array?
Use an array with a top pointer to push and pop elements. Time complexity: O(1) for push/pop.
38. How do you implement a stack using a linked list?
Use a linked list where push/pop operations occur at the head. Time complexity: O(1).
39. How do you check for balanced parentheses using a stack?
Push opening brackets, pop when matching closing brackets. Time complexity: O(n).
40. How do you evaluate a postfix expression?
Use a stack to process operands and operators. Time complexity: O(n).
41. How do you find the next greater element in an array?
Use a stack to track elements and find the next larger value. Time complexity: O(n).
42. How do you implement two stacks in one array?
Divide the array or use two pointers from opposite ends. Time complexity: O(1) for push/pop.
43. How do you reverse a stack without extra space?
Use recursion to pop and reinsert elements at the bottom. Time complexity: O(n²).
44. How do you find the minimum element in a stack in O(1) time?
Maintain a second stack to track minimums. Time complexity: O(1) for push/pop/min.
45. How do you implement a stack using two queues?
Use one queue for storage, simulating LIFO by rotating elements. Time complexity: O(n) for push.
Queues
Queues follow the First In, First Out (FIFO) principle, used in scheduling and BFS.
46. What is a queue?
A data structure where elements are added at the rear and removed from the front (FIFO).
47. How do you implement a queue using an array?
Use front and rear pointers to enqueue and dequeue. Time complexity: O(1).
48. How do you implement a queue using a linked list?
Use pointers for front and rear nodes. Time complexity: O(1).
49. How do you implement a circular queue?
Use a fixed-size array with modulo arithmetic for wrap-around. Time complexity: O(1).
50. How do you implement a priority queue?
Use a heap to prioritize elements based on a key. Time complexity: O(log n) for insert/remove.
51. How do you implement a queue using two stacks?
Use one stack for enqueue, another for dequeue with transfer. Time complexity: O(n) for dequeue.
52. How do you reverse a queue?
Use a stack to reverse the order of elements. Time complexity: O(n).
53. What is a dequeue (double-ended queue)?
A queue allowing insertion and deletion at both ends.
54. How do you implement a sliding window maximum using a deque?
Use a deque to store indices of potential maximums. Time complexity: O(n).
55. How do you generate binary numbers from 1 to n using a queue?
Enqueue “1”, then generate next numbers by appending 0 and 1. Time complexity: O(n).
Trees
Trees are hierarchical data structures, widely tested for traversal and manipulation.
56. What is a binary tree?
A tree where each node has at most two children (left and right).
57. How do you perform an inorder traversal of a binary tree?
Visit left subtree, root, then right subtree. Time complexity: O(n).
58. How do you perform a preorder traversal of a binary tree?
Visit root, left subtree, then right subtree. Time complexity: O(n).
59. How do you perform a postorder traversal of a binary tree?
Visit left subtree, right subtree, then root. Time complexity: O(n).
60. How do you find the height of a binary tree?
Recursively compute the maximum height of left and right subtrees. Time complexity: O(n).
61. How do you check if a binary tree is balanced?
Ensure the height difference between left and right subtrees is at most 1. Time complexity: O(n).
62. How do you find the lowest common ancestor in a binary tree?
Recursively traverse until both nodes are found in different subtrees. Time complexity: O(n).
63. How do you check if a binary tree is a binary search tree (BST)?
Ensure each node’s value lies within a valid range. Time complexity: O(n).
64. How do you find the diameter of a binary tree?
Compute the maximum path through left and right subtrees. Time complexity: O(n).
65. How do you implement a level-order traversal (BFS) of a binary tree?
Use a queue to process nodes level by level. Time complexity: O(n).
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def level_order(root):
if not root:
return []
result, queue = [], deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(level)
return result
Graphs
Graphs are non-linear data structures with nodes and edges, tested for traversal and pathfinding.
66. What is a graph?
A set of nodes (vertices) connected by edges, representing relationships.
67. How do you implement a depth-first search (DFS) in a graph?
Use recursion or a stack to explore as far as possible along each branch. Time complexity: O(V + E).
68. How do you implement a breadth-first search (BFS) in a graph?
Use a queue to explore nodes level by level. Time complexity: O(V + E).
69. How do you detect a cycle in a directed graph?
Use DFS with a recursion stack to detect back edges. Time complexity: O(V + E).
70. How do you find the shortest path in an unweighted graph?
Use BFS to explore nodes layer by layer. Time complexity: O(V + E).
71. How do you implement Dijkstra’s algorithm for shortest paths?
Use a priority queue to select the node with the minimum distance. Time complexity: O((V + E) log V).
72. How do you perform a topological sort on a DAG?
Use DFS or Kahn’s algorithm with in-degree tracking. Time complexity: O(V + E).
73. How do you find connected components in an undirected graph?
Use DFS or BFS to group nodes in the same component. Time complexity: O(V + E).
74. How do you implement a minimum spanning tree (Kruskal’s or Prim’s)?
Kruskal’s uses a union-find; Prim’s uses a priority queue. Time complexity: O(E log E) or O((V + E) log V).
75. How do you check if a graph is bipartite?
Use BFS/DFS to color nodes and ensure no adjacent nodes have the same color. Time complexity: O(V + E).
Sorting
Sorting algorithms arrange elements in a specific order, tested for efficiency and implementation.
76. What is bubble sort?
Repeatedly swap adjacent elements if they are in the wrong order. Time complexity: O(n²).
77. How does selection sort work?
Select the smallest element and place it at the beginning, repeat for the rest. Time complexity: O(n²).
78. How does insertion sort work?
Build a sorted portion by inserting elements in the correct position. Time complexity: O(n²).
79. How does merge sort work?
Divide the array into halves, sort recursively, and merge. Time complexity: O(n log n).
80. How does quicksort work?
Choose a pivot, partition the array, and recursively sort subarrays. Time complexity: O(n log n) average.
def quicksort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quicksort(arr, low, pi - 1)
quicksort(arr, pi + 1, high)
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
81. What is heap sort?
Build a max-heap, repeatedly extract the maximum element and place it at the end. Time complexity: O(n log n).
82. How does counting sort work?
Use a count array to store frequencies of elements, then reconstruct the sorted array. Time complexity: O(n + k).
83. How does radix sort work?
Sort digits from least significant to most significant using counting sort. Time complexity: O(d(n + k)).
84. What is the difference between stable and unstable sorting?
Stable sorting preserves the relative order of equal elements; unstable sorting does not.
85. How do you sort an array with elements in a given range?
Use counting sort or bucket sort for efficiency. Time complexity: O(n + k).
Searching
Searching algorithms locate elements in a data structure, tested for speed and applicability.
86. How does linear search work?
Iterate through the array until the target is found. Time complexity: O(n).
87. How does binary search work?
Divide the sorted array in half repeatedly to find the target. Time complexity: O(log n).
88. How do you implement a binary search iteratively?
Use a loop to update low and high pointers until the target is found. Time complexity: O(log n).
89. How do you search in a rotated sorted array?
Use modified binary search to handle the rotation point. Time complexity: O(log n).
90. How do you find the first and last occurrence of an element in a sorted array?
Use binary search to find the leftmost and rightmost positions. Time complexity: O(log n).
Dynamic Programming
Dynamic programming solves problems by breaking them into overlapping subproblems, tested for optimization.
91. What is dynamic programming?
A method to solve problems by storing solutions to subproblems to avoid recomputation.
92. How do you solve the Fibonacci sequence using dynamic programming?
Use a table to store Fibonacci numbers iteratively. Time complexity: O(n).
93. How do you solve the 0/1 knapsack problem?
Use a 2D table to track maximum value for each weight and item. Time complexity: O(nW).
94. How do you find the longest common subsequence of two strings?
Use a 2D table to compare characters and build the sequence. Time complexity: O(mn).
95. How do you solve the coin change problem?
Use a table to track minimum coins needed for each amount. Time complexity: O(n * amount).
96. How do you solve the longest increasing subsequence problem?
Use a table to track the length of increasing subsequences. Time complexity: O(n²).
97. How do you solve the matrix chain multiplication problem?
Use a 2D table to find the minimum cost of multiplying matrices. Time complexity: O(n³).
98. How do you solve the edit distance problem?
Use a 2D table to compute minimum operations to transform one string to another. Time complexity: O(mn).
99. How do you solve the subset sum problem?
Use a 2D table to check if a subset sums to the target. Time complexity: O(n * sum).
100. How do you solve the rod cutting problem?
Use a table to find the maximum revenue from cutting a rod. Time complexity: O(n²).
Download PDF Link
You can find the detailed code implementation and solutions of all the questions above in the Pdf via GDrive link below, by the way solutions are in Python coding language but I believe anyone can easily understand the crux of the approach and implement the solution in their desired language.