C++ DSA Problems

Note: Questions marked with the (for practice) tag do not include the exact solutions. The provided code solutions in this section serve as hints or are solutions to similar problems from platforms like LeetCode, GeeksforGeeks, or HackerRank....

Report an Issue

Basics of Programming

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Say "Hello, World" with c++(for practice) Easy
Basic Data Types(for practice) Easy
Input and Output(for practice) Easy
Conditional Statements(for practice) Easy
Switch Case Statement(for practice) Easy
Number System Conversion(binary to decimal and decimal to binary) Easy
Prime Number(for practice) Basic
Factorial(for practice) Basic
Reverse Integer Medium
Set Kth Bit Basic
Convert The Temperature Easy
Count all Set Bits Basic
Create Number Using Digits Easy

Patterns I

Question Status Mark for revision Code Solution
Solid Square Pattern
Solid Rectangle Pattern
Hollow Square Pattern
Hollow Rectangle Pattern
Half Pyramid Pattern
Hollow Half Pyramid Pattern
Inverted Half Pyramid Pattern
Inverted Hollow Half Pyramid Pattern
Numeric Half Pattern
Inverted Numeric Half Pyramid Pattern
Full Pyramid Pattern
Inverted Full Pyramid Pattern
Diamond Pattern
Hollow Pyramid Pattern
Inverted Hollow Pyramid Pattern

Patterns II

Question Status Mark for revision Code Solution
Hollow Diamond Pattern
Mixed Pyramid Pattern
Fancy 12 Pattern
Full Fancy 12 Pattern
ABCBA Pattern
Numeric Hollow Half Pyramid Pattern
Numeric Hollow Inverted Half Pyramid Pattern
Numeric Palindrome Equilateral Pyramid Pattern
Solid Half Diamond Pattern
Fancy Pattern 1
Fancy Pattern 2
Fancy Pattern 3
Floyd's Triangle Pattern
Pascal's Triangle Pattern
Butterfly Pattern

Arrays I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Linear Search Algorithm(for practice) Basic
Find max/largest element in array(for practice) Basic
Count the Zeros(for practice) Easy
Swap Two Numbers(for practice) Basic
Reverse Array(for practice) Beginner
Single Number Easy
Two Sum Easy
Three Sum Easy
Rotate Array Medium
Search an element in 2D array Easy
Find minimum value in 2d array Easy
Find maximum value in 2d array Easy

Arrays II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Row Wise Sum in 2D array -
Column Wise Sum in 2D array -
Diagonal Sum in 2D array(for practice) Basic
Transpose of 2D matrix (for practice) Easy
Remove Duplicates from Sorted Array Easy
Find Pivot Index Easy
Missing Number Easy
Maximum Average Subarray I Easy
Sort Colors Medium
Move Negative Nos. to Left of Array/ Dutch National Flag Algorithm Easy

Arrays III

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Find Duplicate Number Medium
Missing Elements from an array with duplicates -
First Repeating Elements(for practice) Easy
Common in 3 sorted arrays(for practice) Easy
Wave Print a Matrix -
Spiral Matrix Medium
Add two numbers represented by two arrays(for practice) Easy
Factorial of Large Numbers Medium
Two's Complement -
Rotate Image Medium
Maximum Subarray(Kadane Algorithm) ⭐⭐⭐ 👩🏻‍💻 Medium

Searching and Sorting I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Binary Search Easy
Find First and Last Position of Element in Sorted Array Medium
Missing Number(using binary search) Easy
Peak Index in a Mountain Array Medium
Search in Rotated Sorted Array(Pivot Index) Medium
Sqrt(x) (Search space pattern) Easy
Search a 2D Matrix Medium
Find Quotient -
Search in a nearly Sorted Array -
Single Element in a Sorted Array Medium
Odd Occurring Element -
Square root with precision -
Binary Search with precision -
Divide Two Integers Medium

