How I solve coding challenges using The “Start small grow big” and “Think twice,code once” method

How I solve coding challenges using The “Start small grow big” and “Think twice,code once” method

Hey there, welcome welcome, please take a seat and let’s have a little chat, only that this time i get to do all the talking 😊. It’s nothing serious actually, today ( if you’re seeing this in a couple of days/weeks/months/years time still read it as today) i will be talking about how i solve coding challenges (Both simple and difficult) using the “Think twice code once” and “Start small, grow big” method and I hope you enjoy it.

So last year i took on a technical interview coding question when I applied for the Andela fellowship program,I received a coding challenge that looked a bit simple to the eye and if you are not careful you will end up getting confused, frustrated, or maybe just end up smashing your laptop on the ground or perhaps a wall (I heard that walls are better by the way), because the smallest of problems sometimes end of being the most complex ones, Oh and by the way, I am not the smartest but let’s proceed.

Well I was not careful and I almost lost it countless of times but I solved the coding challenge eventually, I have grown since taking this coding question i must confess and now It is safe to say that I know one of many secrets to actually cracking a coding challenge, but we will just use the Andela coding challenge as a test case.

This is the Question, you need to have a basic knowledge of math and c# ,i used c# in this article because this is what i am currently comfortable with, Although for Andela i used Html, Css and Javascript.

1_mYFGid2YnyLz4m8p2eJGcQ.png

Two methods I adopted recently are the “Think twice, code once” and also “Start small and grow big” methods, and also maybe throw in a little bit of “Whiteboard/pen and paper coding“ before you start typing your genius code into your computer.

Please note, my approach to solving this problem might well differ from yours but that is what makes each and every one of us unique right?. Let's get started.

Breaking down the question (starting small)

Let's kick off by declaring an array, this array will serve as a container where I will be storing all the numbers or values that would be gotten from the dices rolled, and I will also be making sure that I can display each item in the array by using a foreach loop.

1_oLvOVT1qSIGjHFDTK9KrQA.png

Summing up the items in our array

So now that I have succeeded in displaying all the items in the array I am sure that I am on track and my items will definitely display anytime I want it to, “Starting small” remember?, ok next up , notice from the question that the items in the array are actually summed up , take out the conditions that were given in the question and you would definitely notice that it was summed up, for instance {1,2,3} when summed up should actually give us 6, but the conditions attached (if you have a 1, bad luck, next count will be 0) is what gives us 4 as the output, you get the point. So I add up all the items in the array to actually make sure that i am on track. In order to do that, i decided to initialize a variable called sum, this will be used to handle the summation of all the items in the array.

1_3lqN4QTihNNBiDKpp25-7w.png

The output is as should be, {1,2,3} = 6 or {1 + 2 + 3} = 6, so let’s move on yeah? Now it is time to apply the conditions we were given,

Applying the conditions given

For this next step,i decided to declare a variable called a “decider”, u get the pun? “Decided” to declare a “Decider” because without it the result will be false, dry jokes day, but let us move on, I then assigned 1 to the variable decider because it cannot be null or 0, else our condition(s) will not give us the desired result.

So we are almost done yes, it is now time to do a little bit of thinking, remember the “Think twice, code once” method, it doesn’t literally mean that you only get to think two times before you code but you get the point, we have initially done the summation of all the items in our array, i.e int [] dicerolls = {1,2,3} = 6, but now we need to consider the conditions that have been set, i.e if a 1 is rolled, that is bad luck. The next roll counts as 0, else if a 6 is rolled, that is good luck, the next roll is amplified by a factor of 2.

So, since we have {1,2,3} our result should be 4 because according to the condition set, if a 1 is rolled, the next roll counts as 0, meaning {1, 2x0, 3} ,therefore {1,0,3} = 4. Get it?. Lets take another example {2,6,2,5} , our result should be 17 based on the conditions set if a 6 is rolled, that is good luck, the next roll is amplified by a factor of 2, meaning {2, 6 ,2x2, 5}, therefore {2,6,4,5} = 17.

Doing this little math we know what we are to do next, all we have to do is just write a little formula to do the math we just described above, the formula would be sum += (roll * decider); or sum = sum + (roll * decider); , lets keep this formula at the back of our minds as it will be very important.

So now we use our good old "if,else statements" to check the conditions we have been given in this challenge ( if a 1 is rolled, that is bad luck. The next roll counts as 0, else if a 6 is rolled, that is good luck, the next roll is amplified by a factor of 2. )

1_mxBDDs6XVsEXFAgeNWsS-Q.png

Use the Debugger to run a small check

Now is this the right and final answer? We will confirm by using a tool known as the Debugger to run through the loop and see if what we get as our result is correct or an epic fail. Knowing how to use the Debugger properly will save you from a lot of stress now and in the future when you are working on more complex applications. Lets run a small check with the Debugger.

ezgif.com-crop (2).gif

Turns out we are wrong as we didn’t get 4 from {1, 2, 3} but during the debugging process we got getting 6 instead. So what went wrong? Yup, I didn’t use the formula, hope you still remember it tho, sum += (diceroll * decider); .

Thinking twice (making use of realistic)

Let’s try out a realistic example with the formula, recall that to print out all the items in the array we printed “diceroll” as in Console.WriteLine(diceroll); // 1,2,3. And to get the sum of all the items we said “sum += diceroll” and we print sum as in Console.WriteLine(sum); // 6 , so based on the condition we are given(if a 1 is rolled, that is bad luck. The next roll counts as 0 ), and the formula we are to use i.e sum += (diceroll * decider) , any number that comes after 1 will be multiplied by 0 ,note that the value “0” is tied to the variable “decider”. For better understanding, look at it this way {diceroll (1), diceroll (2) * decider (0) = 0, diceroll (3) },where sum= {1 + 0 + 4} // 4

Let’s look at another example, to print out all the items in the array we printed “diceroll” as in Console.WriteLine(diceroll); // {2,6,2,5}. And to get the sum of all the items we said “sum += diceroll” and we print sum as in Console.WriteLine(sum);//15 , so based on the condition we are given(if a 1 is rolled, that is bad luck. The next roll counts as 0 ), and the formula we are to use i.e sum += (diceroll * decider) , any number that comes after 1 will be multiplied by 0 ,note that the value “0” is tied to the variable “decider”. For better understanding, look at it this way {diceroll (2), diceroll (6) , diceroll (2) * decider (2) = 4 , diceroll (5) } ,where sum= {2 + 6 + 4+5} // 17.

Conclusion

We now have a clearer picture of the result we want to achieve, so let’s proceed to adding our formula and see if we nailed this challenge or neh.

1_vV-2GyairwusQLyO2A662g.png

1_ujPPyuTi3c3sioHaH22vmg.png

1_y8KxEIBggk4cauZyN3raeg.png

Well there we have it, the formula worked and our program is running fine, we all are different and we have other ways of solving this said challenge and that’s what makes us unique, this is my thought process, i hope you enjoyed it.

Thanks for reading, please leave your seats behind 😊