After 900 leetcode problems here is what I learned
Why do we practice algorithms? (not just for interview)
Solving Algorithm problems could help train our minds to be used to use data structure and algorithms, and be ready to use them to solve issues.
instead of CRUD works, the leetcode problems usually require a good understanding of data structures like trees, graphs, heap, even though most of them are unlikely to be used in daily work, but when the requirement comes in(pathfinding, shorted path (weight based), graph/tree traverse, reference counting, etc) it will help if we can come out with solutions fast and then compare the pros and cons to get an optimal solution.
so we need practice.
Problem Tags
Below are common tags (sorted by a number of problems being asked during the interview based on my experience). there are many more listed on leetcode
DFS
A classic way to traverse a tree or graph starts from a node reaches until the end. or within a matrix finding the area by going up, down, left, right .usually every tree or graph or 0–1 matrix searching problem could be related to DFS.
BFS
Unlike DFS. BFS could be done with the queue. Find the next reachable nodes, and add them to a queue until the queue is empty. unlike DFS, BFS focuses more on all “next nodes” from the current “parents” queue.
Binary Search
Within a sorted array find some value, that usually falls into this category.
Heap
When finding max or min, or top issue. usually, this structure is available in the language you choose, can directly use it. java, c#, python, etc. if not you can also build your heap.
Trie
Given a dictionary, build the Trie, do a word search or frequency.
Stack
Use min or max stack to compare the top 1 in the stack while looping through some array.
Linklist
common problems are like find cycle, common node between 2 link list, reverse, etc.
Greedy
The idea is to use “greedy thinking” to find the maximum or minimum.
Sliding window
use start and end position to track the “window” start and end position while looping array, need to increase the start when match some condition, usually need to track the window size also
Two pointers
maintain 2 indexes while looping array or string
Back Tracking
like DFS, just that needs to loop through each possibility for each recursion. This approach is only useful for a small number of input values.
Divide and conquer
The idea is to find a partitioner and divide the issue into smaller ones (e.g. left and right), find the answer for each of them, and “merge” the sub-answers. e.g. quick-sort
Union Find
“Group” the parents to the same “root-parent” while finding parent for child node
Dynamic programming
There is an array to store “previous answers”. it may not be easy to come out with this approach the first time. but when we solved some issue with DFS or backtracking, then we may find “some issue solved there should be a cache for these previous answers” . this is when DP comes to be a solution.
Another pattern is “bottom-up”. try to solve the problem with a small number of numbers, see the answer, then try to reuse the previous answer to solve the problem with more numbers added in.
Topo sort
while traverse graph remove the “out degree=0”
Bit manipulation
use bit operation to solve the issue. e.g. bitmask
How to leet the issues?
By Tag
If not sure where to start, on leetcode there are many tags (or started from the list above). clicking on each of them can focus only on those issues.
so that you “reduced” the difficulty level by knowing where should find the answer (e.g. if choose DFS, you already know this issue should be solved by the DFS approach)
Skip the low rating issues first
Those issues with low ratings are usually because of poor description or not related to any algorithm or not a programming issue, e.g. pure math. can skip those issues first if you are not interested.
If you are preparing an interview, focus on medium problems first
I have been interviewed with Facebook, amazon, google, and Microsoft, indeed. all of them give me some medium level leetcode problems, like top (find 5th largest number among millions), read4, longest palindrome str, binary search, graph traverse, Trie, string permutation, etc . you do not have to solve all the problems, but make sure have a good understanding on the problems under the common tags.
A timeline
Give yourself a time limit while solving issues (e.g. 20 mins). this will put on a bit of stress on yourself to come out with answer within the given time, not unlimited.
Discuss
If you are stuck, no worry, there are hundreds or thousands of good answers to each of the issues in the “discuss tab”.
That’s all. Happy leet coding!
If you are still reading me why don’t we connect?
Follow me on Twitter: https://twitter.com/Awaiskhan4040
Follow me on GitHub: https://github.com/Awaiskhan404