Searching and Sorting Algorithms II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Bubble Sort(practice) Easy
Selection Sort(practice) Easy
Insertion Sort(practice) Easy
K-diff Pairs in an Array Medium
Find K Closest Elements Medium
Exponential Search and Unbounded Binary Search -
Allocate Minimum Pages(Book Allocation Problem) Medium
The Painter's Partition Problem-II Hard
Aggressive Cows Hard
EKO spoj -
PRATA spoj -
Koko Eating Bananas Medium
Minimum Number of Days to Make m Bouquets Medium

Char Arrays and Strings I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Remove all Adjacent Duplicates in a String Easy
Remove all occurrences of a substring Medium
Valid Palindrome II Easy
Palindromic substrings Medium
Decode the message Easy
Minimum Amount of time to collect garbage Medium
Custom sort string Medium
Find and replace pattern Medium
Valid Anagram Easy
Reverse only letters Easy
Longest common prefix Easy
Reverse vowels of a string Easy
Isomorphic strings Easy
Reorganize strings Medium
Group anagrams Medium

Char Arrays and Strings II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Longest Palindromic Substring Medium
Find the index of the first occurrence in a string Easy
String to integer (atoi) Medium
String Compression Medium
Integer to roman Medium
Zigzag conversion Medium
Largest Number Medium
Minimum time difference Medium
Number of laser beams in a bank Medium
Remove all adjacent duplicates in string II Medium
Sum of beauty of all substrings Medium
Sherlock and Anagrams Medium
Reverse words in a string Medium
Text Justification Hard

Basic Maths and Pointers

Question Link for practice Difficulty Level Status Mark for revision Code Solution
C++ Pointers ( Incrementing a variable) - (for practice) Basic
Count Primes Medium
Fast Exponentiation -
GCD of two numbers Basic
Modular exponentiation for large numbers Medium
Sieve of eratosthenes -
Segmented Sieve -
Product of primes Medium

Recursion I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Factorial(for practice) Basic
Fibonacci Number(practice) Easy
Basic array operations using recursion Easy
Binary Search using recursion Easy
Print Subsequence using recursion -
House Robber (optimise using DP) Medium
Coin Change (optimise using DP) Medium
Last occurrence of character using recursion -
Reverse a string recursively -
Add Strings (recursively) Easy
Check palindrome recursively -
Print all subarrays recursively -
Remove all occurrences of a substring Medium
Best Time to Buy and Sell Stock ⭐ Easy
Integer to English Words Hard

Recursion II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Wildcard Matching (optimise using DP) Hard
Number of Dice rolls with Target Sum (optimise using DP) Medium
Perfect Squares (optimise using DP) Medium
Minimum Cost for Tickets (optimise using DP) Medium
House Robber II (optimise using DP) Medium
Count Derangements (Permutation such that no element appears in its original position) -
Painting the Fence Medium
Edit Distance (optimise using DP) Medium
Maximal Square (optimise using DP) Medium
Partition Equal Subset Sum (optimise using DP) Medium
Minimize the difference between target and chosen elements (optimise using DP) Medium
Jump Game (optimise using DP) Medium
Jump Game II (optimise using DP) Medium
Triangle (optimise using DP) Medium

Backtracking and DnC I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Merge Sort / Divide and conquer (for practice) Medium
Quick Sort Medium
Rat in a Maze Problem - I Medium
Permutations Medium
N Queens Hard
Sudoku Solver Hard
Letter combinations of a phone number Medium
Generate Parentheses Medium
Combinations Medium
Word Search Medium
Matchsticks to Square Medium

Backtracking and DnC II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Merge Sort: Count Inversions Hard
Sort an Array Medium
Merge Two Sorted Arrays Without Extra Space Hard
Maximum Subarray (divide and conquer approach) Medium
Combination Sum Medium
Combination Sum II Medium
Permutations II Medium
Beautiful Arrangement Medium
Distribute Repeating Integers Hard

OOPS Concepts

Question Status Mark for revision Code
How to create objects in Cpp
Encapsulation
Inheritance
Polymorphism
Abstraction
Miscellaneous Concepts

