Binary search optimizes algorithms that need to search for an index or element in a sorted collection. Problems like these could be solved with a linear search, which produces a time complexity of O(n). Binary search improves the time complexity to O(log n) because the collection size is halved after each comparison.
In computer science, recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Such problems can generally be solved by iteration, but this needs to identify and index the smaller instances at programming time. Recursion solves such recursive problems by using functions that call themselves from within their own code. (Wikipedia)
With the recursion technique, we can replace code written iteratively with clean and simple code. Recursion does not optimize for performance, instead, it prioritizes readability. …
The dynamic-size sliding window pattern optimizes algorithms that involve searching an array or string for a consecutive subsection that satisfies a given condition. Unlike the fixed-size sliding window, this window’s size changes as it moves along the data structure.
To solve problems like these, we could apply a brute force solution with a nested loop, but it would produce O(n²) time complexity at best. Applying the dynamic-size sliding window pattern can reduce the time complexity to O(n) and space complexity to O(1).
In this pattern, two pointers create a window that represents the current subsection of the data structure. One…
The fixed-size sliding window pattern optimizes algorithms that involve searching an array or string for a consecutive subsection, of a given size, that satisfies a given condition. It can also be considered a variation of the two pointers pattern.
To solve problems like these, we could apply a brute force solution with a nested loop, but it would produce O(n²) time complexity at best. Applying the fixed-size sliding window pattern can reduce the time complexity to O(n) and space complexity to O(1).
In this pattern, two pointers create a window that represents the current subsection of the data structure. One…
The two pointers pattern optimizes algorithms that handle strings, sorted arrays, or linked lists by using two pointers to keep track of indices, simplify the logic, and save time and space. In this pattern, two pointers iterate through the data structure, in tandem, until one or both of the pointers hit a certain condition.
This pattern is useful when we need to analyze each element of a collection compared to its other elements.
To solve problems like these, we could start from the first index and loop through one or more times. While a brute force or naive solution with…
We use Big O Notation to analyze the performance of an algorithm.
Big O Notation:
How can we analyze the run time of an algorithm…
The increment and decrement operators are used to change a variable’s value by 1. The increment operator (
++
) increases the value by 1 and the decrement operator (—-
) decreases the value by 1.
If used prefix (++i
or --i
): FIRST, the variable’s value is changed THEN, the variable is used. It returns the value AFTER incrementing/decrementing.
let count = 7console.log(++count)// 8
If used postfix (i++
or i--
): FIRST, the variable is used THEN, the variable’s value is changed. It returns the value BEFORE incrementing/decrementing.
let count = 7console.log(count++)// 7
Let’s look at a JavaScript…
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. (Wikipedia)
ActiveRecord scopes are custom queries defined inside a model and are available as class methods. Scopes take 2 arguments:
Does anyone else immediately think of ‘Revenge of the Nerds’? The Lambda Lambda Lambda fraternity. No? Just me?
A lambda is an object that represents a block and…