Codify DSA Sheet — 308 DSA problems with solutions
A free DSA sheet for placements and coding interviews: 308 problems in 16 sections, from basics, arrays and strings to trees, graphs and dynamic programming. Every problem has hints, the approach with time and space complexity, and solutions in C++, Java and Python, and you can run your code against real test cases.
Arrays
- Find the Largest Element in an Array — Easy
- Maximum in an Array — Easy
- Second Largest Element — Easy
- Check if Array is Sorted — Easy
- Remove Duplicates from Sorted Array — Easy
- Left Rotate by K — Easy
- Move Zeros to End — Easy
- Missing Number — Easy
- Max Consecutive Ones — Easy
- Single Number — Easy
- Best Time to Buy and Sell Stock — Easy
- Leaders in an Array — Easy
- Pascal's Triangle Row — Easy
- Merge Two Sorted Arrays — Easy
- Range Sum Queries — Easy
- Union of Two Sorted Arrays — Easy
- Rotate Array Right by K — Easy
- Two Sum — Medium
- Maximum Subarray Sum — Medium
- Majority Element — Medium
- Sort 0s, 1s and 2s — Medium
- Rearrange Array by Sign — Medium
- Longest Consecutive Sequence — Medium
- Subarray Sum Equals K — Medium
- Merge Intervals — Medium
- Maximum Product Subarray — Medium
- 3Sum Count — Medium
- Spiral Matrix — Medium
- Rotate Matrix 90 Degrees — Medium
- Set Matrix Zeroes — Medium
- Product of Array Except Self — Medium
- Next Permutation — Medium
- Find the Duplicate Number — Medium
- Majority Element II — Medium
- Repeating and Missing Number — Medium
- Best Time to Buy and Sell Stock II — Medium
- Maximum Circular Subarray Sum — Medium
- Count Subarrays with XOR K — Medium
- Count Inversions — Hard
- Trapping Rain Water — Hard
- Reverse Pairs — Hard
- Practice Streak — Easy
Basics
- Sum of Two Numbers — Easy
- Even or Odd — Easy
- Count Digits — Easy
- Reverse a Number — Easy
- Palindrome Number — Easy
- GCD and LCM — Easy
- Prime Check — Easy
- Sum of Digits — Easy
- Factorial — Easy
- Armstrong Number — Easy
- All Divisors — Easy
- Leap Year — Easy
- Decimal to Binary — Easy
- Perfect Number — Easy
- Prime Factorization — Easy
- Floyd's Triangle — Easy
- Star Pyramid — Easy
- Power with Modulo — Medium
- Count Primes up to N — Medium
- nCr Modulo a Large Prime — Medium
Strings
- Reverse a String — Easy
- Check Palindrome — Easy
- Count Vowels — Easy
- Valid Anagram — Easy
- First Non-Repeating Character — Easy
- Longest Common Prefix — Easy
- String Compression — Easy
- Roman to Integer — Easy
- Isomorphic Strings — Easy
- Check String Rotation — Easy
- Remove Outermost Parentheses — Easy
- Largest Odd Number in a String — Easy
- Maximum Nesting Depth of Parentheses — Easy
- Pangram Check — Easy
- Reverse Words in a String — Medium
- Integer to Roman — Medium
- Longest Palindromic Substring — Medium
- Count and Say — Medium
- String to Integer (atoi) — Medium
- Sort Characters by Frequency — Medium
- Find All Pattern Occurrences — Medium
- Shortest Palindrome by Adding in Front — Hard
Hashing & Two Pointers
- Frequency of Elements — Easy
- Contains Duplicate — Easy
- Intersection of Two Arrays — Easy
- Squares of a Sorted Array — Easy
- Happy Number — Easy
- Word Pattern — Easy
- Pair Sum in a Sorted Array — Easy
- Count Pairs with Difference K — Medium
- Container With Most Water — Medium
- Zero Sum Subarray — Medium
- Group Anagrams Count — Medium
- 3Sum Closest — Medium
- Longest Subarray with Equal 0s and 1s — Medium
- Subarray Sums Divisible by K — Medium
- Pairs Divisible by K — Medium
Sliding Window
- Maximum Sum Subarray of Size K — Easy
- Minimum Size Subarray Sum — Medium
- Longest Subarray with Sum K — Medium
- Distinct Elements in Every Window — Medium
- Longest Substring Without Repeating Characters — Medium
- Max Consecutive Ones III — Medium
- Fruit Into Baskets — Medium
- Longest Repeating Character Replacement — Medium
- Permutation in String — Medium
- Maximum Points from Cards — Medium
- Count Nice Subarrays — Medium
- Sliding Window Maximum — Hard
- Minimum Window Substring — Hard
- Subarrays with K Different Integers — Hard
- Shortest Window with K Distinct — Medium
Binary Search
- Lower Bound — Easy
- First and Last Occurrence — Easy
- Square Root (Floor) — Easy
- Kth Missing Positive Number — Easy
- Search Insert Position — Easy
- Floor and Ceil in a Sorted Array — Easy
- Row with Maximum 1s — Easy
- Binary Search — Medium
- Search in Rotated Sorted Array — Medium
- Peak of Mountain Array — Medium
- Koko Eating Bananas — Medium
- Minimum in Rotated Sorted Array — Medium
- Single Element in a Sorted Array — Medium
- Nth Root of an Integer — Medium
- Capacity to Ship Packages — Medium
- Minimum Days to Make Bouquets — Medium
- Search a 2D Matrix — Medium
- Aggressive Cows — Hard
- Book Allocation — Hard
- Median of Two Sorted Arrays — Hard
- Kth Element of Two Sorted Arrays — Hard
Recursion & Backtracking
- Fibonacci Number — Easy
- Subset Sums — Medium
- Generate Parentheses — Medium
- Permutations of a String — Medium
- Tower of Hanoi — Medium
- Combination Sum Count — Medium
- Rat in a Maze — Medium
- Binary Strings Without Consecutive 1s — Medium
- Letter Combinations of a Phone Number — Medium
- Subsets with Duplicates — Medium
- Combination Sum II — Medium
- Palindrome Partitioning — Medium
- Word Search — Medium
- M-Coloring Problem — Medium
- N-Queens Count — Hard
- Sudoku Solver — Hard
Linked List
- Reverse a Linked List — Easy
- Middle of a Linked List — Easy
- Palindrome Linked List — Easy
- Merge Two Sorted Lists — Easy
- Remove Duplicates from Sorted List — Easy
- Delete All Occurrences of a Key — Easy
- Detect a Cycle in a Linked List — Easy
- Remove Nth Node From End — Medium
- Add Two Numbers as Lists — Medium
- Rotate List — Medium
- Starting Point of a Loop — Medium
- Odd Even Linked List — Medium
- Intersection of Two Linked Lists — Medium
- Sort a Linked List — Medium
- Add One to a Linked List — Medium
- Reverse Nodes in K-Group — Hard
Stack & Queue
- Implement Queue using Stacks — Easy
- Previous Smaller Element — Easy
- Valid Parentheses — Medium
- Next Greater Element — Medium
- Min Stack — Medium
- Evaluate Postfix Expression — Medium
- Stock Span — Medium
- Daily Temperatures — Medium
- Next Greater Element II — Medium
- Asteroid Collision — Medium
- Remove K Digits — Medium
- Infix to Postfix — Medium
- Sum of Subarray Minimums — Medium
- The Celebrity Problem — Medium
- Largest Rectangle in Histogram — Hard
- LRU Cache — Hard
Heap & Priority Queue
- Kth Largest Element in an Array — Easy
- Last Stone Weight — Easy
- Minimum Cost to Connect Ropes — Easy
- K Closest Points to Origin — Medium
- Top K Frequent Elements — Medium
- Sort a K-Sorted Array — Medium
- Merge K Sorted Arrays — Medium
- Task Scheduler — Medium
- Hand of Straights — Medium
- Median of a Running Stream — Hard
Trees
- Height of a Binary Tree — Easy
- Inorder Traversal — Easy
- Level Order Traversal — Easy
- Preorder Traversal — Easy
- Postorder Traversal — Easy
- Identical Trees — Easy
- Symmetric Tree — Easy
- Search in a BST — Easy
- Balanced Binary Tree — Medium
- Diameter of a Binary Tree — Medium
- Validate Binary Search Tree — Medium
- Lowest Common Ancestor in a BST — Medium
- Left View of a Binary Tree — Medium
- Right Side View — Medium
- Zigzag Level Order Traversal — Medium
- Top View of a Binary Tree — Medium
- Bottom View of a Binary Tree — Medium
- Boundary Traversal — Medium
- Vertical Order Traversal — Medium
- Maximum Width of a Binary Tree — Medium
- Root to Leaf Paths — Medium
- Lowest Common Ancestor in a Binary Tree — Medium
- Kth Smallest Element in a BST — Medium
- Two Sum in a BST — Medium
- Construct Tree from Preorder and Inorder — Medium
- Nodes at Distance K — Medium
- Binary Tree Maximum Path Sum — Hard
- Time to Burn a Binary Tree — Hard
Trie
- Count Words with a Given Prefix — Easy
- Implement a Trie — Medium
- Longest Word with All Prefixes — Medium
- Count Distinct Substrings — Medium
- Maximum XOR of Two Numbers — Medium
- Maximum XOR with a Limit — Hard
Graphs
- BFS of a Graph — Easy
- DFS of a Graph — Easy
- Flood Fill — Easy
- Number of Provinces — Easy
- Number of Connected Components — Medium
- Shortest Path in an Unweighted Graph — Medium
- Detect Cycle in an Undirected Graph — Medium
- Number of Islands — Medium
- Dijkstra's Shortest Paths — Medium
- Course Schedule — Medium
- Rotting Oranges — Medium
- Distance of Nearest Zero — Medium
- Surrounded Regions — Medium
- Number of Enclaves — Medium
- Is Graph Bipartite — Medium
- Detect Cycle in a Directed Graph — Medium
- Topological Sort — Medium
- Minimum Spanning Tree — Medium
- Bellman-Ford Shortest Paths — Medium
- Floyd-Warshall All-Pairs Shortest Paths — Medium
- Cheapest Flights Within K Stops — Medium
- Path With Minimum Effort — Medium
- Strongly Connected Components — Hard
- Bridges in a Graph — Hard
- Word Ladder — Hard
- One Free Ride — Hard
Dynamic Programming
- Climbing Stairs — Easy
- Unique Grid Paths — Easy
- Frog Jump — Easy
- Longest Common Subsequence — Medium
- House Robber — Medium
- Coin Change (Minimum Coins) — Medium
- Coin Change (Number of Ways) — Medium
- 0/1 Knapsack — Medium
- Longest Increasing Subsequence — Medium
- Minimum Path Sum — Medium
- Partition Equal Subset Sum — Medium
- Longest Palindromic Subsequence — Medium
- Frog Jump with K Distances — Medium
- House Robber II — Medium
- Unique Paths with Obstacles — Medium
- Triangle Minimum Path Sum — Medium
- Maximum Falling Path Sum — Medium
- Count Subsets with Sum K — Medium
- Minimum Subset Sum Difference — Medium
- Unbounded Knapsack — Medium
- Rod Cutting — Medium
- Longest Common Substring — Medium
- Stock Trading with Cooldown — Medium
- Maximal Square — Medium
- Number of Longest Increasing Subsequences — Medium
- Edit Distance — Hard
- Matrix Chain Multiplication — Hard
- Distinct Subsequences — Hard
- Wildcard Matching — Hard
- Burst Balloons — Hard
- Palindrome Partitioning II — Hard
Greedy
- Assign Cookies — Easy
- Lemonade Change — Easy
- Minimum Coins (Greedy) — Easy
- N Meetings in One Room — Medium
- Jump Game — Medium
- Minimum Platforms — Medium
- Fractional Knapsack — Medium
- Job Sequencing — Medium
- Jump Game II — Medium
- Non-overlapping Intervals — Medium
- Insert Interval — Medium
- Gas Station — Medium
- Valid Parenthesis String — Medium
- Candy — Hard
Bit Manipulation
- Count Set Bits — Easy
- Power of Two — Easy
- XOR from 1 to N — Easy
- Check if Kth Bit is Set — Easy
- Minimum Bit Flips — Easy
- Single Number II — Medium
- Two Non-Repeating Numbers — Medium
- Divide Two Integers — Medium
- Total Set Bits from 1 to N — Medium
- Power Set Using Bitmasks — Medium