Linked List I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Linked List Insertion At Head / Beginning (for practice) Easy
Print Linked List elements (for practice) Basic
Linked List Insertion At Tail / End (for practice) Easy
Linked List Insertion At Position (for practice) -
Search in Linked List (for practice) Easy
Deletion of Node in Single Linked List (for practice) Easy
Doubly linked list Insertion at given position (for practice) Basic
Delete node in Doubly Linked List (for practice) Easy
Reverse Linked List Easy
Middle of the Linked List Easy
Palindrome Linked List Easy
Check Linked List Cycle Easy

Linked List II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Remove Duplicates from Sorted List Easy
Sort a linked list of 0s, 1s and 2s Easy
Add 1 to a Linked List number Medium
Reverse Nodes in k-Group ⭐⭐⭐👩🏻‍💻 Hard
Add 1 to Linked List using Head Recursion -
Add two numbers represented by linked list Medium
Linked List Cycle II Medium
Split Linked List in Parts Medium
Flatten a Multilevel Doubly Linked List Medium
Remove loop in linked list(practice) Medium
Delete N nodes after M nodes of a linked list Easy
Merge Two Sorted Lists Easy
Get Node Value Easy

Linked List III

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Intersection of Two Linked Lists Easy
Flattening a Linked List Medium
Copy List with Random Pointer Medium
Rotate List Medium
Find the Minimum and Maximum Number of Nodes Between Critical Points Medium
Merge Nodes in Between Zeros Medium
Odd Even Linked List Medium
Double a Number Represented as a Linked List Medium
Remove Zero Sum Consecutive Nodes from Linked List Medium
Swapping Nodes in a Linked List Medium
Sort List (using merge sort) Medium

Stacks I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Stack Implementation (for practice) Easy
Implement two stacks in an array (for practice) Easy
Reverse a string using stack (for practice) Easy
Insert an Element at the Bottom of a Stack (for practice) Easy
Print middle -
Check Sorted -
Reverse a Stack (for practice) Medium
Sorted Insert -
Valid Parentheses Easy
Check Expression contains redundant bracket or not Medium
Next Smaller Element -
Prev Smaller Element -

Stacks II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Largest Rectangle in Histogram Hard
Min Stack Medium
Sum of Subarray Minimums Medium
Sum of Subarray Ranges Medium
Basic Calculator Hard
Minimum Bracket Reversal / Count the Reversals Medium
Remove All Adjacent Duplicates In String (using stack) Easy
The Celebrity Problem Medium
Next Greater Node In Linked List Medium
N stacks in an array (for practice) Hard
Online Stock Span Medium
Check if word is valid after substitutions Medium

Stacks III

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Decode String Medium
Car Fleet Medium
Car Fleet II Hard
Simplify Path Medium
Maximal Rectangle / max rectangle in binary matrix Hard
Daily Temperatures Medium
Remove K Digits Medium
Minimum Add to Make Parentheses Valid Medium
Longest Valid Parentheses Hard
Asteroid Collision Medium
Design Browser History Medium

Queues I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Implement Queue using array (for practice) Basic
Deque Implementations (for practice) Easy
Implement Circular Queue -
Implement Circular Deque -
Reverse a Queue (for practice) Easy
Reverse in K Group -
Interleave the First Half of the Queue with Second Half Medium
First negative in every window of size k Medium
Sliding Window Maximum Hard
First non-repeating character in a stream Medium
Gas Station Medium

Queues II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Flatten Nested List Iterator Medium
Dota2 Senate Medium
Steps by Knight Medium
Implement K queues in an array -
Implement Queue using Stacks Easy
Implement Stack using Queues Easy
Sum of minimum and maximum elements of all subarrays of size k -
Number of Recent Calls Easy
Number of People Aware of a Secret Medium
Find the Winner of the Circular Game Medium
Reveal Cards In Increasing Order Medium

