DEV Community

Cover image for Road to Genius: smart #25
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: smart #25

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

In this post I will discuss two challenge, an easy and a more difficult one.

coding challenge

For the first challenge we have to find R's value. The code starts with an array of random numbers and R=false. Then the for-loop iterates over each number. Then it sets R to true if that number is not equal to 3. The answer will always be true unless every number in arr is 3, the chance of that happening is very small.

coding challenge

The second challenge's code is a whole lot more difficult, but fortunately we can ignore most of it, we only need to fix the bug 😈. Let's have a closer look at the buggy line:
LG = Math.max(LG, i - arr[arr.😈 - 1]);
The seems to be a property (or method) of an array. If you've worked with arrays in JavaScript you might immediately guess that 😈 should be length. The reason is that code like arr.length - 1 is used for getting the last element (i.e. last index) of an array.

coding challenge answer

Let's briefly analyze the code fully to understand its purpose. We have a function that takes one parameter, in this case it takes the following string:
(((())(())()
The code iterates over all parentheses and whether it's an open or closed one it keeps track of some things. The end result is to determine the length of the longest valid parentheses in that string, valid means it open has a corresponding closing parenthesis.
The longest valid parentheses sub-string in this example is:
(())(())() which has length 10 (that's why A=10)

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/

Top comments (0)