r/learnprogramming • u/munro9443 • Mar 24 '18
I would like to share 500 Data Structure and Algorithms problems
40
35
u/serianx Mar 24 '18 edited Mar 24 '18
I'm also working on a repo full of algorithms solved in many languages (including python, javascript, ruby, go, julia, etc). If you feel like contributing please join us on github! link
7
u/darthyodaX Mar 24 '18
This is awesome! I’ll try to contribute C/C++ when I can. Thanks for sharing this.
2
-33
u/Xeverous Mar 24 '18
Please don't contribute C/C++ abonimation. Write either proper C or C++. Mixing both is bad
8
u/yoctometric Mar 24 '18
I think they might have meant C or C++. I dunno I’m super beginner
2
u/Xeverous Mar 25 '18
Usually yes, and it should be written this way. These are 2 different languages, the only valid terms for "C/C++" would be compability and community. Speaking "C/C++" as 1 language is just ignorance of people who do not know differences
3
u/YoloIsuppose Mar 25 '18
I don't know if you know it but there's this one already which is pretty big, maybe consider contributing to it instead?
2
u/shingtaklam1324 Mar 25 '18
I'll try and do some in Rust when I'm free
1
u/serianx Mar 25 '18
d do some in Rust when I'm free
That'd be awesome, thanks!
1
u/shingtaklam1324 Mar 25 '18
Actually, giving it some more thought, I think it may be easier to have the Rust ones in a separate repo with the way that Cargo integrates into the build process
1
11
u/Rafat_lne Mar 24 '18
wow, I like how every problem has own visual solution. I don't have to go another website to understand those problems.
Can you add some math related algorithms/problems?
edit: typo
7
u/shiprian Mar 24 '18
Not crashed . Just a feature called error connecting to database. We are all programmers here.
6
6
u/GustavDitters Mar 24 '18
If I can solve all of these will I get a job?
10
u/serianx Mar 24 '18
you will get a job long before you finish them all
3
1
-6
u/Iggyhopper Mar 24 '18
Logically, I will get a job if I finish one and not all.
So I finished one, where is my job?!
6
u/persiepanthercat Mar 24 '18
I have a midterm in a week and a half on this and you might have just helped me get a better grade.
16
Mar 24 '18
Error establishing a database connection
Also, implementations in C and Java aren't that useful because they're not all that different. F# or Python and Java would be more reasonable or so.
7
u/darthyodaX Mar 24 '18 edited Mar 24 '18
2
1
1
u/Xeverous Mar 25 '18
Neither implementations in C++. They are almost straight-copied C. Writers make a feeling that C++ does not have standard library
5
4
3
3
u/The_Larger_Fish Mar 24 '18
I've been working through all these! I've done about a hundred so far and find them really helpful. There isn't actually 500 unique problems. For example, the binary tree problems shares a lot of questions with stacks and queues. But they're still good questions!
3
5
5
u/iamagupta Mar 24 '18
Saved, upvoted, was gonna give gold but too bad I don't have a job right now.
6
2
2
2
2
2
2
u/prakrieti Mar 24 '18
Good problems, definitely gonna help the students for the tech Interview! I hope to try most of them. I like the trie section.
2
u/petak5 Mar 24 '18
Looks like your database server is down: Error establishing a database connection
2
2
u/parion Mar 24 '18
Thanks man! I have an interview on Thursday and it's been years since I did any data structures and algorithms problems. This will be a good refresher, thanks!
2
2
2
2
2
2
2
2
u/anasrchid Mar 24 '18
i was thinking about solving some exercises and then this post showed up, THANKS!
2
2
2
2
Mar 25 '18
"Yea I'm into programming as a hobby in my spare time."
> see websites like this with algorithms on them
"Fuck I have no idea where I'd even begin to tackle that problem."
Seriously, I feel like a pretty competent programmer, but I have no idea how I would go about getting anything done in a professional setting.
3
2
2
2
1
u/Rafat_lne Mar 24 '18
wow, I like how every problem has own visual solution. I don't have to go another website to understand those problems. Can add some math related algorithms/problems?
1
1
u/gigastack Mar 24 '18
Instead of an algorithm problem, I get a resources problem - can’t establish a connection to the database. Looking forward to checking this out in the future though.
1
1
1
1
1
u/geek_007 Aug 16 '18
Dynamic Programming:
Find size of largest square sub-matrix of 1’s present in given binary matrix
Find the minimum cost to reach last cell of the matrix from its first cell
Find longest sequence formed by adjacent numbers in the matrix
Count number of paths in a matrix with given cost to reach destination cell
Coin Change Problem – Find total number of ways to get the denomination of coins
Count number of times a pattern appears in given string as a subsequence
Collect maximum points in a matrix by satisfying given constraints
Count total possible combinations of N-digit numbers in a mobile keypad
Graphs:
Breadth First Search - BFS | Iterative & Recursive Implementation
Depth First Search - DFS | Iterative & Recursive Implementation
Minimum number of throws required to win Snake and Ladder game
Total paths in given digraph from given source to destination having exactly m edges
Determine if an undirected graph is a Tree - Acyclic Connected Graph
Chess Knight Problem – Find Shortest path from source to destination
Check if given Graph is Strongly Connected or not using one DFS Traversal
Union-Find Algorithm for Cycle Detection in undirected graph
Find Cost of Shortest Path in DAG using one pass of Bellman-Ford
Find maximum cost path in graph from given source to destination
1
u/geek_007 Aug 16 '18
Heap:
Linked List:
Insert given node into the correct sorted position in the given sorted linked list
Split the nodes of the given linked list into front and back halves
Move front node of the given list to the front of the another list
Split given linked list into two lists where each list containing alternating elements from it
Construct a linked list by merging alternate nodes of two given lists
Merge alternate nodes of two linked lists into the first list
Delete every N nodes in a linked list after skipping M nodes
Detect Cycle in a linked list - Floyd’s Cycle Detection Algorithm
Rearrange the linked list so that it has alternating high, low values
Rearrange a Linked List by Separating Odd Nodes from the Even Ones
Calculate height of a binary tree with leaf nodes forming a circular doubly linked list
Matrix:
Find Shortest path from source to destination in a matrix that satisfies given constraints
Change all elements of row i and column j in a matrix to 0 if cell i,j has value 0
Replace all occurrences of 0 that are not surrounded by 1 in a binary matrix
In-place rotate the matrix by 90 degrees in clock-wise direction
Count negative elements present in sorted matrix in linear time
Report all occurrences of an element in row wise and column wise sorted matrix in linear time
Calculate sum of all elements in a sub-matrix in constant time
Find probability that a person is alive after taking N steps on the island
Find all common elements present in every row of given matrix
1
u/geek_007 Aug 16 '18
Stack:
Queue:
String:
Check if repeated subsequence is present in the string or not
Check if strings can be derived from each other by circularly rotating them
Find all binary strings that can be formed from given wildcard pattern
Find all possible combinations of words formed from mobile keypad
Find all possible combinations by replacing given digits with characters of the corresponding list
Find all words from given list that follows same order of characters as given pattern
Find first k non-repeating characters in a string in single traversal
Inplace remove all occurrences of ‘AB’ and ‘C’ from the string
Find the longest substring of given string containing k distinct characters
Find all substrings of a string that are permutation of a given string
Find the longest substring of given string containing all distinct characters
Iterative Approach to find Permutations of a String in C++ and Java
Generate all Permutations of a String in Java | Recursive & Iterative
Find all lexicographically next permutations of a string sorted in ascending order
Find all strings of given length containing balanced parentheses
Find all N-digit binary numbers having more 1’s than 0’s for any prefix
Find all N-digit binary numbers with k-bits set where k ranges from 1 to N
Find all combinations of non-overlapping substrings of a string
Calculate rank of given string among all its lexicographically sorted permutations
Find all N-digit binary numbers with equal sum of bits in its two halves
Check if given string is interleaving of two other given strings
In-place remove all adjacent duplicates from the given string
Find the minimum number of inversions needed to make the given expression balanced
Construct the longest palindrome by shuffling or deleting characters from a string
Determine if characters of a String follows a specified order or not
Print all combinations of phrases that can be formed by picking words from each of the given lists
Break a string into all possible combinations of non-overlapping substrings
1
u/geek_007 Aug 16 '18 edited Aug 16 '18
Binary:
Bit Hacks – Part 3 - Playing with rightmost set bit of a number
Bit Hacks – Part 4 - Playing with letters of English alphabet
Bit Hacks – Part 5 - Find absolute value of an integer without branching
Find the minimum or maximum of two integers without using branching
Find two odd occurring element in an array without using any extra space
Perform Division of two numbers without using division operator
Check if adjacent bits are set in binary representation of a given number
Circular shift on binary representation of an integer by k positions
Solve given set of problems without using multiplication or division operators
Trie:
Memory efficient Trie Implementation using Map | Insert, Search and Delete
Find first k maximum occurring words in given set of strings
Puzzles:
Clock angle problem – Find angle between hour and minute hand
Implement power function without using multiplication and division operators
Find numbers represented as sum of two cubes for two different pairs
Print all numbers between 1 to N without using any loop | 4 methods
Print a semicolon without using semicolon anywhere in the program
Multiply two numbers without using multiplication operator or loops
Find square of a number without using multiplication and division operator | 3 methods
Find if a number is even or odd without using any conditional statement
Find minimum number without using conditional statement or ternary operator
Perform Division of two numbers without using division operator
Return 0, 1 and 2 with equal Probability using the specified function
Generate numbers from 1 to 7 with equal probability using specified function
Implement Ternary Operator Without Using Conditional Expressions
Determine if two integers are equal without using comparison and arithmetic operators
Return 0 and 1 with equal Probability using the specified function
Generate Random Input from an Array according to given Probabilities
1
u/geek_007 Aug 16 '18
Binary Tree:
Find next node in same level for given node in a binary tree
Combinations of words formed by replacing given numbers with corresponding alphabets
Determine if given binary tree is a subtree of another binary tree or not
Check if binary tree can be converted to another by doing any no. of swaps of left & right child
Print all paths from root to leaf nodes in given binary tree
Find the distance between given pairs of nodes in a binary tree
Print nodes in vertical order of a given Binary Tree - Vertical Traversal
In-place convert convert given Binary Tree to Doubly Linked List
Convert given binary tree to full tree by removing half nodes
Truncate given binary tree to remove nodes which lie on a path having sum less than K
Convert normal binary tree to Left-child right-sibling binary tree
Convert a Binary Tree to BST by maintaining its original structure
Print leaf to root path for every leaf node in a binary tree
Find all nodes at given distance from leaf nodes in a binary tree
Count all subtrees having same value of nodes in a binary tree
Find Maximum Difference Between a Node and its Descendants in a Binary Tree
Calculate height of a binary tree with leaf nodes forming a circular doubly linked list
BST:
Check if given keys represents same BSTs or not without building the BST
Find Lowest Common Ancestor of two nodes in a Binary Search Tree
Convert a Binary Tree to BST by maintaining its original structure
Remove nodes from BST that have keys outside the valid range
Replace every element of an array with the least greater element on its right
1
u/geek_007 Aug 16 '18
Binary Tree:
Find next node in same level for given node in a binary tree
Combinations of words formed by replacing given numbers with corresponding alphabets
Determine if given binary tree is a subtree of another binary tree or not
Check if binary tree can be converted to another by doing any no. of swaps of left & right child
Print all paths from root to leaf nodes in given binary tree
Find the distance between given pairs of nodes in a binary tree
Print nodes in vertical order of a given Binary Tree - Vertical Traversal
In-place convert convert given Binary Tree to Doubly Linked List
Convert given binary tree to full tree by removing half nodes
Truncate given binary tree to remove nodes which lie on a path having sum less than K
Convert normal binary tree to Left-child right-sibling binary tree
Convert a Binary Tree to BST by maintaining its original structure
Print leaf to root path for every leaf node in a binary tree
Find all nodes at given distance from leaf nodes in a binary tree
Count all subtrees having same value of nodes in a binary tree
Find Maximum Difference Between a Node and its Descendants in a Binary Tree
Calculate height of a binary tree with leaf nodes forming a circular doubly linked list
BST:
Check if given keys represents same BSTs or not without building the BST
Find Lowest Common Ancestor of two nodes in a Binary Search Tree
Convert a Binary Tree to BST by maintaining its original structure
Remove nodes from BST that have keys outside the valid range
Replace every element of an array with the least greater element on its right
1
u/geek_007 Aug 16 '18 edited Aug 16 '18
Backtracking:
Find path from source to destination in a matrix that satisfies given constraints
Find total number of unique paths in a maze from source to destination
Print all k-colorable configurations of the graph - Vertex coloring of graph
Find all binary strings that can be formed from given wildcard pattern
Find ways to calculate a target from elements of specified array
Sorting:
Divide & Conquer:
Find first or last occurrence of a given number in a sorted array
Count occurrences of a number in a sorted array with duplicates
Find Minimum and Maximum element in an array using minimum comparisons
Efficiently implement power function | Recursive and Iterative
Find Minimum and Maximum element in an array by doing minimum comparisons
Find Frequency of each element in a sorted array containing duplicates
Greedy:
1
u/geek_007 Aug 16 '18
Binary Tree:
Find next node in same level for given node in a binary tree
Combinations of words formed by replacing given numbers with corresponding alphabets
Determine if given binary tree is a subtree of another binary tree or not
Check if binary tree can be converted to another by doing any no. of swaps of left & right child
Print all paths from root to leaf nodes in given binary tree
Find the distance between given pairs of nodes in a binary tree
Print nodes in vertical order of a given Binary Tree - Vertical Traversal
In-place convert convert given Binary Tree to Doubly Linked List
Convert given binary tree to full tree by removing half nodes
Truncate given binary tree to remove nodes which lie on a path having sum less than K
Convert normal binary tree to Left-child right-sibling binary tree
Convert a Binary Tree to BST by maintaining its original structure
Print leaf to root path for every leaf node in a binary tree
Find all nodes at given distance from leaf nodes in a binary tree
Count all subtrees having same value of nodes in a binary tree
Find Maximum Difference Between a Node and its Descendants in a Binary Tree
Calculate height of a binary tree with leaf nodes forming a circular doubly linked list
BST:
Check if given keys represents same BSTs or not without building the BST
Find Lowest Common Ancestor of two nodes in a Binary Search Tree
Convert a Binary Tree to BST by maintaining its original structure
Remove nodes from BST that have keys outside the valid range
Replace every element of an array with the least greater element on its right
1
u/geek_007 Aug 16 '18
Array:
Find maximum length sub-array having equal number of 0’s and 1’s
Find index of 0 to replaced to get maximum length sequence of continuous ones
Replace each element of array with product of every other element without using / operator
Find maximum difference between two elements in the array by satisfying given constraints
Find all distinct combinations of given length with repetition allowed
Find maximum sequence of continuous 1’s formed by replacing at-most k zeroes by ones
Find the length of smallest subarray whose sum of elements is greater than the given number
Find the smallest window in array sorting which will make the entire array sorted
Maximum profit earned by buying and selling shares any number of times
Find minimum platforms needed in the station so to avoid any delay in arrival of any train
Length of longest continuous sequence with same sum in given binary arrays
Rearrange array such that A* [A* [i]] is set to i for every element A* [i]
Find pairs with given difference k in the array | Constant space solution
Print all quadruplets with given sum | 4-sum problem extended
Find two odd occurring element in an array without using any extra space
Replace each element of the array by its corresponding rank in the array
Print all Triplets in an array with sum less than or equal to given number
Find minimum difference between index of two given elements present in the array
Find maximum absolute difference between sum of two non-overlapping sub-arrays
Find count of distinct elements in every sub-array of size k
Find ways to calculate a target from elements of specified array
Generate Random Input from an Array according to given Probabilities
Find Index of Maximum Occurring Element with Equal Probability
1
u/geek_007 Aug 16 '18
Array
Find Minimum Product among all Combinations of Triplets in an Array
Replace every element of an array with the least greater element on its right
Find all odd occurring elements in an array having limited range of elements
Print all combinations of positive integers in increasing order that sum to a given number
Find smallest range with at-least one element from each of the given lists
Custom Sort | Sort elements of the array by order of elements defined by the second array
Find first or last occurrence of a given number in a sorted array
Count occurrences of a number in a sorted array with duplicates
Find Minimum and Maximum element in an array using minimum comparisons
Coin Change Problem - Total number of ways to get the denomination of coins
Combinations of words formed by replacing given numbers with corresponding alphabets
Decode the given sequence to construct minimum number without repeated digits
Find Floor and Ceil of a number in a sorted array - Recursive solution
Find two duplicate elements in an limited range array - using XOR
Find Minimum and Maximum element in an array by doing minimum comparisons
Find Frequency of each element in a sorted array containing duplicates
1
u/geek_007 Aug 16 '18
Popular algorithms and data structures:
1
0
u/b_curious Mar 25 '18
There are already thousands of resources for finding such problems, what's new? it would be better if there were some cohorts to help each other and motivation within group. I have tried to create such group but hardly got any traction, its sad that such posts receive such high votes.
-1
u/Xeverous Mar 24 '18 edited Mar 24 '18
The C++ implementation of KMP algorithm (link) does not compile (array size is not compile time). Algorithm is implemented very poorly (unnecessary memory allocations and copies and no iterators). This looks like someone copy-pasted C solution, added misused standard string and called it "C++". The writer did not knew STL
0
Mar 24 '18
[deleted]
10
u/PathToTheLight Mar 24 '18
8 hours later still down, smh
16
u/ScrumptuousLick Mar 25 '18 edited Mar 25 '18
How to royally fuck a website;
1: copy url of site. 2: post in relevant subreddit 3: ? 4: profit.
14
u/im_not_afraid Mar 25 '18
How to royally fuck a list;
1: make list 2: don't put double spaces to make newlines 3: use old ??? profit meme 4: ??? 5: profit
6
3
2
3
0
0
Mar 24 '18
[deleted]
2
u/SweetOnionTea Mar 24 '18
Nah, but understanding why a particular algorithm is faster or coming up with your own solutions may be difficult without some math or cs background. I would check out a course in discrete math to get a good background to analyze algorithms.
1
54
u/[deleted] Mar 24 '18
Dude I think about how I need to study data structures and algorithms and then this post shows up. Thank you so much OP.