Generic and Binary Trees I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Preorder Traversal (for practice) Basic
Inorder Traversal (for practice) Basic
Postorder Traversal (for practice) Basic
Level Order Traversal (for practice) Easy
Maximum (Height) Depth of Binary Tree Easy
Diameter of Binary Tree Easy
Balanced Binary Tree Easy
Lowest Common Ancestor(LCA) of a Binary Tree ⭐⭐⭐ Medium
Path Sum Easy
Path Sum II Medium
Construct Binary Tree from Preorder and Inorder Traversal Medium

Generic and Binary Trees II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Construct Binary Tree from Inorder and Postorder Traversal Medium
Tree Boundary Traversal Medium
Left View of Binary Tree Easy
Right View of Binary Tree Easy
Top View of Binary Tree Medium
Bottom View of Binary Tree Medium
Maximum Width of Binary Tree Medium
All Nodes Distance K in Binary Tree Medium
Count Complete Tree Nodes Easy
Binary Tree Cameras Hard
Same Tree (Two trees mirror or identical?) Easy
Symmetric Tree Easy

Generic and Binary Trees III

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Binary Tree Zigzag Level Order Traversal Medium
Transform to Sum Tree Easy
Diagonal Traversal of Binary Tree Medium
Vertical Order Traversal of a Binary Tree Hard
Path Sum III (K-sum paths) Medium
Binary Tree Inorder Traversal (Morris Traversal) Easy
Flatten Binary Tree to Linked List Medium
Sum of nodes on longest (bloodline) path from root to leaf node Medium
Maximum sum of Non-adjacent nodes Medium
Burning Tree Hard
Find Duplicate Subtrees Medium

Binary Search Trees I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Build BST and traverse -
Min and max element in BST(for practice) Basic
Search a node in BST Basic
Delete Node in a BST Medium
Two Sum IV - Input is a BST Easy
Binary Search Tree to Greater Sum Tree Medium
Kth Smallest Element in a BST Medium
Lowest Common Ancestor (LCA) of a BST Medium
Convert Inorder Traversal/Sorted Array to BST Easy
Binary Tree to DLL Hard
Convert Sorted linked List to BST Medium
Maximum Sum BST in Binary Tree Hard
Minimum Distance Between BST Nodes Easy
Validate Binary Search Tree Medium

Binary Search Trees II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Binary Search Tree Iterator Medium
Recover Binary Search Tree Medium
Inorder Predecessor of Node in BST Medium
Inorder Successor in BST Easy
Construct BST from Preorder Traversal Medium
Brothers From Different Roots Easy
Balance a Binary Search Tree Medium
Median of BST Easy
Check whether BST contains Dead End Medium
Range Sum of BST /count bst nodes lying in a range Easy
Flatten BST to sorted linked list Medium
Replace every element with the least greater element on its right Medium
Valid BST from Preorder Medium
Merge two BST 's Medium

Heaps I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Heap Creation -
Build Heap and Heap Sort(for practice) Medium
Kth Largest Element in an Array Medium
Check Completeness of a Binary Tree Medium
Is Binary Tree Heap Medium
Minimum Cost of ropes Easy
Merge k Sorted Arrays Medium
Merge k Sorted Lists Hard
Smallest Range Covering Elements from K Lists Hard
Find Median from Data Stream Hard

Heaps II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Top K Frequent Elements Medium
Maximum Score From Removing Stones Medium
Design Twitter Medium
Task Scheduler Medium
Merge two binary Max heaps Easy
K Closest Points to Origin Medium
Get Biggest Three Rhombus Sums in a Grid Medium
Minimum Difference in Sums After Removal of Elements Hard
Minimum Number of Refueling Stops Hard
Sliding Window Maximum Hard

Maps and Tries I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Maps STL Practice -
Two Sum - using Maps Easy
Group Anagrams - using Maps Medium
Substring with Concatenation of All Words Hard
Implement Trie (Prefix Tree) Medium
Longest Common Prefix Easy
Searching using Trie -
Repeated DNA Sequences Medium
Design Add and Search Words Data Structure Medium
Implement Magic Dictionary Medium
Search Suggestions System Medium

