Intuitive Programming: Tips that no one tells you about coding!

Have you ever wondered about what kind of problems can be solved using which kind of data structures and algorithms? This article will answer all your questions!

--

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

Source

In this article, you will come across tips for becoming a better and smarter programmer. This is ideal for both newbies and experienced programmers alike.

  1. If you are asked to find the minimum number of steps to reach a position, you can think to use Breadth-First Search.
  2. If you are asked whether a path exists or not, using Depth First Search or Breadth-First Search are good options.
  3. If the problem talks about connecting different places, cities, nodes, positions, etc, there is a very high probability that you can use a Graph to represent this.
  4. If your problem talks about having shared subproblems, you can think of using Dynamic Programming or recursion with memoization.
  5. If you are asked to find the k-largest elements, using a max heap will optimize your solution. Just add all elements of the given list or array to a max heap and pop them k-times.
  6. If you are asked to find the k-smallest elements, using a min-heap will optimize your solution. Just add all elements of the given list or array to a min-heap and pop them k-times.
  7. If you see a problem containing N-ary Trees or Binary Trees, using traversal techniques such as Inorder, PostOrder, PreOrder or Level Order Traversal helps solve most questions.
  8. If you are given a multi-dimensional array or matrix, you can perform DFS or BFS to solve most problems.
  9. If you are working with linked lists, using a separate list to store either the node or value, helps us sort the list, reverse the list, and do other similar problems. You can also use pointers to optimize your solution.
  10. You can solve problems that involve finding 2-Sum by either storing the complement in the dictionary of a hash map or by using two pointers in a sorted list.
  11. If you are given a problem that contains a list or array, and you need to find a sum of overlapping subarrays, you can use prefix sum to store the sum beforehand or use dynamic programming to optimize your solutions.
  12. When you see a question that uses BFS, you can always use deque, but you can also think of using min or max heap if necessary.
  13. If you want solve problems in which you need to resolve prerequisites, dependencies, etc, and decide whether an operation is possible or not, then using Graph and then finding whether it has a cycle helps.
  14. If you want to find the number of isolated nodes, etc, you can find number of components of a Graph and return it.
  15. In order to find if a graph can be made into a valid tree, you can either detect a cycle or check if the number of edges is one less than the number of nodes and the number of components is equal to 1.
  16. If you need to search for an element by taking a specific number of steps (whether fixed or variable), dfs or bfs is an option. Example: Frog Jump
  17. Topological Sorting can be used in problems that consist of prerequisites, scheduling, etc. Any problem that gives preference to nodes with 0 in degree first and then their children. In other words, if traversing a parent first is necessary and only after that you can traverse the child, topological sorting becomes a good choice.
  18. If you want to find a unique number in a list, you can take XOR of all elements with a number = 0 (^), like in this problem (https://leetcode.com/problems/single-number/). The only number remaining after all XORs, is the unique element. Also, the property of XOR is that the XOR of two same elements is always 0.

I hope some of these tricks help you in your programming journey. Please note that this list is in no way exhaustive and there are always more optimized ways of solving these problems. This article merely lists a few of the many tricks that can help you get started and become a great problem solver.

If you have any tricks that you find useful which are not present here, then please feel free to drop a comment, so more and more people can learn from you!

Please like, comment, and share the article if you liked it. You can also follow me for more articles on programming, data science, machine learning and NLP.

Thank you so much for reading! 😊

--

--