Skip to main content

Way to structure/ refactor code

  1. Write down the key points at the top (i.e. sorted array). Make having all the details. Show how organized you are. Better to have an example.
Example
Example
// Given two arrays, create a function that let's a user know (true/false) whether these two arrays contain any common items
// For Example:
// const array1 = ['a', 'b', 'c', 'x'];
// const array2 = ['z', 'y', 'i'];
// should return false.
// -----------
// const array1 = ['a', 'b', 'c', 'x'];
// const array2 = ['z', 'y', 'x'];
// should return true.
  1. Make sure to double check: What are the inputs? What are the outputs?
Example
Example
// 2 parameters - arrays - no size limit
// return true or false
  1. What is the most important value of the problem? Do you have time, and space and memory, etc.. What is the main goal?
Example
Example
// Most important thing is to improve the time complexity
  1. Start with the naive/ brute force approach. First thing that comes into mind. Think well and critically (don't need to write code yet, just organize it).
Example
Example
// O(n^2): Nested for loop
  1. Think deeper why this approach is not the best (i.e. O(n^2) or higher, not readable, etc...)
Example
Example
// Nested for loop is not the best because it is O(n^2)
  1. Walk through the approach, comment things and see where to break things.
    • List out unnecessary work
    • Ensure all information used
    • Bottleneck (This will be the main focus)
  2. Before you start coding, walk through your code and write down the steps you are going to follow.
  3. Modularize the code from the very beginning. Break up your code into beautiful small pieces and add just comments if you need to.
Example
To improve the O(n^2), we can break it down to O(a + b) instead of O(a * b). And list out the steps:
Example
// 1. Loop through the first array and create hashMap
// 2. Loop through the second array and check if item in second array exists on the hashMap
// 3. If it exists in the second array, return true
  1. Start actually writing your code now. the more getting prepared, the better the refactor will go. So never start coding without being sure of how things are going to work out.
Example
Example
const findCommon = (array1, array2) => {
const map = new HashMap();
array1.forEach((item) => {
if (!map.get(item)) {
map.set(item, true);
}
});

array2.forEach((item) => {
if (map.get(item)) {
return true;
}
});
};
  1. Think about error checks and how can break the code. For example passing in the null as arg.

  2. Don’t use bad/confusing names like i and j. Write code that reads well.

  3. Finally, think where you would improve the code. Does it work? Are there different approaches? Is it readable? What would you google to improve? How can performance be improved?

Example

The previous approach solves the O(a * b) problem, but it introduce a new problem of space complexity O(n)

So we might want to think how to improve the space complexity. We can do that by using the built-in includes method.

Example
const findCommon = (array1, array2) => {
return array1.some((item) => array2.includes(item));
};
// Using this approach, we can reduce the space complexity to O(1)
// And we keep the time complexity to O(a + b)