Most Asked DSA Interview Programs Solved in Python
Array Programs
- Reverse an array
- Find the maximum and minimum elements in an array
- Find the Kth maximum and minimum elements in an array
- Sort an array containing only 0, 1, and 2 without using a standard sorting algorithm
- Move all negative elements to one side of an array
- Find the union and intersection of two sorted arrays
- Cyclically rotate an array by one position
- Find the largest sum contiguous subarray in an array
- Minimize the maximum difference between heights in an array
- Find the minimum number of jumps to reach the end of an array
- Detect duplicates in an array of N+1 integers
- Merge two sorted arrays without using extra space
- Apply Kadane’s algorithm on an array for the maximum sum subarray
- Merge overlapping intervals in an array
- Generate the next permutation of an array
- Count inversions in an array
- Determine the best time to buy and sell stock using an array
- Find all pairs in an array whose sum equals a given number
- Find common elements in three sorted arrays
- Rearrange an array in alternating positive and negative order with O(1) extra space
- Check if any subarray within an array has a sum equal to 0
- Calculate the factorial of a large number using an array
- Find the maximum product subarray in an array
- Identify the longest consecutive subsequence in an array
- Find all elements in an array that appear more than n/k times
- Calculate maximum profit by buying and selling stock at most twice using an array
- Check whether one array is a subset of another array
- Find a triplet in an array that sums to a given value
- Solve the trapping rain water problem using an array
- Solve the chocolate distribution problem using an array
- Find the smallest subarray in an array with a sum greater than a given value
- Partition an array into three parts around a given value
- Find the minimum swaps required to bring elements less than or equal to a given value together in an array
- Determine the minimum operations required to make an array a palindrome
- Find the median of two sorted arrays of equal size
- Find the median of two sorted arrays of different sizes
Searching and Sorting Programs
- Search in a rotated sorted array
- Find the repeating and missing elements in an array
- Find first and last positions of an element in a sorted array
- Find a fixed point (value equal to index) in an array
- Find the square root of an integer
- Find the maximum and minimum of an array using minimum comparisons
- Find the optimum location of a point to minimize total distance
- Find the majority element in an array
- Search in an array where adjacent elements differ by at most k
- Find a pair in an array with a given difference
- Find four elements in an array that sum to a given value
- Find the maximum sum in an array such that no two elements are adjacent
- Count triplets in an array with a sum smaller than a given value
- Merge two sorted arrays
- Print all subarrays with zero sum
- Solve the product array puzzle
- Sort an array according to the count of set bits
- Find the minimum number of swaps required to sort an array
- Solve the “Bishu and Soldiers” problem
- Solve the “Rasta and Kheshtak” problem
- Find the kth smallest number in an array
- Find the pivot element in a sorted array
- Find the kth element of two sorted arrays
- Solve the “Aggressive Cows” problem
- Solve the Book Allocation problem
- Solve the EKOSPOJ problem
- Implement a job scheduling algorithm
- Find the missing number in an arithmetic progression
- Find the smallest number with at least n trailing zeroes in factorial
- Solve the Painters Partition problem
- Solve the ROTI-Prata SPOJ problem
- Solve the Double Helix SPOJ problem
- Solve the Subset Sums problem
- Find the inversion count in an array
- Implement merge sort in-place
- Partition and sort arrays with many repeated entries
Matrix Programs
- Spiral traversal of a matrix
- Search for an element in a matrix
- Find the median in a row-wise sorted matrix
- Find the row with the maximum number of 1’s in a matrix
- Print elements in sorted order using a row-column wise sorted matrix
- Find the maximum size rectangle in a matrix
- Find a specific pair in a matrix
- Rotate a matrix by 90 degrees
- Find the kth smallest element in a row-column wise sorted matrix
- Find common elements in all rows of a matrix
String Programs
- Reverse a string
- Check whether a string is a palindrome
- Find duplicate characters in a string
- Why are strings immutable in Java?
- Check whether one string is a rotation of another
- Check whether a string is a valid shuffle of two strings
- Count and say problem
- Find the longest palindrome in a string (Longest Palindromic Substring)
- Find longest recurring subsequence in a string
- Print all subsequences of a string
- Print all permutations of a string
- Split a binary string into two substrings with equal 0’s and 1’s
- Solve the word wrap problem
- Solve the edit distance problem
- Find the next greater number with the same set of digits
- Solve the balanced parenthesis problem
- Solve the word break problem
- Implement the Rabin-Karp algorithm
- Implement the KMP algorithm
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Find the minimum number of bracket reversals needed to balance an expression
- Count all palindromic subsequences in a string
- Count the occurrences of a given string in a 2D character array
- Search for a word in a 2D grid of characters
- Implement the Boyer-Moore algorithm for pattern searching
- Convert Roman numerals to decimal
- Find the longest common prefix among strings
- Count the number of flips needed to make a binary string alternate
- Find the first repeated word in a string
- Find the minimum number of swaps required for bracket balancing
- Find the longest common subsequence between two strings
- Generate all possible valid IP addresses from a given string
- Find the smallest window that contains all characters of the string
- Rearrange characters in a string so that no two adjacent are the same
- Find the minimum characters to add at the front to make a string palindrome
- Print all anagrams together from a sequence of words
- Find the smallest window in a string containing all characters of another string
- Recursively remove all adjacent duplicates in a string
- Perform string matching with wildcard characters
- Find the number of customers who could not get a computer
- Transform one string to another using the minimum number of operations
- Check if two given strings are isomorphic
- Recursively print all sentences that can be formed from a list of word lists
LinkedList Programs
- Reverse a linked list (Iterative and Recursive)
- Reverse a linked list in groups of given size
- Detect a loop in a linked list
- Delete a loop in a linked list
- Find the starting point of a loop in a linked list
- Remove duplicates in a sorted linked list
- Remove duplicates in an unsorted linked list
- Move the last element to the front of a linked list
- Add 1 to a number represented as a linked list
- Add two numbers represented by linked lists
- Find the intersection of two sorted linked lists
- Find the intersection point of two linked lists
- Merge sort for linked lists
- Quicksort for linked lists
- Find the middle element of a linked list
- Check if a linked list is circular
- Split a circular linked list into two halves
- Check if a singly linked list is a palindrome
- Delete a node from a circular linked list
- Reverse a doubly linked list
- Find pairs with a given sum in a doubly linked list
- Sort a k-sorted doubly linked list
- Rotate a doubly linked list by N nodes
- Rotate a doubly linked list in groups of given size
- Can we reverse a linked list in less than O(n)?
- Why is quicksort preferred for arrays and merge sort for linked lists?
- Flatten a linked list
- Sort a linked list of 0’s, 1’s, and 2’s
- Merge k sorted linked lists
- Multiply two numbers represented by linked lists
- Count triplets in a sorted doubly linked list whose sum equals a given value
- Clone a linked list with next and random pointers
- Delete nodes that have a greater value on the right side
- Segregate even and odd nodes in a linked list
- Find the nth node from the end of a linked list
- Find the first non-repeating character from a stream of characters
Binary Trees
- Preorder traversal of a binary tree (recursive and iterative)
- Postorder traversal of a binary tree (recursive and iterative)
- Level order traversal of a binary tree
- Reverse level order traversal of a binary tree
- Height of a binary tree
- Diameter of a binary tree
- Mirror of a binary tree
- Inorder traversal of a binary tree (recursive and iterative)
- Left view of a binary tree
- Right view of a binary tree
- Top view of a binary tree
- Bottom view of a binary tree
- Zigzag traversal of a binary tree
- Check if a binary tree is balanced
- Diagonal traversal of a binary tree
- Boundary traversal of a binary tree
- Construct a binary tree from a string with bracket representation
- Convert a binary tree into a doubly linked list
- Convert a binary tree into a sum tree
- Construct a binary tree from inorder and preorder traversals
- Find minimum swaps required to convert a binary tree into a BST
- Check if a binary tree is a sum tree
- Check if all leaf nodes are at the same level in a binary tree
- Find the lowest common ancestor (LCA) in a binary tree
- Solve the tree isomorphism problem
- Check if a binary tree contains duplicate subtrees of size 2 or more
- Check if two binary trees are mirror images of each other
- Calculate the sum of nodes on the longest path from root to leaf
- Verify if a given graph is a tree
- Find the largest subtree sum in a binary tree
- Find the maximum sum of nodes in a binary tree such that no two are adjacent
- Print all paths in a binary tree with a given sum
- Find the distance between two nodes in a binary tree
- Find the kth ancestor of a node in a binary tree
- Find all duplicate subtrees in a binary tree
Binary Search Trees
- Find a value in a BST
- Delete a node in a BST
- Find the minimum and maximum values in a BST
- Find the inorder successor and inorder predecessor in a BST
- Check if a tree is a BST
- Populate the inorder successor for all nodes in a BST
- Find the lowest common ancestor (LCA) of two nodes in a BST
- Construct a BST from a preorder traversal
- Convert a binary tree into a BST
- Convert a BST into a balanced BST
- Merge two BSTs
- Find the kth largest element in a BST
- Find the kth smallest element in a BST
- Count pairs from two BSTs whose sum is equal to a given value
- Find the median of a BST in O(n) time and O(1) space
- Replace every element with the least greater element on its right
- Find conflicting appointments given n appointments using a BST approach
- Flatten a BST into a sorted list
- Count BST nodes that lie within a given range
- Check if a preorder traversal is valid for a BST
- Check whether a BST contains a dead end
- Find the largest BST within a binary tree
Greedy Algorithm Problems
- Find the minimum and maximum amount to buy all N candies
- Minimize cash flow among a given set of friends who have borrowed money from each other
- Minimum cost to cut a board into squares
- Check if it is possible to survive on an island
- Activity Selection Problem
- Job Sequencing Problem
- Huffman Coding
- Water Connection Problem
- Fractional Knapsack Problem
- Find the minimum number of coins (coin change problem)
- Maximum trains for which stoppage can be provided
- Minimum Platforms Problem
- Buy maximum stocks if i stocks can be bought on i-th day
- Maximum meetings in one room
- Maximum product subset of an array
- Maximize array sum after K negations
- Maximize the sum of arr[i] * i
- Maximum sum of absolute difference of an array
- Maximize sum of consecutive differences in a circular array
- Minimum sum of absolute differences of pairs of two arrays
- Shortest Job First (SJF) CPU Scheduling
- Least Recently Used (LRU) Page Replacement algorithm
- Smallest subset with sum greater than all other elements
- Chocolate Distribution Problem
- DEFKIN – Defense of a Kingdom
- ARRANGE – Arranging Amplifiers
- K Centers Problem
- Minimum cost of ropes
- Find maximum sum possible from equal sum of three stacks
- Find the smallest number with a given number of digits and sum of digits
- Rearrange characters in a string such that no two adjacent are the same
Back Tracking Programs
- Rat in a Maze Problem
- Printing All Solutions for the N-Queen Problem
- Word Break Problem Using Backtracking
- Remove Invalid Parentheses
- Sudoku Solver
- m Coloring Problem
- Print All Palindromic Partitions of a String
- Subset Sum Problem
- The Knight’s Tour Problem
- Tug of War
- Find the Shortest Safe Route in a Path with Landmines
- Combination Sum
- Find the Maximum Number Possible by Doing At-Most K Swaps
- Print All Permutations of a String
- Find if There Is a Path Longer Than K from a Given Source
- Longest Possible Route in a Matrix with Hurdles
- Print All Possible Paths from Top Left to Bottom Right in an m×n Matrix
- Partition a Set into K Subsets with Equal Sum
- Find the K-th Permutation Sequence of the First N Natural Numbers
Stacks and Queues Programs
- Implement a stack from scratch
- Implement a queue from scratch
- Implement two stacks in an array
- Find the middle element of a stack
- Implement N stacks in an array
- Check if an expression has valid (balanced) parentheses
- Reverse a string using a stack
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Find the next greater element using a stack
- Solve the celebrity problem using stacks/queues
- Evaluate an arithmetic expression using a stack
- Evaluate a postfix expression
- Insert an element at the bottom of a stack without using any additional data structure
- Reverse a stack using recursion
- Sort a stack using recursion
- Merge overlapping intervals using a stack
- Find the largest rectangular area in a histogram
- Find the length of the longest valid substring
- Check if an expression contains redundant brackets
- Implement a stack using a queue
- Implement a stack using a deque
- Check if one array is a valid stack permutation of another
- Implement a queue using a stack
- Implement N queues in an array
- Implement a circular queue
- Implement an LRU cache
- Reverse a queue using recursion
- Reverse the first K elements of a queue
- Interleave the first half of a queue with the second half
- Find the first circular tour that visits all petrol pumps
- Find the minimum time required to rot all oranges
- Find the distance of the nearest cell having 1 in a binary matrix
- Find the first negative integer in every window of size K
- Check if all levels of two trees are anagrams
- Calculate the sum of the minimum and maximum elements of all subarrays of size K
- Calculate the minimum sum of squares of character counts in a string after removing K characters
- Find the next smaller element using a queue-based approach
Heap Programs
- Implement a max heap/min heap using arrays and recursion
- Heap sort an array using a heap
- Find the maximum of all subarrays of size k using a heap
- Find the k largest elements in an array using a heap
- Find the kth smallest and kth largest element in an unsorted array using a heap
- Merge k sorted arrays using a heap [IMP]
- Merge 2 binary max heaps
- Find the kth largest sum of continuous subarrays using a heap
- Reorganize strings using a heap (Leetcode)
- Merge k sorted linked lists using a heap [V.IMP]
- Find the smallest range in k lists using a heap
- Find the median in a stream of integers using a heap
- Check if a binary tree is a heap
- Convert a BST to a min heap
- Convert a min heap to a max heap
- Find the minimum sum of two numbers formed from digits of an array using a heap
- Rearrange characters in a string such that no two adjacent characters are the same using a heap
Graph Programs
- Create a graph and print it
- Implement BFS algorithm
- Implement DFS algorithm
- Detect cycle in a directed graph using BFS/DFS
- Detect cycle in an undirected graph using BFS/DFS
- Search in a maze
- Find the minimum steps for a knight
- Flood fill algorithm
- Clone a graph
- Making wired connections
- Word ladder
- Dijkstra algorithm
- Implement topological sort
- Find the minimum time to complete each job in a DAG
- Determine if it is possible to finish all tasks given dependencies
- Count the number of islands
- Given a sorted dictionary of an alien language, determine the order of characters
- Implement Kruskal’s algorithm
- Implement Prim’s algorithm
- Count the total number of spanning trees in a graph
- Implement Bellman-Ford algorithm
- Implement Floyd-Warshall algorithm
- Travelling Salesman Problem
- Graph colouring problem
- Snake and Ladders problem
- Find bridges in a graph
- Count strongly connected components (Kosaraju algorithm)
- Check if a graph is bipartite
- Detect a negative cycle in a graph
- Find the longest path in a Directed Acyclic Graph
- Journey to the Moon
- Cheapest flights within K stops
- Oliver and the Game
- Water Jug problem using BFS
- Find if there is a path longer than K from a given source
- m-Colouring problem
- Find the minimum edges to reverse to make a path from source to destination
- Find paths that traverse each node using every edge (Seven Bridges problem)
- Vertex Cover problem
- Chinese Postman (Route Inspection) problem
- Count the number of triangles in a graph (directed and undirected)
- Minimize cash flow among a set of friends who have borrowed money
- Two Clique problem
Dynamic Programming Programs
- Knapsack Problem
- Binomial Coefficient Problem
- Permutation Coefficient Problem
- nth Catalan Number
- Matrix Chain Multiplication
- Edit Distance
- Subset Sum Problem
- Coin Change Problem
- Friends Pairing Problem
- Gold Mine Problem
- Assembly Line Scheduling Problem
- Painting the Fence Problem
- Maximize the Cut Segments
- Longest Common Subsequence (LCS)
- Longest Repeated Subsequence
- Longest Increasing Subsequence
- Space-Optimized LCS
- LCS of Three Strings
- Maximum Sum Increasing Subsequence
- Count All Subsequences with Product Less Than K
- Longest Subsequence with Adjacent Difference of One
- Maximum Subsequence Sum with No Three Consecutive Elements
- Egg Dropping Problem
- Maximum Length Chain of Pairs
- Maximum Size Square Sub-matrix with All 1s
- Maximum Sum of Pairs with Specific Difference
- Minimum Cost Path Problem
- Maximum Difference of Zeros and Ones in a Binary String
- Minimum Number of Jumps to Reach the End
- Minimum Cost to Fill a Given Weight in a Bag
- Minimum Removals from an Array to Make Max – Min ≤ K
- Longest Common Substring
- Count the Number of Ways to Reach a Given Score in a Game
- Count Balanced Binary Trees of Height h
- Largest Sum Contiguous Subarray
- Smallest Sum Contiguous Subarray
- Unbounded Knapsack (Repetition of Items Allowed)
- Word Break Problem
- Largest Independent Set Problem
- Partition Problem
- Longest Palindromic Subsequence
- Count All Palindromic Subsequences in a String
- Longest Palindromic Substring
- Longest Alternating Subsequence
- Weighted Job Scheduling
- Coin Game Winner (Each Player Has Three Choices)
- Count Derangements (Permutations with No Element in Its Original Position)
- Maximum Profit by Buying and Selling a Share at Most Twice
- Optimal Strategy for a Game
- Optimal Binary Search Tree
- Palindrome Partitioning Problem
- Word Wrap Problem
- Mobile Numeric Keypad Problem
- Boolean Parenthesization Problem
- Largest Rectangular Sub-matrix Whose Sum is 0
- Largest Area Rectangular Sub-matrix with Equal Number of 1’s and 0’s
- Maximum Sum Rectangle in a 2D Matrix
- Maximum Profit by Buying and Selling a Share at Most k Times
- Find if a String is an Interleaving of Two Other Strings
- Maximum Length of Pair Chain
Bit Manipulation Programs
- Count set bits in an integer
- Find the two non-repeating elements in an array of repeating elements
- Count the number of bits to be flipped to convert A to B
- Count total set bits in all numbers from 1 to n
- Check whether a number is a power of two
- Find the position of the only set bit
- Copy set bits in a given range
- Divide two integers without using multiplication, division, or mod operators
- Calculate the square of a number without using multiplication, division, or pow()
- Power set