Best practice of solving algorithms

Osgood Gunawan
4 min readAug 17, 2020

This week I have been doing a lot of algorithm questions in binarysearch.io. For some of you who don’t know binarysearch.io, It is a new platform where you can collaborate with strangers or friends across the country to do algorithm questions together. It is more fun and engaging to do algorithms together than to do it alone. At the same time, you can improve your pair programming skills and help out other debugging codes. If you are still unsure what the difference is between bs .io than any other coding platform, think of Slack and Leetcode having a baby.

Okay, all joking aside, I am here to talk about best practice for solving algorithms. I found this best practice when I was answering an easy question in bs.io. So here is the question:

https://binarysearch.io/problems/Remove-last-duplicate-entries

Understand the Problem

  • Before you try to solve any algorithm question, it is okay not to write code for the first 5–7 mins. Try to understand the problem in the first place. In this example: the question is asking to delete last occurrences for the number of the duplicate. The highlighted words here are duplicates and last occurrences, so as long as there are the same numbers in the array, all we need to do is to delete the last occurrences of that same number in the array.

Plan

  • Plan on how you will solve the question, pseudocode it first if that makes your life easier. We need to worry about any constraints along the way, apply what kind of data structure to solve the problem, different ideas/perspectives to solve the problem such as looping from the back, etc.
  • Since we need to keep track of all the duplicate numbers in the array, we can utilize a hash table to track the number frequency. Once we get each of the number frequencies, we can start looping from the back of the array by deleting the last occurrences duplicate number with the hash references.

Action

  • Now we start writing code and use the plan to approach the problem. One thing to keep in mind, It is okay to solve the question in a brute force way first or not in the best optimal approach yet. Sometimes it is easier to address the problem first, and you can always reverse engineering later to optimize your solution.
Utilize the splice method

Iterate through

  • Before you run the test cases or submit button, make sure you understand what your codes are doing. What value are we returning? Do we hit all the requirements? Any other additional constraints that we need to think of? Iterate your codes once again to understand the bigger picture.

Congrats, you solve the problem! Now what?

  • This practice is a crucial step; now, we can reverse engineering to find a better optimal solution.
  • The built-in .splice() method might make the time complexity O(n²) with for loop.
  • Let’s try a different approach with two hash tables and avoid the splice method.
Utilize two hashes instead of splice method
  • The first hash table would be the same as the previous approach, getting all the frequency numbers in the array. The other hash table indexCount={} keeps track of the duplicates number index of the array. From there, we know the last index occurrence for the duplicate numbers. Lastly, we can create an array (array=[]), loop through the original nums array, and if those duplicates number indexes of the array are not met(in indexCount={}) push the number into the new array we just created.
  • As you can see, here the time complexity is O(n) instead of O(n²)

Summary

  • Try to understand the question first before writing out any code. Take your time to understand what the problem is asking.
  • Check to see if you can find any pattern, constraint, and different data structures to solve the question.
  • Solve the question first with brute force if you think it makes your life easier and reverse engineering later.
  • Before submitting codes, make sure you hit all the constraints that the question asks, run through your thought process of each line of your codes again, check if you hit all the goals that the questions ask.
  • One of the best and essential practices to scale your algorithm skill is to solve the problem, optimize your solution, or use different approaches to address the question. You can use different data structures or other programming languages to solve. Perhaps, you can see other people’s solutions, and discover a new way of approaching the problem. There is nothing to be ashamed of after optimizing your solution and look at other people’s answers on how they approach the problem, as long as you learn something new.

Thanks for taking the time to read, and I hope this post can help the way you practice your problem-solving skills. If you want to practice algorithm questions together, don’t be afraid to add me(@the_goodone) in binarysearch.io!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Osgood Gunawan
Osgood Gunawan

Written by Osgood Gunawan

UX designer | Software Engineer | Dancer | ETL Developer | Data Migration. More about me : https://www.osgood1024.com/

No responses yet

Write a response