800+ Data-Structure & Algorithm Interview Questions
800+ Data-Structure & Algorithm Interview Questions
Practice Problems:
Array
1. Find pair with given sum in the array
2. Check if subarray with 0 sum is exists or not
3. Print all sub-arrays with 0 sum
4. Sort binary array in linear time
5. Find a duplicate element in a limited range array
6. Find maximum length sub-array having given sum
7. Find maximum length sub-array having equal number of 0’s and 1’s
8. Find maximum product of two integers in an array
9. Sort an array containing 0’s, 1’s and 2’s (Dutch National Flag Problem)
10. In place merge two sorted arrays
11. Merge two arrays by satisfying given constraints
12. Find index of 0 to replace to get maximum length sequence of continuous ones
13. Shuffle a given array of elements (Fisher–Yates shuffle)
14. Rearrange the array with alternate high and low elements
15. Find equilibrium index of an array
16. Find largest sub-array formed by consecutive integers
17. Find majority element (Boyer–Moore Majority Vote Algorithm)
18. Move all zeros present in the array to the end
19. Replace each element of array with product of every other element without using /
operator
20. Find Longest Bitonic Subarray in an array
21. Longest Increasing Subsequence
22. Find maximum difference between two elements in the array by satisfying given
constraints
23. Maximum Sum Subarray Problem (Kadane’s Algorithm)
24. Print continuous subarray with maximum sum
25. Maximum Sum Circular Subarray
26. Find all distinct combinations of given length — I
27. Find all distinct combinations of given length with repetition allowed
28. Find maximum sequence of continuous 1’s formed by replacing at-most k zeroes by ones
29. Find minimum sum subarray of given size k
30. Find maximum product subarray in a given array
31. Find subarray having given sum in given array of integers
32. Find the length of smallest subarray whose sum of elements is greater than the given
number
33. Find largest number possible from set of given numbers
34. Find the smallest window in array sorting which will make the entire array sorted
35. Find maximum sum path involving elements of given arrays
36. Maximum profit earned by buying and selling shares any number of times
37. Trapping Rain Water within given set of bars
38. Find minimum platforms needed in the station so to avoid any delay in arrival of any train
39. Decode the array constructed from another array
40. Sort an array using one swap
41. Find Triplet with given sum in an array
42. Length of longest continuous sequence with same sum in given binary arrays
43. Reverse every consecutive m elements of the given subarray
44. Maximum Product Subset Problem
45. Find pairs with given difference k in the array
46. Find pairs with given difference k in the array | Constant space solution
47. 4 sum problem | Quadruplets with given sum
48. Print all quadruplets with given sum | 4-sum problem extended
49. Quickselect Algorithm
50. Rearrange array such that A[A[i]] is set to i for every element A[i]
51. Print all Triplets that forms Arithmetic Progression
52. Print all Triplets that forms Geometric Progression
53. Print all combination of numbers from 1 to n having sum n
54. Replace each element of the array by its corresponding rank in the array
55. Print all Triplets in an array with sum less than or equal to given number
56. Group elements of an array based on their first occurrence
57. Find minimum difference between index of two given elements present in the array
58. Find maximum absolute difference between sum of two non-overlapping sub-arrays
59. Find all Symmetric Pairs in an Array of Pairs
60. Partition an array into two sub-arrays with the same sum
61. Find count of distinct elements in every sub-array of size k
62. Find two numbers with maximum sum formed by array digits
63. Print all sub-arrays of an array having distinct elements
64. Find a Triplet having Maximum Product in an Array
65. Find Minimum Index of Repeating Element in an Array
66. Generate random input from an array according to given probabilities
67. Find pair in an array having minimum absolute sum
68. Find Index of Maximum Occurring Element with Equal Probability
69. Check if an Array is Formed by Consecutive Integers
70. Find two non-overlapping pairs having same sum in an array
71. Add elements of two arrays into a new array
72. Find Minimum Product among all Combinations of Triplets in an Array
73. Replace every element of an array with the least greater element on its right
74. Find all odd occurring elements in an array having limited range of elements
75. Count the distinct absolute values in the sorted array
76. Print all combinations of positive integers in increasing order that sum to a given number
77. Find all distinct combinations of given length — II
78. Find subarrays with given sum in an array
79. Find the surpasser count for each element of an array
80. Find maximum length sequence of continuous ones (Using Sliding Window)
81. Find maximum length sequence of continuous ones
82. Find index that divides an array into two non-empty subarrays of equal sum
83. Calculate frequency of all elements present in an array of specified range
84. Rearrange the array such that it contains positive and negative numbers at alternate
positions
85. Find a sorted triplet in the given array
86. Shuffle an array according to the given order of elements
87. Count number of strictly increasing sub-arrays in an array
88. Find duplicates within given range k in an array
89. Longest Alternating Subarray Problem
90. Find minimum range with at-least one element from each of the given arrays
91. Find longest subsequence formed by consecutive integers
92. Find all elements in an array that are greater than all elements present to their right
93. Find missing number in array without using extra space
94. Determine index of an element in given array which satisfies given constraints
95. Find minimum moves required for converting a given array to an array of zeroes
96. Left rotate an array
97. Right rotate an array k times
98. Find maximum profit earned from at most two stock transactions
99. Find Frequency of each element in a sorted array containing duplicates
100. Find Minimum and Maximum element in an array using minimum comparisons
101. Difference between Subarray, Subsequence and Subset
102. Find odd occurring element in an array in single traversal
103. Find odd occurring element in logarithmic time
104. Find two odd occurring elements in an array without using any extra space
105. Check if given array represents min heap or not
106. Find K’th smallest element in an array
107. Find K’th largest element in an array
108. Sort a K-Sorted Array
109. Merge M sorted lists of variable length
110. Find smallest range with at-least one element from each of the given lists
111. Merge M sorted lists each containing N elements
112. Find maximum sum of subsequence with no adjacent elements
113. Find ways to calculate a target from elements of specified array
114. Sort elements by their frequency and Index
115. Sort an array based on order defined by another array
116. Inversion Count of an array
117. Segregate positive and negative integers in linear time
118. Find number of rotations in a circularly sorted array
119. Search an element in a circular sorted array
120. Find first or last occurrence of a given number in a sorted array
121. Count occurrences of a number in a sorted array with duplicates
122. Find smallest missing element from a sorted array
123. Find Floor and Ceil of a number in a sorted array
124. Search in a nearly sorted array in logarithmic time
125. Find number of 1’s in a sorted binary array
126. Find Missing Term in a Sequence in Logarithmic time
127. Find missing number and duplicate elements in an array
128. Find the peak element in an array
129. Find Floor and Ceil of a number in a sorted array (Recursive solution)
130. Print all distinct subsets of a given set
131. Find two duplicate elements in a limited range array (using XOR)
132. Combinations of words formed by replacing given numbers with corresponding
alphabets
133. 0–1 Knapsack Problem
134. Subset sum Problem
135. Partition Problem
136. 3-Partition Problem
137. 3-partition problem extended | Print all partitions
138. K-Partition Problem | Printing all Partitions
139. Minimum Sum Partition Problem
140. Rod Cutting
141. Longest Alternating Subsequence Problem
142. Coin change-making problem (unlimited supply of coins)
143. Coin Change Problem — Find total number of ways to get the denomination of
coins
144. Find maximum profit earned from at most K stock transactions
String
Backtracking
Binary Tree
BST
1. Insertion in BST
2. Search given key in BST
3. Deletion from BST
4. Construct balanced BST from given keys
5. Determine if given Binary Tree is a BST or not
6. Check if given keys represents same BSTs or not without building the BST
7. Find inorder predecessor for given key in a BST
8. Find Lowest Common Ancestor (LCA) of two nodes in a Binary Search Tree
9. Find K’th smallest and K’th largest element in BST
10. Floor and Ceil in a Binary Search Tree
11. Find optimal cost to construct binary search tree
12. Convert a Binary Tree to BST by maintaining its original structure
13. Remove nodes from BST that have keys outside the valid range
14. Find a pair with given sum in a BST
15. Find inorder successor for given key in a BST
16. Replace every element of an array with the least greater element on its right
17. Fix a binary tree that is only one swap away from becoming a BST
18. Update every key in BST to contain sum of all greater keys
19. Check if a given sequence represents preorder traversal of a BST
20. Build a Binary Search Tree from a Postorder Sequence
21. Build a Binary Search Tree from a Preorder Sequence
22. Find a triplet with given sum in a BST
23. Count subtrees in a BST whose nodes lies within a given range
24. Merge two BSTs into a doubly linked list in sorted order
25. Construct a height-balanced BST from an unbalanced BST
26. Find the size of the largest BST in a Binary Tree
27. Convert a Binary Search Tree into a Min Heap
28. Construct a Height-Balanced BST from a Sorted Doubly Linked List
Dynamic Programming
Graph
Greedy
1. Activity Selection Problem
2. Huffman Coding
3. Job Sequencing Problem with Deadlines
4. Graph Coloring Problem
5. Kruskal’s Algorithm for finding Minimum Spanning Tree
6. Single-Source Shortest Paths — Dijkstra’s Algorithm
7. Shortest Superstring Problem
Heap
1. Introduction to Priority Queues using Binary Heaps
2. Min Heap and Max Heap Implementation — C++, Java
3. Heap Sort Algorithm
4. Check if given array represents min heap or not
5. Convert Max Heap to Min Heap in linear time
6. Find K’th largest element in an array
7. Sort a K-Sorted Array
8. Merge M sorted lists of variable length
9. Merge K sorted linked lists
10. Find K’th smallest element in an array
11. Find smallest range with at-least one element from each of the given lists
12. Merge M sorted lists each containing N elements
13. Find first k non-repeating characters in a string in single traversal
14. Find first k maximum occurring words in given set of strings
15. Implementation of Treap Data Structure (Insert, Search and Delete)
16. Convert a Binary Search Tree into a Min Heap
17. Check if a binary tree is a min-heap or not
18. Huffman Coding
19. External Merge Sort Algorithm
Linked List
1. Introduction to Linked Lists
2. Linked List Implementation — C, C++, Java, Python
3. Linked List | Insertion at Tail
4. Static Linked List
5. Clone given Linked List
6. Delete Linked List
7. Pop operation in linked list
8. Insert given node into the correct sorted position in the given sorted linked list
9. Rearrange linked list in increasing order (Sort linked list)
10. Split the nodes of the given linked list into front and back halves
11. Remove duplicates from a sorted linked list
12. Move front node of the given list to the front of the another list
13. Move even nodes to the end of the list in reverse order
14. Split given linked list into two lists where each list containing alternating elements from it
15. Construct a linked list by merging alternate nodes of two given lists
16. Merge Sort Algorithm for Singly Linked List
17. Merge two sorted linked lists into one
18. Merge K sorted linked lists
19. Intersection of two given sorted linked lists
20. Reverse Linked List (Iterative Solution)
21. Reverse Linked List (Recursive Solution)
22. Reverse every group of k nodes in given linked list
23. Find K’th node from the end in a linked list
24. Merge alternate nodes of two linked lists into the first list
25. Merge two sorted linked lists from their end
26. Delete every N nodes in a linked list after skipping M nodes
27. Rearrange linked list in specific manner in linear time
28. Check if linked list is palindrome or not
29. Move last node to front in a given Linked List
30. Rearrange the linked list in specific manner
31. Detect Cycle in a linked list (Floyd’s Cycle Detection Algorithm)
32. Sort linked list containing 0’s, 1’s and 2’s
33. Implement Stack using Linked List
34. Implement Queue using Linked List
35. Remove duplicates from a linked list
36. Rearrange the linked list so that it has alternating high, low values
37. Rearrange a Linked List by Separating Odd Nodes from the Even Ones
38. Calculate height of a binary tree with leaf nodes forming a circular doubly linked list
39. XOR Linked List: Overview and Implementation
40. Convert a multilevel linked list to a singly linked list
41. Recursively check if linked list of characters is palindrome or not
42. Merge two BSTs into a doubly linked list in sorted order
43. Remove redundant nodes from a path formed by a linked list
44. Add a single-digit number to a linked list representing a number
45. Reverse every alternate group of k nodes in a linked list
46. Determine if a given linked list is a palindrome or not
47. Sort a Doubly Linked List using Merge Sort
48. Reverse a Doubly Linked List
49. Pairwise swap adjacent nodes of a linked list
50. Flatten a linked list
51. Check if a Linked List of String is Palindromic
52. Flatten a multilevel linked list
53. Construct a height-balanced BST from an unbalanced BST
54. Swap K’th node from beginning with K’th node from end in a Linked List
55. Add two linked lists without using any extra space
56. Clone a Linked List with Random Pointers
57. Update random pointer for each linked list node to point to the maximum node
58. Link nodes present in each level of a binary tree in the form of a linked list
59. Convert a Ternary Tree to a Doubly Linked List
60. Print nodes of a given binary tree in vertical order
61. Convert a Binary Tree into a Doubly Linked List in Spiral Order
62. Construct a Height-Balanced BST from a Sorted Doubly Linked List
63. In-place merge two sorted linked lists without modifying links of the first list
64. Reverse specified portion of a Linked List
Matrix
1. Print Matrix in Spiral Order
2. Create Spiral Matrix from given array
3. Shift all matrix elements by 1 in Spiral Order
4. Find Shortest path from source to destination in a matrix that satisfies given constraints
5. Change all elements of row i and column j in a matrix to 0 if cell (i, j) has value 0
6. Print diagonal elements of the matrix having positive slope
7. Find all paths from first cell to last cell of a matrix
8. Replace all occurrences of 0 that are not surrounded by 1 in a binary matrix
9. In-place rotate the matrix by 90 degrees in clock-wise direction
10. Count negative elements present in sorted matrix in linear time
11. Report all occurrences of an element in row wise and column wise sorted matrix in linear time
12. Calculate sum of all elements in a sub-matrix in constant time
13. Find maximum sum K x K sub-matrix in a given M x N matrix
14. Find maximum sum submatrix present in a given matrix
15. Count the number of islands
16. Flood Fill Algorithm
17. Find shortest safe route in a field with sensors present
18. Find all occurrences of given string in a character matrix
19. Shortest path in a Maze | Lee Algorithm
20. Check if given matrix is Toeplitz matrix or not
21. In-place rotate the matrix by 180 degrees
22. Fill Binary Matrix with Alternating Rectangles of 0 and 1
23. Find all common elements present in every row of given matrix
24. Construct a Binary Tree from Ancestor Matrix
25. Find common elements present in all rows of a matrix
26. Find index of the row containing maximum number of 1’s in a binary matrix
27. Find the largest square sub-matrix which is surrounded by all 1's
28. Find minimum passes required to convert all negative values in the matrix
29. Print a spiral square matrix without using any extra space
30. Print all shortest routes in a rectangular grid
31. Find length of longest path in the matrix with consecutive characters
32. Collect maximum value of coins in a matrix
33. Young Tableau | Insert, Search, Extract-Min, Delete, Replace
34. Sort an array using Young tableau
35. Find path from source to destination in a matrix that satisfies given constraints
36. Generate list of possible words from a character matrix
37. Find probability that a person is alive after taking N steps on the island
38. Collect maximum points in a matrix by satisfying given constraints
39. Count number of paths in a matrix with given cost to reach destination cell
40. Find longest sequence formed by adjacent numbers in the matrix
41. Find the minimum cost to reach last cell of the matrix from its first cell
42. Ways to reach the bottom-right corner of a matrix with exactly k turns allowed
43. Matrix Chain Multiplication
44. Find size of largest square sub-matrix of 1’s present in given binary matrix
45. Chess Knight Problem — Find Shortest path from source to destination
46. Find Duplicate rows in a binary matrix
47. Print all possible solutions to N Queens Problem
48. Print all Possible Knight’s Tours in a chessboard
49. Find Shortest Path in Maze
50. Find Longest Possible Route in a Matrix
51. Find total number of unique paths in a maze from source to destination
52. Calculate size of the largest plus of 1’s in binary matrix
53. Find the maximum value of M[c][d] — M[a][b] over all choices of indexes
54. Find shortest distance of every cell from landmine in a Maze
55. Find shortest route in a device to construct the given string
56. Calculate minimum cost to reach destination city from source city
57. Count all paths in a matrix from first cell to last cell
58. Merge M sorted lists each containing N elements
59. Travelling Salesman Problem using Branch and Bound
Puzzles
1. Clock Angle Problem — Find angle between hour and minute hand
2. Add two numbers without using addition operator
3. Generate power set of a given set
4. Implement power function without using multiplication and division operators
5. Print all numbers between 1 to N without using semicolon
6. Swap two numbers without using third variable
7. Determine the if condition to print specific output
8. Find maximum & minimum of triplet without using conditional statement and ternary operator
9. Find numbers represented as sum of two cubes for two different pairs
10. Print “Hello World” with empty main() function
11. Tower of Hanoi Problem
12. Print all numbers between 1 to N without using any loop
13. Print a semicolon without using semicolon anywhere in the program
14. Multiply two numbers without using multiplication operator or loops
15. Find square of a number without using multiplication and division operator
16. Find if a number is even or odd without using any conditional statement
17. Set both elements of a binary array to 0 in single line
18. Find minimum number without using conditional statement or ternary operator
19. Perform Division of two numbers without using division operator (/)
20. Generate 0 and 1 with 75% and 25% Probability
21. Generate Desired Random Numbers With Equal Probability
22. Return 0, 1 and 2 with equal Probability using the specified function
23. Generate Fair Results from a Biased Coin
24. Generate numbers from 1 to 7 with equal probability using specified function
25. Implement Ternary Operator Without Using Conditional Expressions
26. Determine if two integers are equal without using comparison and arithmetic operators
27. Return 0 and 1 with equal Probability using the specified function
28. Generate random input from an array according to given probabilities
29. Compute modulus division without division and modulo operator
Queue
1. Queue Implementation using Array/List — C, C++, Java, Python
2. Queue Implementation using Linked List
3. Implement Stack using Queue Data Structure
4. Implement a Queue using Stack Data Structure
5. Efficiently print all nodes between two given levels in a binary tree
6. Chess Knight Problem — Find Shortest path from source to destination
7. Shortest path in a Maze | Lee Algorithm
8. Find shortest safe route in a field with sensors present
9. Flood Fill Algorithm
10. Count the number of islands
11. Find Shortest path from source to destination in a matrix that satisfies given constraints
12. Generate binary numbers between 1 to N
13. Calculate height of a binary tree
14. Delete given Binary Tree
15. Level Order Traversal of Binary Tree
16. Spiral Order Traversal of Binary Tree
17. Reverse Level Order Traversal of Binary Tree
18. Print all nodes of a given binary tree in specific order
19. Print left view of binary tree
20. Find next node in same level for given node in a binary tree
21. Check if given binary tree is complete binary tree or not
22. Print Diagonal Traversal of Binary Tree
23. Print corner nodes of every level in binary tree
24. Invert a Binary Tree
25. Find minimum passes required to convert all negative values in the matrix
26. Convert a Binary Tree into a Doubly Linked List in Spiral Order
27. Check if a binary tree is a min-heap or not
28. Invert alternate levels of a perfect binary tree
29. Convert a Binary Search Tree into a Min Heap
30. Snake and Ladder Problem
31. Find shortest distance of every cell from landmine in a Maze
32. Convert a multilevel linked list to a singly linked list
33. Breadth First Search (BFS) Algorithm
34. Check if an undirected graph contains cycle or not
35. Find maximum cost path in graph from given source to destination
36. Total paths in given digraph from given source to destination having exactly m edges
37. Least cost path in given digraph from given source to destination having exactly m edges
Sorting
1. Insertion Sort Algorithm
2. Selection Sort Algorithm
3. Bubble Sort Algorithm
4. Merge Sort Algorithm
5. Iterative Merge Sort Algorithm (Bottom-up Merge Sort)
6. QuickSort Algorithm
7. Iterative Implementation of QuickSort
8. Hybrid QuickSort
9. QuickSort using Dutch National Flag Algorithm
10. QuickSort using Hoare’s Partitioning scheme
11. Heap Sort Algorithm
12. Introsort Algorithm
13. External Merge Sort Algorithm
14. Counting Sort Algorithm
15. Inversion Count of an array
16. Sort an array using Young tableau
17. Merge Sort Algorithm for Singly Linked List
18. Problems solved using partitioning logic of QuickSort
19. Sort a Doubly Linked List using Merge Sort
20. Sort elements by their frequency and Index
21. Sort an array based on order defined by another array
22. Efficiently sort an array with many duplicated values
23. Find largest number possible from set of given numbers
24. Find the surpasser count for each element of an array
25. Segregate positive and negative integers using Merge Sort
26. Group anagrams together from given list of words
Stack
1. Stack Implementation using Array/List — C, C++, Java, Python
2. Stack Implementation using Linked List
3. Check if given expression is balanced expression or not
4. Find duplicate parenthesis in an expression
5. Evaluate given postfix expression
6. Decode the given sequence to construct minimum number without repeated digits
7. Design a stack which returns minimum element in constant time
8. Design a stack which returns minimum element without using auxiliary stack
9. Merging Overlapping Intervals
10. Reverse String without using Recursion
11. Implement Stack using Queue Data Structure
12. Implement a Queue using Stack Data Structure
13. Implement two stacks in a single array
14. Recursive solution to sort a stack
15. Find length of the longest balanced parenthesis in a string
16. Reverse a string using stack data structure
17. Find all elements in an array that are greater than all elements present to their right
18. Inorder Tree Traversal
19. Preorder Tree Traversal
20. Postorder Tree Traversal
21. Find preorder traversal of a binary tree from its inorder and postorder sequence
22. Find ancestors of given node in a Binary Tree
23. Check if two given binary trees are identical or not
24. Reverse Level Order Traversal of Binary Tree
25. Reverse given text without reversing the individual words
26. Find all binary strings that can be formed from given wildcard pattern
27. Iterative Implementation of QuickSort
28. Depth First Search (DFS) Algorithm
29. Invert a Binary Tree
30. Print leaf to root path for every leaf node in a binary tree
31. Longest Increasing Subsequence
32. Invert alternate levels of a perfect binary tree
Trie
1. Trie Implementation — C, C++, Java, Python
2. Memory Efficient Implementation of Trie | Insert, Search and Delete
3. Longest Common Prefix in given set of strings (using Trie)
4. Lexicographic sorting of given set of keys
5. Find maximum occurring word in given set of strings
6. Find first k maximum occurring words in given set of strings
7. Find Duplicate rows in a binary matrix
8. Word Break Problem | Using Trie
9. Generate list of possible words from a character matrix
10. Find all words matching a pattern in the given dictionary