
Below you can find a curated list of exercises for each topic covered in the DSA course. Each exercise links to a dedicated page with a problem summary, the key techniques involved, and a TypeScript solution walkthrough.
| Exercise | Difficulty | Description |
|---|---|---|
| Best Time to Buy and Sell Stock | Easy | Given an array of stock prices where prices[i] is the price on the i-th day, find the maximum profit by choosing a single day to buy and a different future day to sell. |
| Best Time to Buy and Sell Stock II | Medium | Given an array of stock prices, you can buy and sell the stock multiple times — find the maximum total profit you can achieve. |
| First Missing Positive | Hard | Given an unsorted integer array nums, return the smallest missing positive integer. The algorithm must run in O(n) time and use constant extra space. |
| Increasing Triplet Subsequence | Medium | Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. |
| Majority Element | Easy | Given an array nums of size n, return the majority element — the element that appears more than ⌊n / 2⌋ times. The majority element always exists in the array. |
| Move Zeroes | Easy | Given an integer array nums, move all 0s to the end of it while maintaining the relative order of the non-zero elements, in-place. |
| Number of Zero-Filled Subarrays | Medium | Given an integer array nums, return the number of subarrays entirely filled with 0s. A subarray is a contiguous non-empty sequence of elements within an array. |
| Product of Array Except Self | Medium | Given an integer array nums, return an array where each element is the product of all other elements, without using division, in O(n) time. |
| Remove Duplicates from Sorted Array | Easy | Given a sorted array nums, remove duplicates in-place so each unique element appears only once and return the count of unique elements. |
| Rotate Array | Medium | Given an array, rotate it to the right by k steps, where k is non-negative, using an in-place reversal technique. |
| Exercise | Difficulty | Description |
|---|---|---|
| Guess the Word | Hard | You are given an array of unique strings words where words[i] is six letters long. One word of words was chosen as a secret word. |
| Is Subsequence | Easy | Given two strings s and t, return true if s is a subsequence of t, or false otherwise. |
| Longest Common Prefix | Easy | Write a function to find the longest common prefix string amongst an array of strings. |
| Reverse Words in a String | Medium | Given an input string s, reverse the order of the words. |
| Valid Palindrome | Easy | A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. |
| Zigzag Conversion | Medium | The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) |
| Exercise | Difficulty | Description |
|---|---|---|
| Bitwise AND of Numbers Range | Medium | Given two integers |
| Counting Bits | Easy | Given an integer |
| Number of 1 Bits | Easy | Given a positive integer |
| Reverse Bits | Easy | Reverse bits of a given 32 bits signed integer. |
| Single Number | Easy | Given a non-empty array of integers |
| Single Number III | Medium | Given an integer array |
| Sum of Two Integers | Medium | Given two integers |
| Exercise | Difficulty | Description |
|---|---|---|
| Isomorphic Strings | Easy | Given two strings |
| Contains Duplicate II | Easy | Given an integer array |
| Design HashMap | Easy | Design a HashMap without using any built-in hash table libraries. |
| Encode and Decode TinyURL | Medium | Note: This is a companion problem to the System Design problem: Design TinyURL. |
| Group Anagrams | Medium | Given an array of strings |
| Longest Consecutive Sequence | Medium | Given an unsorted array of integers |
| Maximum Number of Balloons | Easy | Given a string |
| Number of Good Pairs | Easy | Given an array of integers |
| Number of Good Ways to Split a String | Medium | You are given a string |
| Number of Matching Subsequences | Medium | Given a string |
| Ransom Note | Easy | Given two strings |
| Reorganize String | Medium | Given a string |
| Split Array into Consecutive Subsequences | Medium | You are given an integer array |
| Exercise | Difficulty | Description |
|---|---|---|
| 3Sum | Medium | Given an integer array nums, return all the triplets |
| Container With Most Water | Medium | You are given an integer array |
| Merge Sorted Array | Easy | You are given two integer arrays |
| Trapping Rain Water | Hard | Given |
| Two Sum II - Input Array Is Sorted | Medium | Given a 1-indexed array of integers |
| Exercise | Difficulty | Description |
|---|---|---|
| Contiguous Array | Medium | Given a binary array |
| Continuous Subarray Sum | Medium | Given an integer array nums and an integer k, return |
| Range Sum Query - Immutable | Easy | Given an integer array |
| Subarray Sum Equals K | Medium | Given an array of integers |
| Subarray Sums Divisible by K | Medium | Given an integer array |
| Exercise | Difficulty | Description |
|---|---|---|
| Find All Anagrams In A String | Medium | Given two strings |
| Longest Repeating Character Replacement | Medium | You are given a string |
| Longest Substring Without Repeating Characters | Medium | Given a string |
| Max Consecutive Ones III | Medium | Given a binary array |
| Maximum Average Subarray I | Easy | You are given an integer array |
| Maximum Sum of Distinct Subarrays With Length K | Medium | You are given an integer array |
| Minimum Size Subarray Sum | Medium | Given an array of positive integers |
| Minimum Window Substring | Hard | Given two strings |
| Permutation in String | Medium | Given two strings |
| Substring with Concatenation of All Words | Hard | You are given a string |
| Exercise | Difficulty | Description |
|---|---|---|
| Best Sightseeing Pair | Medium | You are given an integer array |
| Maximum Product Subarray | Medium | Given an integer array |
| Maximum Sum Circular Subarray | Medium | Given a circular integer array |
| Maximum Subarray | Medium | Given an integer array |
| Exercise | Difficulty | Description |
|---|---|---|
| Game of Life | Medium | According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." |
| Rotate Image | Medium | You are given an |
| Set Matrix Zeroes | Medium | Given an |
| Spiral Matrix | Medium | Given an |
| Valid Sudoku | Medium | Determine if a |
| Exercise | Difficulty | Description |
|---|---|---|
| Add Two Numbers | Medium | You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers... |
| Copy List With Random Pointer | Medium | A linked list of length |
| Design Linked List | Medium | Design your implementation of the linked list. You can choose to use a singly or doubly linked list. |
| Flatten a Multilevel Doubly Linked List | Medium | You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate dou... |
| Intersection of Two Linked Lists | Easy | Given the heads of two singly linked-lists |
| Partition List | Medium | Given the |
| Remove Duplicates from Sorted List II | Medium | Given the |
| Remove Nth Node From End of List | Medium | Given the |
| Rotate List | Medium | Given the |
| Swap Nodes in Pairs | Medium | Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) |
| Exercise | Difficulty | Description |
|---|---|---|
| Basic Calculator II | Medium | Given a string |
| Evaluate Reverse Polish Notation | Medium | You are given an array of strings |
| Longest Valid Parentheses | Hard | Given a string containing just the characters |
| Min Stack | Medium | Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. |
| Remove All Adjacent Duplicates In String | Easy | You are given a string |
| Remove Duplicate Letters | Medium | Given a string |
| Removing Stars From a String | Medium | You are given a string |
| Valid Parentheses | Easy | Given a string |
| Exercise | Difficulty | Description |
|---|---|---|
| Number of Recent Calls | Easy | You have a |
| Reveal Cards In Increasing Order | Medium | You are given an integer array |
| Time Needed To Buy Tickets | Easy | There are |
| Exercise | Difficulty | Description |
|---|---|---|
| Maximum Gap | Medium | Given an integer array |
| Sort Characters By Frequency | Medium | Given a string |
| Top K Frequent Words | Medium | Given an array of strings |
| Exercise | Difficulty | Description |
|---|---|---|
| Decode String | Medium | Given an encoded string, return its decoded string. |
| Merge Two Sorted Lists | Easy | You are given the heads of two sorted linked lists |
| Pow(x, n) | Medium | Implement pow(x, n), which calculates |
| Exercise | Difficulty | Description |
|---|---|---|
| Reverse Pairs | Hard | Given an integer array |
| Sort List | Medium | Given the |
| Exercise | Difficulty | Description |
|---|---|---|
| Kth Largest Element in an Array | Medium | Given an integer array |
| Sort Colors | Medium | Given an array |
| Exercise | Difficulty | Description |
|---|---|---|
| Find First and Last Position of Element in Sorted Array | Medium | Given an array of integers |
| Find in Mountain Array | Hard | (This problem is an interactive problem.) |
| Find Minimum in Rotated Sorted Array | Medium | Suppose an array of length |
| Find Peak Element | Medium | A peak element is an element that is strictly greater than its neighbors. |
| Koko Eating Bananas | Medium | Koko loves to eat bananas. There are |
| Median of Two Sorted Arrays | Hard | Given two sorted arrays |
| Random Pick with Weight | Medium | You are given a 0-indexed array of positive integers |
| Search a 2D Matrix | Medium | You are given an |
| Search in Rotated Sorted Array | Medium | There is an integer array |
| Search Insert Position | Easy | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. |
| Exercise | Difficulty | Description |
|---|---|---|
| Combination Sum | Medium | Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You ... |
| Combination Sum II | Medium | Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. |
| Generate Parentheses | Medium | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. |
| Letter Combinations of a Phone Number | Medium | Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. |
| N-Queens | Hard | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. |
| Palindrome Partitioning | Medium | Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. |
| Permutations | Medium | Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. |
| Subsets | Medium | Given an integer array nums of unique elements, return all possible subsets (the power set). |
| Exercise | Difficulty | Description |
|---|---|---|
| 01 Matrix | Medium | Given an |
| Binary Search Tree Iterator | Medium | Implement the |
| Binary Tree Inorder Traversal | Easy | Given the |
| Binary Tree Level Order Traversal | Medium | Given the |
| Binary Tree Maximum Path Sum | Hard | A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that ... |
| Binary Tree Paths | Easy | Given the |
| Binary Tree Postorder Traversal | Easy | Given the |
| Binary Tree Preorder Traversal | Easy | Given the |
| Binary Tree Right Side View | Medium | Given the |
| Binary Tree Zigzag Level Order Traversal | Medium | Given the |
| Bus Routes | Hard | You are given an array |
| Construct Binary Tree from Inorder and Postorder Traversal | Medium | Given two integer arrays |
| Construct Binary Tree from Preorder and Inorder Traversal | Medium | Given two integer arrays |
| Convert Sorted Array to Binary Search Tree | Easy | Given an integer array |
| Count Complete Tree Nodes | Easy | Given the |
| Delete Nodes And Return Forest | Medium | Given the |
| Diameter of Binary Tree | Easy | Given the |
| Distribute Coins in Binary Tree | Medium | You are given the |
| Find Duplicate Subtrees | Medium | Given the |
| Flatten Binary Tree to Linked List | Medium | Given the |
| Invert Binary Tree | Easy | Given the |
| Kth Smallest Element in a BST | Medium | Given the |
| Lowest Common Ancestor of a Binary Tree | Medium | Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. |
| Maximum Difference Between Node and Ancestor | Medium | Given the |
| Maximum Width of Binary Tree | Medium | Given the |
| Minimum Absolute Difference in BST | Easy | Given the |
| Minimum Distance Between BST Nodes | Easy | Given the |
| Open the Lock | Medium | You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: |
| Path Sum III | Medium | Given the |
| Populating Next Right Pointers in Each Node II | Medium | Given a binary tree |
| Rotting Oranges | Medium | You are given an |
| Same Tree | Easy | Given the roots of two binary trees |
| Serialize and Deserialize Binary Tree | Hard | Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to... |
| Shortest Path in a Grid with Obstacles Elimination | Hard | You are given an |
| Symmetric Tree | Easy | Given the |
| Trim a Binary Search Tree | Medium | Given the |
| Validate Binary Search Tree | Medium | Given the |
| Word Ladder | Hard | A transformation sequence from word |
| Exercise | Difficulty | Description |
|---|---|---|
| My Calendar I | Medium | You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking. |
| My Calendar II | Medium | You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking. |
| Stock Price Fluctuation | Medium | You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp. |
| Trim a Binary Search Tree | Medium | Given the |
| Exercise | Difficulty | Description |
|---|---|---|
| Design Add and Search Words Data Structure | Medium | Design a data structure that supports adding new words and finding if a string matches any previously added string. |
| Implement Trie (Prefix Tree) | Medium | A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structu... |
| Longest Word in Dictionary | Medium | Given an array of strings |
| Maximum XOR of Two Numbers in an Array | Medium | Given an integer array |
| Search Suggestions System | Medium | You are given an array of strings |
| Word Search II | Hard | Given an |
| Exercise | Difficulty | Description |
|---|---|---|
| Find Median from Data Stream | Hard | Design a data structure that supports inserting integers from a data stream one at a time and efficiently computing the median of all values added so far. |
| Furthest Building You Can Reach | Medium | Given building heights, a supply of bricks and ladders, travel from building 0 as far as possible by optimally choosing when to spend bricks versus ladders for upward climbs. |
| IPO | Hard | Given n projects each with a required capital and profit, starting with w capital and allowed to complete at most k projects, return the maximum capital achievable by greedily picking the highest-profit affordable project at each step. |
| K Closest Points to Origin | Medium | Given an array of 2D points and an integer k, return the k points closest to the origin (0,0) by Euclidean distance. |
| Kth Largest Element in a Stream | Easy | Design a class that maintains a running stream of integers and returns the k-th largest value after each new integer is added. |
| Process Tasks Using Servers | Medium | You have n servers (each with a weight) and m tasks (each with a processing duration). Assign each task to the free server with the smallest weight, breaking ties by index. Return the server index assigned to each task. |
| Single-Threaded CPU | Medium | Given n tasks each with an enqueue time and processing duration, simulate a single-threaded CPU that always picks the shortest available task. Return the order in which tasks are processed. |
| Sliding Window Median | Hard | Given an integer array and a window size k, return the median for each sliding window position as the window moves from left to right across the array. |
| Top K Frequent Elements | Medium | Given an integer array nums and an integer k, return the k elements that appear most frequently. The answer is guaranteed to be unique. |
| Exercise | Difficulty | Description |
|---|---|---|
| Insert Interval | Medium | You are given an array of non-overlapping intervals |
| Maximum Number Of Events That Can Be Attended | Medium | You are given an array of |
| Merge Intervals | Medium | Given an array of |
| Minimum Number of Arrows to Burst Balloons | Medium | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array |
| Non Overlapping Intervals | Medium | Given an array of intervals |
| Exercise | Difficulty | Description |
|---|---|---|
| Find K Pairs with Smallest Sums | Medium | You are given two integer arrays |
| Kth Smallest Element in a Sorted Matrix | Medium | Given an |
| Merge k Sorted Lists | Hard | You are given an array of |
| Smallest Range Covering Elements from K Lists | Hard | You have |
| Exercise | Difficulty | Description |
|---|---|---|
| Design a Food Rating System | Medium | Design a food rating system that supports modifying food ratings and returning the highest-rated food for a given cuisine. |
| Design Browser History | Medium | Design a browser history system that supports visiting URLs, navigating back, and navigating forward through history. |
| Design Twitter | Medium | Design a simplified Twitter where users can post tweets, follow and unfollow others, and retrieve the 10 most recent tweets in their news feed. |
| Insert Delete GetRandom O(1) | Medium | Implement a data structure that supports insert, delete, and getRandom in average O(1) time. |
| LRU Cache | Medium | Design a data structure that follows the constraints of a Least Recently Used (LRU) cache with O(1) get and put operations. |
| Maximum Frequency Stack | Hard | Design a stack-like data structure that pushes elements and pops the most frequent element, breaking ties by recency. |
| Snapshot Array | Medium | Implement a SnapshotArray that supports setting values, taking snapshots, and retrieving the value at a given index for a past snapshot. |
| Time Based Key-Value Store | Medium | Design a time-based key-value data structure that can store multiple values for the same key at different timestamps. |
| Exercise | Difficulty | Description |
|---|---|---|
| Candy | Hard | Given an array of children's ratings, distribute the minimum number of candies so that each child gets at least one and children with higher ratings get more than their neighbors. |
| Gas Station | Medium | Given gas available and cost to travel between stations on a circular route, find the starting station index that allows a complete circuit, or return -1. |
| Jump Game II | Medium | Given an array where each element represents the maximum forward jump length, find the minimum number of jumps to reach the last index. |
| Minimum Add to Make Parentheses Valid | Medium | Given a parentheses string, determine the minimum number of insertions needed to make it valid. |
| Minimum Cost to Hire K Workers | Hard | Given workers with quality and wage expectations, find the minimum cost to hire exactly k workers while paying proportionally to quality. |
| Minimum Number of Refueling Stops | Hard | Given a target distance, starting fuel, and gas stations along the way, find the minimum number of refueling stops to reach the destination. |
| Task Scheduler | Medium | Given CPU tasks with cooldown constraints, find the minimum number of intervals needed to complete all tasks. |
| Exercise | Difficulty | Description |
|---|---|---|
| 01 Matrix | Medium | Given a binary matrix, find the distance of each cell to the nearest zero using BFS. |
| All Nodes Distance K in Binary Tree | Medium | Given a binary tree, a target node, and an integer K, return the values of all nodes that are exactly distance K from the target. |
| All Paths From Source to Target | Medium | Given a directed acyclic graph with n nodes, find all possible paths from node 0 to node n-1. |
| Bus Routes | Hard | Given bus routes and a source and target stop, find the minimum number of buses needed to travel from source to target. |
| Clone Graph | Medium | Given a reference to a node in a connected undirected graph, return a deep copy of the entire graph. |
| Employee Importance | Medium | Given a list of employees with importance values and subordinate lists, return the total importance of a given employee and all their subordinates. |
| Is Graph Bipartite? | Medium | Given an undirected graph, determine whether it can be partitioned into two sets such that every edge connects nodes in different sets. |
| Making A Large Island | Hard | Given an n x n binary grid, change at most one 0 to 1 and return the size of the largest island. |
| Number of Islands | Medium | Given a 2D binary grid representing a map of land and water, return the number of islands formed by horizontally or vertically adjacent land cells. |
| Open the Lock | Medium | Given a lock with 4 circular wheels and a list of deadends, find the minimum number of turns to reach the target combination from 0000. |
| Pacific Atlantic Water Flow | Medium | Given a rectangular island height map, find all cells from which water can flow to both the Pacific and Atlantic oceans. |
| Rotting Oranges | Medium | Given a grid of fresh and rotten oranges, determine the minimum time for all fresh oranges to become rotten, or return -1 if impossible. |
| Shortest Path in a Grid with Obstacles Elimination | Hard | Find the shortest path in a grid from top-left to bottom-right, with the ability to eliminate up to k obstacles. |
| Surrounded Regions | Medium | Given a matrix of 'X' and 'O', capture all regions of 'O' that are completely surrounded by 'X' by flipping them to 'X'. |
| Time Needed to Inform All Employees | Medium | Given a company hierarchy and inform times, find the total time needed for the head to inform all employees. |
| Word Ladder | Hard | Given two words and a dictionary, find the length of the shortest transformation sequence where each step changes one letter. |
| Exercise | Difficulty | Description |
|---|---|---|
| Course Schedule II | Medium | Given a number of courses and their prerequisites, return a valid ordering to complete all courses, or an empty array if impossible. |
| Find Eventual Safe States | Medium | Given a directed graph, return all nodes where every possible path leads to a terminal node. |
| Minimum Height Trees | Medium | Given an undirected tree of n nodes, find all root labels that produce trees with the minimum possible height. |
| Sort Items by Groups Respecting Dependencies | Hard | Sort n items so that items in the same group are adjacent and all dependency constraints are satisfied. |
| Exercise | Difficulty | Description |
|---|---|---|
| Accounts Merge | Medium | Given a list of accounts where each account has a name and a list of emails, merge accounts that share a common email and return the merged result. |
| Minimize Malware Spread | Hard | Given a network of nodes and a list of initially infected nodes, find the node whose removal from the infected list minimizes the total spread of malware. |
| Number of Provinces | Medium | Given an n x n adjacency matrix representing direct connections between cities, return the total number of provinces (groups of directly or indirectly connected cities). |
| Redundant Connection | Medium | Given a graph that started as a tree with one extra edge added, find an edge that can be removed so the resulting graph is a tree. |
| Exercise | Difficulty | Description |
|---|---|---|
| Min Cost to Connect All Points | Medium | Find the minimum cost to connect all points on a 2D plane using Manhattan distance. |
| Exercise | Difficulty | Description |
|---|---|---|
| Cheapest Flights Within K Stops | Medium | Find the cheapest price from a source city to a destination with at most k stops. |
| Network Delay Time | Medium | Find the minimum time for a signal to reach all nodes in a network of directed weighted edges. |
| Path with Maximum Probability | Medium | Find the path with the maximum probability of success between two nodes in an undirected weighted graph. |
| Path With Minimum Effort | Medium | Find the route from top-left to bottom-right of a grid that minimizes the maximum absolute height difference between consecutive cells. |
| Swim in Rising Water | Hard | Find the minimum time to swim from the top-left to the bottom-right of a grid where the water level rises over time. |
| Exercise | Difficulty | Description |
|---|---|---|
| Cracking the Safe | Hard | Find a minimum-length string that contains every possible n-digit password over a k-symbol alphabet as a substring. |
| Reconstruct Itinerary | Hard | Given a list of airline tickets represented as pairs of departure and arrival airports, reconstruct the itinerary in order starting from JFK. |