Maps and Tries II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Array Subset (of another array) Basic
Union of Two Linked Lists Medium
Intersection of Two Linked Lists Easy
Sum equals to Sum Easy
Largest subarray with 0 sum Medium
Contiguous Array / Largest Subarray of 0s and 1s(gfg) Medium
Replace Words Medium
Top K Frequent Words Medium
Camelcase Matching Medium
Palindrome Pairs Hard

Dynamic Programming I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Fibonacci Number(using dp) Easy
House Robber Medium
Longest Increasing Subsequence Medium
Coin Change Medium
0 - 1 Knapsack Problem Medium
Painting the Fence Medium
Partition Equal Subset Sum Medium
Number of Dice Rolls With Target Sum Medium
Longest Common Subsequence Medium
Maximum Height by Stacking Cuboids Hard
Russian doll envelopes Hard
Guess Number Higher or Lower II Medium
Minimum Cost Tree From Leaf Values Medium

Dynamic Programming II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Wildcard Matching Hard
Regular Expression Matching Hard
Last Stone Weight II Medium
Edit Distance Medium
Perfect Squares Medium
Minimum Cost For Tickets Medium

Graphs I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Print adjacency list (for practice) Easy
BFS of graph (for practice) Easy
DFS of graph (for practice) Easy
Undirected Graph Cycle(Detect cycle) Medium
Directed Graph Cycle(Detect cycle) Medium
Topological sort Medium
Directed Graph Cycle Medium
Single Source Shortest path in Undirected Graph (for practice) Medium
Single Source Shortest path - dfs -
Dijkstra Algorithm (for practice) Medium
Bellman-Ford Algorithm - Distance from the Source (for practice) Medium

Graphs II

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Floyd Warshall (for practice) Medium
Strongly Connected Components (Kosaraju's Algo) (for practice) Medium
Bridge edge in a graph (Tarjan's Algo) (for practice) Medium
Critical Connections in a Network Hard
Course Schedule Medium
Course Schedule II Medium
Word Ladder Hard
Path With Minimum Effort Medium
Number of Provinces Medium
Rotting Oranges Medium

Graphs III

Question Link for practice Difficulty Level Status Mark for revision Code Solution
Cheapest Flights Within K Stops Medium
Longest Cycle in a Graph Hard
Prim's Algorithm - Minimum Spanning Tree (for practice) Medium
Kruskal's Algorithm - Minimum Spanning Tree (for practice) Medium
Eventual Safe States Medium
Word Ladder II Hard
Minimum Multiplications to reach End Medium
Number of Operations to Make Network Connected Medium
Find the City With the Smallest Number of Neighbors at a Threshold Distance Medium
Accounts Merge Medium

Bonus I

Question Link for practice Difficulty Level Status Mark for revision Code Solution
DEFKIN - Defense of a Kingdom - GREEDY ALGORITHM -
Huffman Encoding - GREEDY ALGORITHM Hard
Fractional Knapsack - GREEDY ALGORITHM Medium
N meetings in one room - GREEDY ALGORITHM Easy
Sliding Window Maximum - SLIDING WINDOW Hard
Minimum Size Subarray Sum - SLIDING WINDOW Medium
Two Sum - SLIDING WINDOW Easy
Minimum Window Substring - SLIDING WINDOW Hard
Single Number - BIT MANIPULATION Easy
Subsequence / Power Set - BIT MANIPULATION Medium

Bonus II

Question Status Mark for revision Code Solution
Check even or odd - BIT MANIPULATION
Get ith bit - BIT MANIPULATION
Set ith bit - BIT MANIPULATION
Clear ith bit - BIT MANIPULATION
Update ith bit - BIT MANIPULATION
Clear last i bits - BIT MANIPULATION
Count set bit - BIT MANIPULATION
Check if n is power of 2 - BIT MANIPULATION
Exponentiation using bits - BIT MANIPULATION
Clear bits in a range - BIT MANIPULATION