How One Artist Learned to Let His Talents Flow

You may have heard the story of an artist who struggled for years to bring out his talent, only to find it emerged of its own accord once he let go of his ego and surrendered to the creative force…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Problem Solving Techniques Every Developer Should Know

Problem solving is the meta skill. As a software developer, you encounter problems more frequently than you might like, from pesky little bugs to implementing complex features. There are usually more than one way to tackle these problems and as you must have learned through experience, some ways are better than others. A good developer is concerned about the more effective way of solving problems. Effective problem solving involves developing a system and practicing to mastery.

In this post, I will be sharing with you some problem solving techniques suitable for common coding problems. A strategy often used in math is studying common patterns that apply to many problems. In like manner, coding problems can be solved through mastery of some techniques applicable to common challenges.

Note: This is not a sure guide to solving all coding problems. I try to show you tried strategies developed from recurring patterns. The code snippets used are written in JavaScript. They simply illustrate patterns — edge cases are not considered to keep it simple.

This strategy involves collecting the frequency of values using objects or sets. When working with lists or strings, this technique can help you avoid the need for nested loops or O(n²) operations.

You could attempt solving the problem by checking each letter in string a against string b. The time complexity of this approach is O(n²). This means as the length of the strings grow, the time needed to execute the function increases exponentially. Although this could pass as a solution, it’s not an efficient one. A better approach to solving this would be using a counter to record the frequency of each letter in a, then comparing the record against string b. This solution trades space for speed — It offers a faster time complexity of O(n) at the expense of a higher space complexity.

This technique involves creating a “window” which can be a subarray or two numbers representing two positions in an array or string. Depending on the circumstances, the window broadens or closes, and a new window is created.

If tasked with counting the number of unique items in a sorted array, an easy solution using the sliding window approach will be:

While iterating over the array, the window closes and restarts when a new value is found. Each time the window closes, the count variable is updated. The time complexity for this solution is O(n).

Multiple pointers work with strings and linear data structures. It involves creating pointers or values that correspond to indexes or positions. These pointers move towards the beginning, middle or end based on certain conditions. This technique is useful for solving problems with low space and time complexity.

The previous problem can be solved using two pointers.

This is a more popular problem solving technique. Its easier to see at first glance how this method might be useful in solving problems. Like the name implies, it involves dividing a dataset into smaller chunks and repeating a process with those subsets of data. A problem is broken down into smaller problems repeatedly until the subproblems become simple enough to be solved.

This pattern can tremendously improve time complexity and is very useful for searching and sorting.

Sample Problem: Given a sorted array arr, return the index of an element n in the array or return -1 if the element n is not in the array.

Why don’t you give it a go before you read further.

Solution: If your first instinct was to loop through the array, you’re not alone, and you’re not wrong. But hear me out…

If you were to look up a word in a large printout dictionary, you wouldn’t flip through all the pages from cover to cover. However, because you know that the entries in dictionaries are sorted, you take advantage of that to search.

Searching for a word in a dictionary using the divide and conquer technique will involve the following steps:

The same logic can be applied to our sample problem:

It is a good idea to spend time practicing using these techniques in solving problems. This will help you build confidence and problem solving skills. It is helpful to talk through your process out loud to get comfortable with the ideas.

Next time you’re faced with a coding challenge, take a deep breath, then try to visualize your solution using the array of techniques in your arsenal.

Add a comment

Related posts:

Delhi Police Constable Coaching

Dreaming of joining the esteemed Delhi Police force? Unlocking your true potential and achieving success in the Delhi Police exams requires dedication, hard work, and the right guidance. While…

Practitioner Interview with Beth Alston

In a town of about 15,400, Americus, GA proves not to be shy of ground-breaking news. Beth Alston, editor and publisher of the Americus-Times Recorder, informed me that despite running a small-town…