Loop Invariants

Sample Problem

/**
* Find the min in an array of numbers
*/
export function min(array: number[]): number {

}

Solution(s)

/**
* Find the min in an array of numbers
*/
export function min(array: number[]): number {
let minimum = Infinity;
for (const item of array) {
if (item < minimum) {
minimum = item;
}
}
return minimum;
}

/**
* Find the min in an array of numbers
*/
export function minNative(array: number[]): number {
return Math.min(...array);
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

If there is one programming concept that I could pick that will help you ace your coding interview, that will have to be loop in variance. In this lesson, we will demonstrate that with an example that is not particularly algorithmically complex, but will help solidify the concept in your mind so you can talk to it easily in more challenging scenarios. So let's take a look. Now, the programming challenge in question is to find the minimum number in an array of JavaScript numbers. So we've coded up this skeleton function where we take an input array of numbers and return the minimum number.

00:33

Now, the concept of lu in variance is that at any point looping through an input array, we want to maintain some fact about a particular variable. Now, maintaining this invariant or unchanging fact allows us to ensure that once we have gone through all the input elements, we can effectively answer a given question about the entire array. In our particular case, the question that we want to answer is that which number is the minimum? So if you haven't seen any numbers yet, we can effectively assume that the minimum number that we have essentially seen is infinity. And if just in case our input array contains no elements, this is a wise value to return as well.

01:08

Now the next step is to start looping through the input array, which we can do in JavaScript with a simple four of loop. Now, in each iteration of the loop, our objective is to maintain the invariant that the minimum variable points to the minimum value that we have seen so far. So after we see any value, we should check if it is less than the current minimum that we have stored. And if so, we need to update our minimum to this new value. This ensures that the invariant, that the minimum variable is pointing to the lowest value that we have seen so far continues to hold true as we see more and more items within the input array.

01:42

Therefore, when we have gone through all the items in the array, by definition, minimum is pointing to the least value within the input array and thus our correct answer. In terms of challenging programming questions, this was a pretty simple example, but my objective is always to explain the programming concepts in the easiest way possible. Now, just as an added bonus, I will also demonstrate how you would find math admin using JavaScript native functionality. So let's jump back in real World code. If you have to find the minimum of an array of numbers within JavaScript, you would actually use the built-in function

02:14

called math Do Min. That takes the various numbers as arguments. Now, in case you want to pass in an array of numbers, you can actually use the JavaScript spread operator to turn that array into positional arguments. And in our particular case, we have this array being passed in. So I'll replace this local array that I created for a demo and just use that one. An interesting side note is that the built-in JavaScript Math Min function also returns infinity if you give it an empty array, which is similar to the functionality that we wrote ourself.