Coding Challenge: Right Smaller Than

Coding Challenges are the bread and butter of getting a software development position as all major companies test you on coding challenges for software development positions. An entire industry is designed around preparing candidates for coding challenges. Coding challenges are difficult and learning how to approach each coding challenge is difficult. I initially didn't like learning coding challenges. However, after a while i began to enjoy learning them as i felt it opened my eyes to common data structures and patterns. This blog will focus on a coding challenge called “Right Smaller Than” which deals with one of the most common data structures called an array. This coding challenge is considered a difficult coding challenge by many. I hope this blog will help other developers learn how to approach to this problem in a step by step fashion.

The Problem Statement

For every number in an array look to the right of the array and count how many times a number that is smaller than it appears in the array.

The number 8 has 5 numbers smaller than it to the right of the array

Approach to Solving this problem

Solving coding problems like this can be daunting, as human we can easily solve this problem off the top of our head by looking to the right of the number and count how many numbers are smaller than it. Creating a computer function that solves this problem is a lot more difficult as we must understand how computers think in terms of arrays, comparison, counting, storing data and returning the results we are looking for. My approach to solving this problem is first think in terms of how as a human being i would solve this problem and then using my knowledge of programming how i can code this solution so that a computer program can execute the code properly in order to return the correct results. In terms of how we think as human beings compared to how computer programs function is the main difficulty in solving many coding challenges.

How a Human Being will Solve this Problem

Anyone who understand the problem statement will solve this problem easily. If you were to describe how you solved this problem in regular human terms, you can describe it in the following manner.

How can i solve this problem?

Solving Problem as a Computer Program

Lets translate this solution from human terms to computer program terms so we can write code that solves this coding challenge. In the previous example i highlighted key terms in order to show how human terms can be translated to computer code. Lets go over each highlighted part.

The first value of any array in JavaScript starts with an index of 0.

Code Solution

Let’s take a look at one possible solution in JavaScript that utilizes both the human terms and computer program terms discussed earlier.

let array = [8, 5, 11, -1, 3, 4, 2]function rightSmallerThan(array) {const rightSmallerCounts = []for (let i = 0; i < array.length; i++) {let rightSmallerCount = 0for (let j = i + 1; j < array.length; j++) {if (array[j] < array[i])rightSmallerCount++}rightSmallerCounts.push(rightSmallerCount)}return rightSmallerCounts}rightSmallerThan(array)

Code Break Down

I like to write out code on a whiteboard in order to practice whiteboarding which many software developers had to do pre-corona virus days for in person interviews. In the images below i break down step by step what each line of code is doing and how in incorporates many of the human/computer terms we discussed.

Code with numbering
Code Numbering Break Down
Don’t run from coding challenges anymore!!!!

Aspiring Software Engineer and Gamer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store