How to Solve a Coding Challenge in an Optimized Way

How to Solve a Coding Challenge in an Optimized Way

In this article series, I promised to go through the practical applications of Data Structures and Algorithms. But before moving towards that I would like to go through with me a basic and easy beginner-level coding problem, and then make it (not complicated but) as much efficient as we can. But you may think, why do I want to do that? Actually, there are three things that I want to achieve in this article:

  1. To appreciate how simple problems can not only have different solutions but how small better choices could optimize the time and space complexity of our solutions.
  2. To look at the various steps of solving the coding challenges.
  3. How to make simple things complex to make boring things a little interesting??

The coding challenge I have selected is the "FizzBuzz" problem. This is the first coding problem that Microverse gave us to solve.

No alt text provided for this image
FizzBuzz Coding Problem

1. Understand the problem

The problem is asking us to write a program that generates an array of integers from 1 to 100. For multiples of 3, we should print "Fizz" instead of the number. For multiples of 5, we should print "Buzz" instead of the number. For multiples of both 3 and 5, we should print "FizzBuzz" instead of the number.

So this is a plain text read. But what would it look like? Like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, ..., 100.

Ahan. So let's do our second step:

2. Pseudocode

So by reading and understanding the code we will write our pseudocode. The pseudocode for our problem is as follows:

1. Create an empty array called 'results
2. Loop from 1 to 100, with variable i
3. If i is divisible by both 3 and 5, add 'FizzBuzz' to the results array
4. Else if i is divisible by 3, add 'Fizz' to the results array
5. Else if i is divisible by 5, add 'Buzz' to the results array
6. Else, add i to the results array
7. Return the results array'

3. Attempting our Solution:

Now we have to look at our pseudocode and write our solution.

function fizzBuzz() {
  const arr = [];
  
  for (let i = 1; i <= 100; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      arr.push('FizzBuzz');
    } else if (i % 3 === 0) {
      arr.push('Fizz');
    } else if (i % 5 === 0) {
      arr.push('Buzz');
    } else {
      arr.push(i);
    }
  }
  
  return arr;
}

This is a simple solution to this problem. So this solution is doing this:

  1. So we just created an empty array.
  2. And then running a loop over integers from 1-100.
  3. If we have a number divisible by 15, we will push "FizzBuzz" to the array.
  4. If it is not divisible by 15, but divisible by 3 or 5, we will push "Fizz" or "Buzz" to the array respectively.
  5. If it is not divisible by 15, 3, or 5, just push that number in the array as it is.

That's it? No. That is a good solution. But it can be improved by making it more efficient and readable. e.g.

i) Since we are absolutely sure that we have 100 elements to store in an array, then why not create a static array of 100 elements instead of a dynamic array?

ii) Since we know that 15 is divisible by both 3 and 5, then why checking that if an integer is divisible by 15 or not separately?

iii) At every instance, when it is "FizzBuzz", "Fizz" or "Buzz", it is creating a new string to push in the array. Why don't we think of saving "Fizz" and "Buzz" in some variable to use over and over again? (Just to improve readability. Just imagine if we had a long string to use here)

I have found out these three small issues that could be improved. If you find something other than that, do let me know in the comments.

So let's create an optimized solution:

function fizzBuzz() {
  const arr = new Array(100);
  const fizz = "Fizz";
  const buzz = "Buzz";
  
  for (let i = 0; i < arr.length; i++) {
    const num = i + 1;
    let str = "";
    
    if (num % 3 === 0) {
      str += fizz;
    }
    if (num % 5 === 0) {
      str += buzz;
    }
    if (str === "") {
      str = num;
    }
    
    arr[i] = str;
  }
  
  return arr;
}

See? How certain it is. Knows an array of exactly 100 elements needed. If we need fewer or more elements, we can control it by just changing the numbers, and it will take care of memory management by creating a static array and by not allocating more memory as dynamic arrays do. Secondly, it eliminates the if-else statements blocks and then breaks the code free from checking the divisibility by 15, since now it can check the divisibility of both 3 and 5 separately. Lastly, by just creating variables once and using them over and over again.

That's it!

You can also try this coding challenge in GitHub Classroom by yourself. Accept the FizzBuzz problem exercise link and try that out yourself.


#DataStructures #Algorithms #CodingChallenge #FizzBuzz #Optimization #JavaScript #Programming #BeginnerLevel

要查看或添加评论,请登录

Rao Akif的更多文章

社区洞察

其他会员也浏览了